首页 > 代码库 > .Net常用技巧_调用Excel模板格式导出Excel
.Net常用技巧_调用Excel模板格式导出Excel
object missing = System.Reflection.Missing.Value; //表示缺i省object
SaveFileDialog savedialog = new SaveFileDialog();
savedialog.Filter = "(*.xlsx)|*.xlsx|(*.xls)|*.xls";
savedialog.Title = "导成Excel";
if (savedialog.ShowDialog() == DialogResult.OK)
{
Microsoft.Office.Interop.Excel.ApplicationClass App = new Microsoft.Office.Interop.Excel.ApplicationClass();
//Microsoft.Office.Interop.Excel.Workbook workbook = App.Workbooks._Open("C:/Documents and Settings/Administrator/桌面/WindowsForms/WindowsForms/20130122.xlsx", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
//“/”是用在系统上,而“\“一般是用在网络上,所以需要使用@符号转义了
Microsoft.Office.Interop.Excel.Workbook workbook = App.Workbooks._Open(System.Environment.CurrentDirectory+
@"\WindowsForms\WindowsForms\20130122.xlsx", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range("A3", "C3");
Object[] objectdata =http://www.mamicode.com/{"123", "123", "123" };
range.Value2 = objectdata;
App.DisplayAlerts = false;//禁用该“保存”对话框
workbook.SaveCopyAs(savedialog.FileName);
workbook.Close(missing, missing, missing);
App.DisplayAlerts = true;
App.Quit();
GC.Collect();
MessageBox.Show("导出成功!", "Excel", MessageBoxButtons.OK);
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。