加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Excel Interop Worksheet副本引发异常HRESULT:0x800A03EC

发布时间:2020-12-15 21:41:45 所属栏目:百科 来源:网络整理
导读:我在使用Interop在Excel 2003中复制工作表时遇到问题.该代码适用于30-40个副本,然后抛出异常“HRESULT异常:0x800A03EC”.下面的测试代码已经在 http://support.microsoft.com/kb/210684/en-us中包含一个补丁,但没有成功. 有谁知道任何解决方法吗? 提前致谢
我在使用Interop在Excel 2003中复制工作表时遇到问题.该代码适用于30-40个副本,然后抛出异常“HRESULT异常:0x800A03EC”.下面的测试代码已经在 http://support.microsoft.com/kb/210684/en-us中包含一个补丁,但没有成功.
有谁知道任何解决方法吗?
提前致谢

using Excel = Microsoft.Office.Interop.Excel;

private void button2_Click(object sender,EventArgs e)
    {
        string template_path = @"c:temptemplate.xlt";
        string filename = @"c:temptestxl1.xls";
        if (File.Exists(filename))
            File.Delete(filename);

        object missing = System.Reflection.Missing.Value;

        System.Globalization.CultureInfo my_culture = System.Threading.Thread.CurrentThread.CurrentCulture;
        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

        Excel.ApplicationClass xlapp = new Excel.ApplicationClass();
        xlapp.Visible = true;
        xlapp.UserControl = true;

        Excel.Workbook xlwb = xlapp.Workbooks.Add(template_path);

        xlwb.SaveAs(@filename,missing,Excel.XlSaveAsAccessMode.xlNoChange,missing);


        Excel.Worksheet xlws = xlwb.Worksheets["diario"] as Excel.Worksheet;

        int copies = 200;

        for (int i = 0; i < copies; i++)
        {
            #region Patch kb/210684

            if (copies % 10 == 0)
            {                    
                xlwb.Close(true,missing);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlwb);
                xlwb = null;

                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlws);
                xlws = null;

                Application.DoEvents();

                xlwb = xlapp.Workbooks.Open(@filename,missing);
                xlws = xlwb.Worksheets["diario"] as Excel.Worksheet;

            }

            #endregion

            xlws.Copy(System.Reflection.Missing.Value,xlwb.Worksheets.Count);
        }

        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlws);
        xlws = null;

        xlwb.Close(true,missing);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlwb);
        xlwb = null;

        xlapp.Quit();
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlapp);
        xlapp = null;

        System.Threading.Thread.CurrentThread.CurrentCulture = my_culture;
    }

解决方法

我唯一能找到的是当本地机器未设置为美国英语时异常可能是Excel LCID问题.

http://www.made4dotnet.com/Default.aspx?tabid=141&aid=15

要么

http://support.microsoft.com/kb/320369/en-us

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读