关于vs2008 C#导出Excel2007的问题
winform中读取了数据库的数据,保存在数据集ds中,想把这些数据导出为一个Excel表格,但是导出时出现问题,代码如下:
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "excel files (*.xls)|*.xls|All files (*.*)|*.*";
sfd.FilterIndex = 1;
sfd.RestoreDirectory = true;
if (sfd.ShowDialog() == DialogResult.OK)
{
Excel.ApplicationClass xlApp = new Excel.ApplicationClass();
if (xlApp == null)
{
MessageBox.Show("无法创建Excel文档");
}
else
{
//Excel.Workbook workbook = xlApp.Workbooks.Add(true);
Excel.Workbook workbook = xlApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
//先只保存一个表头
worksheet.Cells[1, i] = ds.Tables[0].Columns[i].ColumnName;
}
workbook.Saved = true;
workbook.SaveCopyAs(sfd.FileName);
}
&n