C# 逐单元格读取和写入内容时速度太慢

C# 逐单元格读取和写入内容时速度太慢,c#,performance,xls,C#,Performance,Xls,我开发了一个应用程序,可以读取32 xls文件,并将其内容写入一个excel文件。程序运行正常,但速度太慢(32个文件需要20-25分钟),因为我正在逐单元读写。我有什么办法可以加快速度吗。在我的例子中,每个单元格都有不同的格式、字体、边框等。我是否可以使用格式一次读取全部内容并将其粘贴到结果文件中。我使用的是Microsoft.Office.Interop.Excel API。这是我正在使用的代码 Cursor.Current = Cursors.WaitCursor;

我开发了一个应用程序,可以读取32 xls文件,并将其内容写入一个excel文件。程序运行正常,但速度太慢(32个文件需要20-25分钟),因为我正在逐单元读写。我有什么办法可以加快速度吗。在我的例子中,每个单元格都有不同的格式、字体、边框等。我是否可以使用格式一次读取全部内容并将其粘贴到结果文件中。我使用的是Microsoft.Office.Interop.Excel API。这是我正在使用的代码

Cursor.Current = Cursors.WaitCursor;
            // data member initialization for reading the sheet
            Excel.Application app;
            Excel.Workbook workbook;
            Excel.Worksheet worksheet;
            Excel.Range range;

            // data member initialization for writing sheet
            Excel.Application finalApp;
            Excel.Workbook finalWorkBook;
            Excel.Worksheet finalWorkSheet;

            String path = this.textBox1.Text;
            String numberOfFiles = (String)this.comboBox1.SelectedItem;
            int count = 1;
            int row = 1, col = 2;
            int startingrowIndex = 1;
            int endingrowIndex = 1;
            int valueCount = 1;
            string value = textBox2.Text;
            try
            {
                // Object creation for the final sheet
                finalApp = new Excel.ApplicationClass();
                finalApp.Visible = true;
                finalWorkBook = finalApp.Workbooks.Add(1);
                finalWorkSheet = (Excel.Worksheet)finalWorkBook.Sheets[1];


                // opening a excel file
                app = new Excel.ApplicationClass();
                Excel.Borders b=null;
                Excel.Borders fb = null;
                try
                {
                    for (int k = 0; k < Int32.Parse(numberOfFiles); k++)
                    {
                        fullPath = @path + @"\" + count + ".xls";
                        workbook = app.Workbooks.Open(fullPath, Missing.Value, Missing.Value,
                            Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value
                            , Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                            Missing.Value);
                        worksheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);
                        range = worksheet.UsedRange;
                        int cnum1 = range.Columns.Count;
                        int rnum1 = range.Rows.Count;
                        int i, j;
                        for (i = 1; i <= rnum1; i++)
                        {
                            for (j = 1; j <= cnum1; j++)
                            {
                                if ((range.Cells[i, j] as Excel.Range).Value2 != null)
                                {
                                    string value1 = (range.Cells[i, j] as Excel.Range).Value2.ToString();
                                    finalWorkSheet.Cells[row, col] = value1;
                                }
                                b = (Excel.Borders)(range.Cells[i, j] as Excel.Range).Borders;
                                fb = (finalWorkSheet.Cells[row, col] as Excel.Range).Borders;

                                fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Weight = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Weight;
                                fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].Weight = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].Weight;
                                fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Weight = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Weight;
                                fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Weight = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Weight;

                                fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle;
                                fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].LineStyle = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].LineStyle;
                                fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].LineStyle = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].LineStyle;
                                fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].LineStyle = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].LineStyle;
                                (finalWorkSheet.Cells[row, col] as Excel.Range).Interior.Color = (range.Cells[i, j] as Excel.Range).Interior.Color;
                                (finalWorkSheet.Cells[row, col] as Excel.Range).Font.Color = (range.Cells[i, j] as Excel.Range).Font.Color;

                                (finalWorkSheet.Cells[row, col] as Excel.Range).Interior.PatternColor = (range.Cells[i, j] as Excel.Range).Interior.PatternColor;
                                (finalWorkSheet.Cells[row, col] as Excel.Range).Interior.Pattern = (Excel.XlPattern)(range.Cells[i, j] as Excel.Range).Interior.Pattern;
                                (finalWorkSheet.Cells[row, col] as Excel.Range).WrapText =(range.Cells[i, j] as Excel.Range).WrapText;
                                col++;
                            }
                            row++;
                            col = 2;
                        }
                        //finalWorkBook.SaveAs("hello.xlsx", Excel.XlFileFormat.xlExcel4Workbook, Missing.Value,
                        //Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value,
                        //Missing.Value, Missing.Value, Missing.Value);
                        endingrowIndex = row-1;
                        finalWorkSheet.Cells[startingrowIndex,1]=value+valueCount;
                        (finalWorkSheet.Cells[startingrowIndex, 1] as Excel.Range).VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
                        (finalWorkSheet.Cells[startingrowIndex, 1] as Excel.Range).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                        (finalWorkSheet.Cells[startingrowIndex, 1] as Excel.Range).WrapText = true;
                        finalWorkSheet.get_Range(finalWorkSheet.Cells[startingrowIndex, 1], finalWorkSheet.Cells[endingrowIndex, 1]).Merge(Type.Missing);
                        startingrowIndex = row + 1;
                        workbook.Close(false, false, Missing.Value);
                        count++;
                        row++;
                        col = 2;
                        valueCount++;
                    }
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show("Successfully Completed");
                }
                catch (FileNotFoundException fnfe)
                {
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show("Error while opening the file "+fullPath);
                }
                //finalWorkBook.Close(true,false,Missing.Value);
            }
            catch (Exception ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(@"Some Error has occurred.Please check the path Correctly
                whether it's correct or try again");
            }
        }
