【未解决】C#操作excel出错:System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC

【问题】

一个C#程序,操作excel,结果运行到后来出错。

完整的log错误为:

See the end of this message for details on invoking 

just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************

System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC

   at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)

   at Microsoft.Office.Interop.Excel.Range.set__Default(Object RowIndex, Object ColumnIndex, Object )

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.appendInfoToFile(String fullFilename, AmazonProductInfo productInfo)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.saveProductInfo(AmazonProductInfo productInfo)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.processAmazonItem(String itemAsin)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.processAwsSearchItem(awsSearchResultItem singleAwsSearchItem)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.searchSingleBrowseNodeId(String curBrowseNodeId, String curSearchIndex, String curFullCategoryName)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.doSearchForAllChildOfSingleTreeNode(TreeNode curTreeNode)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.doSearchForAllChildOfSingleTreeNode(TreeNode curTreeNode)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.doSearchForAllChildOfSingleTreeNode(TreeNode curTreeNode)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.doSearchForAllChildOfSingleTreeNode(TreeNode curTreeNode)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.doSearchForAllChildOfSingleTreeNode(TreeNode curTreeNode)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.doSearchForAllChildOfSingleTreeNode(TreeNode curTreeNode)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.awsCategorySearch()

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.btnSearch_Click(Object sender, EventArgs e)

   at System.Windows.Forms.Control.OnClick(EventArgs e)

   at System.Windows.Forms.Button.OnClick(EventArgs e)

   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)

   at System.Windows.Forms.Control.WndProc(Message& m)

   at System.Windows.Forms.ButtonBase.WndProc(Message& m)

   at System.Windows.Forms.Button.WndProc(Message& m)

   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)

   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

************** Loaded Assemblies **************

mscorlib

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5472 (Win7SP1GDR.050727-5400)

    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll

—————————————-

ScrapeAmazonProduct

    Assembly Version: 1.0.0.0

    Win32 Version: 4.4.0.0

    CodeBase: file:///C:/Users/Eli%20Shimony/Desktop/ScrapeAmazonProduct_2013-07-18_home_checkAllCategory.exe

—————————————-

