using Excel = Microsoft.Office.Interop.Excel;
先引用Excel再输入以下代码
private void button2_Click(object sender, EventArgs e)
{
//创建一个Excel应用
Excel.Application app1 = new Excel.Application();
//默认情况下,Excel程序在后台运行,使其可见
app1.Visible = true;
if (app1 == null)
{
MessageBox.Show("没有安装Excel!", "提示");
return;
}
//在Excel中添加一个工作簿同时返回book1
Microsoft.Office.Interop.Excel.Workbook book1 = app1.Workbooks.Add(Type.Missing);
//在Excel中添加一个工作表将其返回到sheet1
Microsoft.Office.Interop.Excel.Worksheet sheet1 = (Microsoft.Office.Interop.Excel.Worksheet)book1.Sheets[1];
//声明适当变量获取行列数
string sValue;
int i, iRows