Answer the question
In order to leave comments, you need to log in
How to export to excel?
When exporting data from datagridview, an exception "HRESULT: 0x800A03EC" occurs.
For export, I use this code. I thought that he was swearing at cells with a null value, but alas, no
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
ExcelApp.Application.Workbooks.Add(System.Type.Missing);
ExcelApp.Visible = true;
for (int i = 0; i < dataGridViewProduct.ColumnCount; i++)
{
for (int j = 0; j < dataGridViewProduct.RowCount; j++)
{
ExcelApp.Cells[j + 2, i + 1] = dataGridViewProduct[i, j].Value;
}
}
Answer the question
In order to leave comments, you need to log in
I'll answer my own question
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
ExcelApp.Application.Workbooks.Add(System.Type.Missing);
ExcelApp.Visible = true;
for (int i = 0; i < dataGridViewProduct.ColumnCount; i++)
{
for (int j = 0; j < dataGridViewProduct.RowCount; j++)
{
if (dataGridViewProduct[i, j].Value == null)
ExcelApp.Cells[j + 2, i + 1] = " ";
else
{
ExcelApp.Cells[j + 2, i + 1] = dataGridViewProduct[i, j].Value.ToString();
}
}
}
I also used this library in my time, but unfortunately the code did not remain. I think you forgot to add the Workbook to the excel file.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question