来自HRESULT的异常:复制excel表时为0x800A03EC

时间:2015-07-31 05:18:27

标签: c# excel vsto

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

0

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

1

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

1 个答案:

答案 0 :(得分:0)

我得到了解决方案

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