【问题标题】:Exception from HRESULT: 0x800A03EC while copy excel sheet来自 HRESULT 的异常:0x800A03EC 复制 excel 表时
【发布时间】:2015-07-31 05:18:27
【问题描述】:

我正在尝试将现有的 Excel 工作表复制到我当前的 Excel 文件中。我正在使用此代码。

                            Workbook wkActive = Globals.ThisAddIn.Application.ActiveWorkbook;
                        Microsoft.Office.Interop.Excel.Workbook workbook = Globals.ThisAddIn.Application.Workbooks.Open(IdsTemplatePath, 0, true, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, false, false);
                        Microsoft.Office.Interop.Excel.Worksheet worksheet = workbook.Sheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
                        worksheet.Copy(Type.Missing, wkActive);
                        wkActive.Save();

但是在复制方法中我得到错误

Exception from HRESULT: 0x800A03EC

我做错了什么,我应该怎么做才能完成我的任务

【问题讨论】:

  • 你为什么要传递Readonly true并将Converter从0改为1。

标签: c# excel vsto


【解决方案1】:

我找到了解决办法

Microsoft.Office.Interop.Excel.Workbook CurrentWk 
    = ((Microsoft.Office.Interop.Excel.Workbook)Globals.ThisAddIn.Application.ActiveWorkbook);
if (Path.GetExtension(CurrentWk.Name) != ".xlsx")
{
    MessageBox.Show("Please save the document before complete this task", "warning");
}
else
{
    Microsoft.Office.Interop.Excel.Workbook workbook = Globals.ThisAddIn.Application.Workbooks.Open(IdsTemplatePath, 0, true, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, false, false);
    Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
    totalSheet = workbook.Worksheets.Count;
    for (int sheetNum = 1; sheetNum <= totalSheet; sheetNum++)
    {
        worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[sheetNum];
        sheetname = worksheet.Name.Replace("\r", "").Replace("\a", "").Trim().ToLower();
        if (sheetname == "ids_template")
        {
            try
            {
                worksheet.Copy(CurrentWk.Sheets[1]);
                workbook.Close(SaveChanges, Type.Missing, Type.Missing);
            }
            catch { }
            ids_template_found = true;//here it is set true because template sheet is added above in current workbook.
            break;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 1970-01-01
    • 2018-06-19
    • 2010-12-31
    • 1970-01-01
    相关资源
    最近更新 更多