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 ={ "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);
}
.Net常用技巧_调用Excel模板格式导出Excel,古老的榕树,5-wow.com