在“NET”选项卡上,找到“ADODB”,然后单击“选择”。
在“COM”选项卡上,找到“Microsoft Excel 10.0 对象库或 Microsoft Excel 11.0 对象库”,然后单击“选择”。
引入命名空间
using System.Reflection;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
创建代码:
private
void button1_Click(
object sender, System.EventArgs e)
{
object missing = System.Reflection.Missing.Value;
Excel.ApplicationClass myExcel = new Excel.ApplicationClass();
Excel._Workbook xBk ; //工作薄
Excel._Worksheet xSt; //工作Sheet
xBk = myExcel.Workbooks.Add(true);
for (int i = 0; i < 5; i ++)
{
xSt = (Excel._Worksheet)xBk.ActiveSheet;
xSt.Name ="chenms_" + i ;
myExcel.Cells[2,2]="chenms";
if(i < 4)
{
myExcel.Sheets.Add(missing,missing,1,Excel.XlSheetType.xlWorksheet);
}
}
myExcel.Visible = true;
xBk.SaveAs(@"d:cms.xls",missing,missing,
missing,missing,missing,Excel.XlSaveAsAccessMode.xlShared,
missing,missing,missing,missing,missing);
myExcel.Quit();
}
{
object missing = System.Reflection.Missing.Value;
Excel.ApplicationClass myExcel = new Excel.ApplicationClass();
Excel._Workbook xBk ; //工作薄
Excel._Worksheet xSt; //工作Sheet
xBk = myExcel.Workbooks.Add(true);
for (int i = 0; i < 5; i ++)
{
xSt = (Excel._Worksheet)xBk.ActiveSheet;
xSt.Name ="chenms_" + i ;
myExcel.Cells[2,2]="chenms";
if(i < 4)
{
myExcel.Sheets.Add(missing,missing,1,Excel.XlSheetType.xlWorksheet);
}
}
myExcel.Visible = true;
xBk.SaveAs(@"d:cms.xls",missing,missing,
missing,missing,missing,Excel.XlSaveAsAccessMode.xlShared,
missing,missing,missing,missing,missing);
myExcel.Quit();
}