【已解决】C#中保存数据到Worksheet结果出错:An unhandled exception of type ‘System.Runtime.InteropServices.COMException’ occurred in mscorlib.dll。Additional information: Exception from HRESULT: 0x800A03EC

【问题】

C#中,通过代码:

            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;

            object misValue = System.Reflection.Missing.Value;
            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            int i = 0;
            int j = 0;

            //save header
            for (i = 0; i <= dgvSearchResult.ColumnCount - 1; j++)
            {
                xlWorkSheet.Cells[0, i] = dgvSearchResult.Columns[i].HeaderText;
            }

去将数据保存到excel中,结果在:

xlWorkSheet.Cells[0, i] = dgvSearchResult.Columns[i].HeaderText;

时出错:

An unhandled exception of type ‘System.Runtime.InteropServices.COMException’ occurred in mscorlib.dll

Additional information: Exception from HRESULT: 0x800A03EC

 

System.Runtime.InteropServices.COMException Exception from HRESULT 0x800A03EC

【解决过程】

1.参考之前的相关代码:

            //save cells
            for (i = 1; i <= dgvSearchResult.RowCount - 1; i++)
            {
                for (j = 0; j <= dgvSearchResult.ColumnCount - 1; j++)
                {
                    DataGridViewCell cell = dgvSearchResult[j, i];
                    if (j == girUrlColumnIdx)
                    {
                        xlWorkSheet.Cells[i + 1, j + 1] = cell.Tag.ToString();
                    }
                    else
                    {
                        xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
                    }
                }
            }

貌似,对于cell[0,0],是个特殊的值,所以无法直接赋值。

2.所以去改为:

xlWorkSheet.Cells[0+1, i+1] = dgvSearchResult.Columns[i].HeaderText;

试试,结果代码执行异常,没有按照预料的,执行后续的代码。

但是的确错误没了。

 

【总结】

后来的研究表明,column[0]和row[0],对应的是excel的左边竖向的索引号那列,和顶部的从左到右的标题列:

row 0 and column 0

所以,在excel中,去给column0或row0赋值时:

xlWorkSheet.Cells[0, i] = dgvSearchResult.Columns[i].HeaderText;

会出错。

解决办法是:

空出column和row0,从余下的

cells[i+1, j+1]

去赋值,即可:

            //save header
            for (i = 0; i <= dgvSearchResult.ColumnCount - 1; i++)
            {
                xlWorkSheet.Cells[0+1, i+1] = dgvSearchResult.Columns[i].HeaderText;
            }

            //save cells
            for (i = 0; i <= dgvSearchResult.RowCount - 1; i++)
            {
                for (j = 0; j <= dgvSearchResult.ColumnCount - 1; j++)
                {
                    DataGridViewCell cell = dgvSearchResult[j, i];
                    if (j == girUrlColumnIdx)
                    {
                        xlWorkSheet.Cells[i + 2, j + 1] = cell.Tag.ToString();
                    }
                    else
                    {
                        xlWorkSheet.Cells[i + 2, j + 1] = cell.Value;
                    }
                }
            }



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量