Cursor.Current=Cursors.WaitCursor;
//用于读取工作表的数据成员初始化
Excel应用程序;
Excel.工作簿;
Excel.工作表;
范围;
//写入工作表的数据成员初始化
Excel.applicationfinalapp;
Excel。工作簿最终工作手册;
Excel。工作表最终工作表;
字符串路径=this.textBox1.Text;
String numberOfFiles=(String)this.comboBox1.SelectedItem;
整数计数=1;
int行=1,列=2;
int startingrowIndex=1;
int-endingrowIndex=1;
int valueCount=1;
字符串值=textBox2.Text;
尝试
{
//为最终图纸创建对象
finalApp=new Excel.ApplicationClass();
finalApp.Visible=true;
finalWorkBook=finalApp.Workbooks.Add(1);
finalWorkSheet=(Excel.Worksheet)finalWorkBook.Sheets[1];
//打开excel文件
app=新建Excel.ApplicationClass();
Excel.Borders b=null;
Excel.Borders fb=null;
尝试
{
for(intk=0;k对于(i=1;i使用range.Copy将加载的xls的一个范围复制到剪贴簿。然后使用将剪贴簿内容粘贴到新的xls。在那里,您可以在新的工作表中说明复制到哪个范围的内容。

如何编写一些代码,以便在整个程序的不同时间将其输出到日志中,并找出代码中的哪些部分使用l需要多长时间执行?一旦你弄明白了,你应该试着找出原因,如果你不能,在这里发布一个新问题。我已经这样做了,这需要时间,因为excel文件中的数据太多,我的程序复制每个单元格的内容并粘贴到结果文件中。然后读取同一单元格的格式并在生成的文件单元格中应用相同的内容。每个单元格都有不同的格式。这就是为什么速度很慢。为什么不能从某个位置开始将所有使用过的行写入新文件?@wonko79我可以这样做,但单元格的格式如何?每个单元格的格式都不同,然后我必须到每个单元格复制粘贴表单格式化。我不知道我是否可以同时复制多个单元格的格式。如果可以,请告诉我怎么做。@wonko……。我先做了……但是在这种情况下,如果我在程序运行期间复制并粘贴了一些内容……。然后程序终止……。因为使用了剪贴板区域,即u被我和我的程序所拒绝。。。