用C#操作Excel
1。新建c#控制台工程:textexcel:;
2。插入「Microsoft Excel 10.0 Object Library」;
3。修改class1.cs,如下:
using System;
using System.Reflection;
namespace ExcelTest
{
class Class1
{
static void Main()
{
/*启动excel*/
Excel.Application app = new Excel.Application();
Excel._Workbook wb = app.Workbooks.Add(Missing.Value);
wb.Application.Visible = true;
/*填充内容*/
Excel._Worksheet ws = (Excel.Worksheet)wb.ActiveSheet;
ws.Cells[1, 1] = "城市";
ws.Cells[2, 1] = "上海";
ws.Cells[3, 1] = "西安";
ws.Cells[4, 1] = "沈阳";
ws.Cells[1, 2] = "人口";
ws.Cells[2, 2] = 1243455;
ws.Cells[3, 2] = 8831177;
ws.Cells[4, 2] = 8687422;
}
}
}