无法将Excel工作表从工作簿A复制到WorkBook B?

时间:2016-10-07 10:00:19

标签: c# .net excel winforms interop

我正在尝试复制" Config"使用以下代码从Excel文件A到Excel文件B的工作表,但是我收到以下错误:

未处理的类型' System.Runtime.InteropServices.COMException'发生了 附加信息:来自HRESULT的异常:0x800A03EC

 Microsoft.Office.Interop.Excel.Application xlApp = null;
                Microsoft.Office.Interop.Excel.Workbook xlWorkBook = null;
                Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet1;

            string reportname = ServerName;
            string Destfilepath = string.Empty;
            Destfilepath = Application.StartupPath + "\\StandardReports\\" + reportname + ConfigurationManager.AppSettings["ExcelType"].ToString();


            object misValue = System.Reflection.Missing.Value;

    xlApp = new Microsoft.Office.Interop.Excel.Application();
            xlApp.DisplayAlerts = false;
            xlWorkBook = xlApp.Workbooks.Open(Destfilepath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);



            Excel.Worksheet wrkD = ((Excel.Worksheet)xlWorkBook.Worksheets["Config"]);            

            Excel.Application excelApp = new Excel.Application();
            excelApp.DisplayAlerts = false;
            // Create a new Excel Workbook
            string SourcePath = Application.StartupPath + @"\Templet\StdReport" + ConfigurationManager.AppSettings["ExcelType"].ToString();
            //string outputPath = System.Windows.Forms.Application.StartupPath + @"\Receipt.sdc";
            Excel.Workbook SourceexcelWorkbook = excelApp.Workbooks.Open(SourcePath,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing);


            Excel.Worksheet wksh1S = ((Excel.Worksheet)excelApp.ActiveWorkbook.Worksheets[1]);
            Excel.Worksheet wksh2D = ((Excel.Worksheet)xlApp.ActiveWorkbook.Worksheets[2]);
            wksh1S.Copy(wksh2D);// I am getting error here

注意:我的动机是复制"配置" Excel文件A中的工作表并将其添加到Excel文件B. B已包含"配置"文件,我想覆盖这个表格或删除并添加新的表格。

0 个答案:

没有答案