excel.Save("D:" + "123.xls");出现Exception from HRESULT: 0x800A03EC
可以导出excel表格,但是如果点击取消的时候就会出现下面的问题,求解!
源代码如下:
//将数据导出Excel表
public bool ExportDataGridview(DataGridView gridView, bool isShowExcle)
{
if (gridView.Rows.Count == 0)
{
return false;
}
//创建Excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(true);
//生成字段名称
for (int i = 0; i < gridView.ColumnCount;i++)
{
excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText;
}
//填充数据
for (int i = 0; i < gridView.RowCount - 1; i++) //循环行
{
for(int j = 0;j < gridView.ColumnCount ;j++) //循环列
{
if(gridView[j,i].ValueType==typeof(string))
{
excel.Cells[i+2,j+1] = "'" + gridView.Rows[i].Cells[j].Value.ToString();
}
else
{
excel.Cells[i+2,j+1] = gridView.Rows[i].Cells[j].Value.ToString();
}
}
}
//设置禁止弹出保存和覆盖的询问提示框
excel.Visible = false;
excel.DisplayA