来自HRESULT的异常:0x800AC472在导出期间从datagridview excel [英] Exception from HRESULT: 0x800AC472 during Export to excel from datagridview
问题描述
嗨我想将datagridview数据导出到excel但是在1198记录之后它显示错误来自HRESULT:0x800AC472
请帮帮我
我的代码在这里
Microsoft.Office。 Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
hi I want to export datagridview data to excel but after 1198 records it is showing the error Exception from HRESULT: 0x800AC472
please help me
my code here
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
// creating new Excelsheet in workbook
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
// see the excel sheet behind the program
app.Visible = true;
// get the reference of first sheet. By default its name is Sheet1.
// store its reference to worksheet
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
// changing the name of active sheet
worksheet.Name = "Exported from gridview";
// storing header part in Excel
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString().Trim();
}
}
// save the application
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
workbook.SaveAs(Application.StartupPath + "\\Reports\\" + System.DateTime.Now.ToString("dd-MMM-yy") + ".xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
推荐答案
检查行号中单元格的值。 1198.如果它从=
或+
或-
开始,这就是例外的原因。
看看这里找到将datagridview数据导出到Excel的替代方法:如何使用c#将数据导出到Excel中[ ^ ]
<将数据从应用程序导出到Excel中的最佳和最快捷的方法是使用ADO.NET(OLEDB)。
使用MS Excel和ADO.NET [ ^ ]
如何使用ADO.NET检索和修改Excel工作簿中的记录使用Visual Basic .NET [ ^ ] - 使用vb .net,但vb.net类似于C#。
Check the value of cells in row no. 1198. If it starts from=
or+
or-
, this is the reason of exception.
Have a look here to find alternative ways to export datagridview data into Excel: How to export data to Excel using c#[^]
The best and the quickest way to export data from your aplication into Excel is to use ADO.NET (OLEDB).
Work with MS Excel and ADO.NET[^]
How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With Visual Basic .NET[^] - uses vb.net, but vb.net is similar to C#.
这篇关于来自HRESULT的异常:0x800AC472在导出期间从datagridview excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!