System.Windows.Forms

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5468 (Win7SP1GDR.050727-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll

—————————————-

System

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5467 (Win7SP1GDR.050727-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll

—————————————-

System.Drawing

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5467 (Win7SP1GDR.050727-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll

—————————————-

System.Configuration

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5473 (Win7SP1GDR.050727-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll

—————————————-

System.Xml

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5473 (Win7SP1GDR.050727-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll

—————————————-

NLog

    Assembly Version: 2.0.0.0

    Win32 Version: 4.4.0.0

    CodeBase: file:///C:/Users/Eli%20Shimony/Desktop/ScrapeAmazonProduct_2013-07-18_home_checkAllCategory.exe

—————————————-

System.ServiceModel

    Assembly Version: 3.0.0.0

    Win32 Version: 3.0.4506.5452 (Win7SP1GDR.030729-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.ServiceModel/3.0.0.0__b77a5c561934e089/System.ServiceModel.dll

—————————————-

System.Data

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5420 (Win7SP1.050727-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll

—————————————-

System.Core

    Assembly Version: 3.5.0.0

    Win32 Version: 3.5.30729.5420 built by: Win7SP1

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Core/3.5.0.0__b77a5c561934e089/System.Core.dll

—————————————-

System.Runtime.Serialization

    Assembly Version: 3.0.0.0

    Win32 Version: 3.0.4506.5452 (Win7SP1GDR.030729-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Runtime.Serialization/3.0.0.0__b77a5c561934e089/System.Runtime.Serialization.dll

—————————————-

System.Web

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5471 (Win7SP1GDR.050727-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_32/System.Web/2.0.0.0__b03f5f7f11d50a3a/System.Web.dll

—————————————-

HtmlAgilityPack

    Assembly Version: 1.4.6.0

    Win32 Version: 4.4.0.0

    CodeBase: file:///C:/Users/Eli%20Shimony/Desktop/ScrapeAmazonProduct_2013-07-18_home_checkAllCategory.exe

—————————————-

System.Web.Extensions

    Assembly Version: 3.5.0.0

    Win32 Version: 3.5.30729.5454

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Web.Extensions/3.5.0.0__31bf3856ad364e35/System.Web.Extensions.dll

—————————————-

System.Web.Abstractions

    Assembly Version: 3.5.0.0

    Win32 Version: 3.5.30729.5420

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Web.Abstractions/3.5.0.0__31bf3856ad364e35/System.Web.Abstractions.dll

—————————————-

Microsoft.Office.Interop.Excel

    Assembly Version: 12.0.0.0

    Win32 Version: 12.0.6600.1000

    CodeBase: file:///C:/Windows/assembly/GAC/Microsoft.Office.Interop.Excel/12.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.Excel.dll

—————————————-

************** JIT Debugging **************

To enable just-in-time (JIT) debugging, the .config file for this

application or computer (machine.config) must have the

jitDebugging value set in the system.windows.forms section.

The application must also be compiled with debugging

enabled.

For example:

<configuration>

    <system.windows.forms jitDebugging="true" />

</configuration>

When JIT debugging is enabled, any unhandled exception

will be sent to the JIT debugger registered on the computer

rather than be handled by this dialog box.

截图为:

Exception from HRESULT 0x800A03EC

【解决过程】

1.参考:

c# 调用excle 错误 HRESULT: 0x800A03EC 如何解决

提到了说是index不能是0,而要从1开始的问题,和这里:

Exception from HRESULT: 0x800A03EC Error

提到的一样。

但是我的程序是运行了很长时间,excel都操作保存了3654行了,一直都是正常的,应该是,不存在index为0的问题。

2.上面帖子也提到了,说,可能是和locale有关。

3.此处,和excel的,相关代码为:

private void appendInfoToFile(string fullFilename, AmazonProductInfo productInfo)
{
    gLogger.Info("Saving product info for " + productInfo.url);

    bool isAutoFitForFistColumn = true; 
    
    Excel.Application xlApp;
    Excel.Workbook xlWorkBook;
    Excel.Worksheet xlWorkSheet;
    object missingVal = System.Reflection.Missing.Value;

    xlApp = new Microsoft.Office.Interop.Excel.Application();
    //xlApp.Visible = true;
    //xlApp.DisplayAlerts = false;

    //http://msdn.microsoft.com/zh-cn/library/microsoft.office.interop.excel.workbooks.open%28v=office.11%29.aspx
    xlWorkBook = xlApp.Workbooks.Open(
        Filename : fullFilename,
        //UpdateLinks:3,
        ReadOnly : false,
        //Format : 2, //use Commas as delimiter when open text file
        //Password : missingVal,
        //WriteResPassword : missingVal,
        //IgnoreReadOnlyRecommended: false, //when save to readonly, will notice you
        Origin: Excel.XlPlatform.xlWindows, //xlMacintosh/xlWindows/xlMSDOS
        //Delimiter: ",",  // usefule when is text file
        Editable : true,
        Notify : false,
        //Converter: missingVal, 
        AddToMru: true, //True to add this workbook to the list of recently used files
        Local: true,
        CorruptLoad: missingVal //xlNormalLoad/xlRepairFile/xlExtractData
        );

    //Get the first sheet
    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); //also can get by sheet name
    Excel.Range range = xlWorkSheet.UsedRange;
    //int usedColCount = range.Columns.Count;
    int usedRowCount = range.Rows.Count;

    const int excelRowHeader = 1;
    const int excelColumnHeader = 1;

    //int curColumnIdx = usedColCount + excelColumnHeader;
    int curColumnIdx = 0 + excelColumnHeader; //start from column begin
    int curRrowIdx = usedRowCount + excelRowHeader; // !!! here must added buildin excelRowHeader=1, otherwise will overwrite previous (added title or whole row value)

    xlWorkSheet.Cells[curRrowIdx, curColumnIdx++] = productInfo.title;
    xlWorkSheet.Cells[curRrowIdx, curColumnIdx++] = productInfo.description;

    const int constBullerLen = 5;
    int bulletListLen = 0;
    if (productInfo.bulletArr.Length > constBullerLen)
    {
        bulletListLen = constBullerLen;
    }
    else
    {
        bulletListLen = productInfo.bulletArr.Length;
    }
    for (int bulletIdx = 0; bulletIdx < bulletListLen; bulletIdx++)
    {
        xlWorkSheet.Cells[curRrowIdx, curColumnIdx + bulletIdx] = productInfo.bulletArr[bulletIdx];
    }
    curColumnIdx = curColumnIdx + bulletListLen;

    const int constImgNameListLen = 5;
    int imgNameListLen = 0;
    if (productInfo.imgUrlArr.Length > constImgNameListLen)
    {
        imgNameListLen = constImgNameListLen;
    }
    else
    {
        imgNameListLen = productInfo.imgUrlArr.Length;
    }
    for (int imgIdx = 0; imgIdx < imgNameListLen; imgIdx++)
    {
        xlWorkSheet.Cells[curRrowIdx, curColumnIdx + imgIdx] = productInfo.imgUrlArr[imgIdx];
    }
    curColumnIdx = curColumnIdx + imgNameListLen;

    xlWorkSheet.Cells[curRrowIdx, curColumnIdx++] = productInfo.cheapestPrice;
    xlWorkSheet.Cells[curRrowIdx, curColumnIdx++] = productInfo.isOneSellerIsAmazon;
    xlWorkSheet.Cells[curRrowIdx, curColumnIdx++] = productInfo.reviewNumber;
    xlWorkSheet.Cells[curRrowIdx, curColumnIdx++] = productInfo.isBestSeller;

    //(2) auto adjust first column width (according to content)
    if (isAutoFitForFistColumn)
    {
        //Range firstColumn = (Range)xlWorkSheet.Columns[0];
        Range firstColumn = xlWorkSheet.get_Range("A1");
        //firstColumn.AutoFit();
        firstColumn.EntireColumn.AutoFit();
    }
    
    ////http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=ZH-CN&k=k%28MICROSOFT.OFFICE.INTEROP.EXCEL._WORKBOOK.SAVEAS%29;k%28SAVEAS%29;k%28TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV3.5%22%29;k%28DevLang-CSHARP%29&rd=true
    //xlWorkBook.SaveAs(
    //    Filename: fullFilename,
    //    ConflictResolution: XlSaveConflictResolution.xlLocalSessionChanges //The local user's changes are always accepted. 
    //    //FileFormat : Excel.XlFileFormat.xlWorkbookNormal
    //);

    //if use above SaveAs -> will popup a window ask you overwrite it or not, even if you have set the ConflictResolution to xlLocalSessionChanges, which should not ask, should directly save
    xlWorkBook.Save();

    //http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=ZH-CN&k=k%28MICROSOFT.OFFICE.INTEROP.EXCEL._WORKBOOK.CLOSE%29;k%28CLOSE%29;k%28TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV3.5%22%29;k%28DevLang-CSHARP%29&rd=true
    xlWorkBook.Close(SaveChanges : true);

    crl.releaseObject(xlWorkSheet);
    crl.releaseObject(xlWorkBook);
    crl.releaseObject(xlApp);
}

4.另外看到:

c# 调用excle 错误 HRESULT: 0x800A03EC 如何解决

中提到:

“是不是超出范围造成的,我今天也遇到了,后来是减少了需要导出的列数就好了 ”

和我这里,有点类似。

 

【总结】



发表评论

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

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