最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

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

C# crifan 3835浏览 0评论

【问题】

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

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

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.165 seconds, using 22.12MB memory