特殊范围粘贴失败

时间:2019-07-08 18:29:45

标签: c# excel-interop

当我在Windows 10 OS中运行c#代码并安装了32位Microsoft Access数据库引擎2010,Visual Studio 2017和Platform X86时,它有时可以工作。其他时候,当我尝试从temp.xlsx复制数据并在发生此错误时将其粘贴到另一个Excel工作表中:

  

粘贴范围类的特殊方法失败

我尝试在Windows 10,已安装Microsoft Access数据库引擎2010-32位,Visual Studio 2017和Platform X86中运行代码。

        object missing = Type.Missing;
        string ExcelFilePath = "";
         switch (scenario)
        {

            case "MultiStop":
                ExcelFilePath = @"C:\NA\NEAT\Output\Templates\MultiStopOutputTemplate.xlsx";

                break;
            default:
                Console.Write("scenario is not included");
                Console.ReadLine();
                Environment.Exit(1);
                break;

        }

        if (Tbl == null || Tbl.Columns.Count == 0)
            throw new Exception("ExportToExcel: Null or empty input table!\n");

        //Create an Excel Worksheet without creating any file
        Excel.Application sourceApp = new Excel.Application();
        Excel.Application OrigExcelApp = new Excel.Application();

        Excel.Application destExcel = new Excel.Application();

        try
        {
            OrigExcelApp.DisplayAlerts = false;
            Excel.Workbook OrigExcelWB = OrigExcelApp.Workbooks.Add(Type.Missing);           
            Excel.Worksheet OrigWorkSheet = (Excel.Worksheet)OrigExcelWB.ActiveSheet;
            OrigWorkSheet.Name = "temp";

            for (int i = 0; i < Tbl.Columns.Count; i++)
            {
                Excel.Range o1 = (Excel.Range)OrigWorkSheet.Cells[1, (i + 1)];
                o1.EntireColumn.NumberFormat = "@";
                OrigWorkSheet.Cells[1, (i + 1)] = Tbl.Columns[i].ColumnName;
            }




            int numBatchPaste = 1;
            int maxLimit = 5000;
            if ((Tbl.Rows.Count % maxLimit) != 0)
                numBatchPaste = (Tbl.Rows.Count) / maxLimit + 1;
            else
                numBatchPaste = (Tbl.Rows.Count) / maxLimit;
            int rowCtr = 0;
            int topRow = 2;
            for (int a = 0; a < numBatchPaste; a++)
            {
                int arrsize = maxLimit;
                if (Tbl.Rows.Count < maxLimit)
                    arrsize = Tbl.Rows.Count;
                else if (a == numBatchPaste - 1)
                    arrsize = Tbl.Rows.Count - (numBatchPaste - 1) * maxLimit;
                else
                    arrsize = maxLimit;
                object[,] arr = new object[Tbl.Rows.Count, Tbl.Columns.Count];
                int p1 = 0;

                for (int t = rowCtr; t < Tbl.Rows.Count; t++)
                {
                    DataRow dr = Tbl.Rows[t];
                    for (int q = 0; q < Tbl.Columns.Count; q++)
                    {
                        arr[p1, q] = dr[Tbl.Columns[q].ColumnName].ToString();
                    }
                    p1++;
                    rowCtr++;
                    if (p1 == arrsize)
                        break;
                }
                Excel.Range d1 = (Excel.Range)OrigWorkSheet.Cells[topRow, 1];
                Excel.Range d2 = (Excel.Range)OrigWorkSheet.Cells[topRow + arrsize - 1, Tbl.Columns.Count];
                Excel.Range range1 = OrigWorkSheet.get_Range(d1, d2);
                range1.Value = arr;
                topRow = topRow + arrsize;
            }
        OrigExcelWB.SaveAs(@"C:\NA\NEAT\Output\Templates\temp.xlsx");
        GC.Collect();            
        OrigExcelWB.Close();
        TerminateExcelProcess(OrigExcelApp);
        moduleTime = "Storing array for Excel file ends" + DateTime.Now.ToString("HH:mm:ss tt");
        System.IO.File.AppendAllText(@"C:\NA\ModuleTime.txt", moduleTime + Environment.NewLine);

        sourceApp.DisplayAlerts = false;
        Excel.Workbook sourceWB = sourceApp.Workbooks.Open(@"C:\NA\NEAT\Output\Templates\temp.xlsx", 0, false, 1, "", "", false, Excel.XlPlatform.xlWindows, 9, true, false, 0, true, false, false);
        Excel.Worksheet WorksheetSource = (Excel.Worksheet)sourceWB.Sheets["temp"];
        destExcel.DisplayAlerts = false;
        Excel.Workbook destWb = destExcel.Workbooks.Open(ExcelFilePath, 0, false, 1, "", "", false, Excel.XlPlatform.xlWindows, 9, true, false, 0, true, false, false);
        Excel.Worksheet WorksheetDest = (Microsoft.Office.Interop.Excel.Worksheet)destWb.Sheets[sheetName];
        WorksheetDest.UsedRange.Clear();
        WorksheetSource.UsedRange.Copy(Type.Missing);
        Excel.Range d3 = (Excel.Range)WorksheetDest.Cells[1, 1];
        Excel.Range d4 = (Excel.Range)WorksheetDest.Cells[Tbl.Rows.Count, Tbl.Columns.Count];
        Excel.Range range2 = WorksheetDest.get_Range(d3, d4);
        range2.PasteSpecial(Excel.XlPasteType.xlPasteValues, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, Type.Missing, Type.Missing);
        destWb.Save();
        destWb.Close();
        sourceWB.Close();          
        TerminateExcelProcess(sourceApp);           
        TerminateExcelProcess(destExcel);
        File.Delete(@"C:\NA\NEAT\Output\Templates\temp.xlsx");
        }
        catch (Exception e)
        {

        }

它应始终运行且没有任何错误,并打印出excel文件。

0 个答案:

没有答案