0

32 個の xls ファイルを読み取り、その内容を 1 つの 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");
            }
        }

任意のヘルプをいただければ幸いです。

4

1 に答える 1

0

range.copy を使用して、読み込まれた xls の範囲をクリップボードにコピーします。次に、 range.PasteSpecialを使用して、クリップボードの内容を新しい xls に貼り付けます。そこでは、新しいシートのどの範囲にコピーされるかを指定できます。

于 2013-04-17T06:11:14.607 に答える