在“NET”选项卡上,找到“ADODB”,然后单击“选择”。
在“COM”选项卡上,找到“Microsoft Excel 10.0 对象库或 Microsoft Excel 11.0 对象库”,然后单击“选择”。
引入命名空间
using
System.Reflection;
using
System.Runtime.InteropServices;
using
Excel
=
Microsoft.Office.Interop.Excel;
创建代码:
private
void
button1_Click(
object
sender, System.EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
object missing = System.Reflection.Missing.Value;
Excel.ApplicationClass myExcel = new Excel.ApplicationClass();
Excel._Workbook xBk ; //工作薄
Excel._Worksheet xSt; //工作Sheet
xBk = myExcel.Workbooks.Add(true);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
for (int i = 0; i < 5; i ++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
xSt = (Excel._Worksheet)xBk.ActiveSheet;
xSt.Name ="chenms_" + i ;
myExcel.Cells[2,2]="chenms";
if(i < 4)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
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();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}