A
A
AndNovak2016-08-15 16:29:10
C++ / C#
AndNovak, 2016-08-15 16:29:10

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

2 answer(s)
A
AndNovak, 2016-08-15
@AndNovak

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();
                    }
                   


                    }
                }

S
Sanan Yuzb, 2016-08-15
@Sanan07

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 question

Ask a Question

731 491 924 answers to any question