将C#中不同数据表中的数百万个数据导出到单个excel文件的最快方法是什么?

时间:2016-09-20 19:29:46

标签: c# .net excel visual-studio-2012

我正在尝试将来自不同数据表的数百万个数据导出到具有多个工作表的单个Excel文件中。我能够为数千条记录实现这一目标,但我的方法对于数百万的数据导出似乎并不高效。

是否有任何替代方法可以实现相同的数百万条记录导出到单个Excel文件? (我检查了很多论坛,但似乎都遵循我在下面代码中所做的相同的方法)

下面是我的代码,用于将数据从不同的数据表导出到单个Excel文件中的多个工作表。

我知道代码有点大但是相信我有很多部分列出我在这里没有提到。我的观点是展示我用来将数据推送到excel文件的技术。

如果有人对此有更好的了解,请告诉我。提前谢谢。

 private void technicalvalidation_DoWork(object sender, DoWorkEventArgs e)
            {
                if (MessageBox.Show("This process will take more than 10 minutes. You cannot use the tool utill processing finishes.\n\nDo you want to continue?", "Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    int countdataset;
                    int loopcount = 1;
                    String Date = DateTime.Now.ToString("MM.dd.yyy_HH.mm.ss");
                    DataSet ds = new DataSet();
                    backgroundWorker.ReportProgress(1);
                    //StoredProcedure Execution for the validation engine
                    string connectionString = ConfigurationManager.ConnectionStrings["VEG_proj_Tool.Properties.Settings.VEG_ToolprojConnectionString"].ConnectionString;
                    string commandText = "proj.ValidationEngine-TechnicalValidation";

                    using (SqlConnection conn = new SqlConnection(connectionString))
                    {
                        SqlCommand cmd = new SqlCommand(commandText, conn);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandTimeout = 600;

                        try
                        {
                            conn.Open();
                            SDA.SelectCommand = cmd;
                            SDA.Fill(ds);
                            ds.Tables[0].TableName = "DataseriesNotinDatavalues";
                            //ds.Tables[1].TableName = "DVDataseriesNotinDataseries";
                            ds.Tables[1].TableName = "DS-IDvalidation";
                            ds.Tables[2].TableName = "DV-IDvalidation";
                            ds.Tables[3].TableName = "CountryCount";
                            ds.Tables[4].TableName = "CountryList";
                            ds.Tables[5].TableName = "StateCount";
                            ds.Tables[6].TableName = "StateList";
                            ds.Tables[7].TableName = "GeoDuplicates";

                            countdataset = ds.Tables.Count;
                        }
                        catch (SqlException ex)
                        {
                            backgroundWorker.ReportProgress(0);
                            MessageBox.Show(ex.Message + "Data processing unsuccessful, please contact technical team.");
                            saveAction(ex.Message);
                        }
                        finally
                        {
                            conn.Close();
                        }


                        if (!Directory.Exists("C:\\proj Tool\\Validation Results proj")) // name of the error file and location
                        {
                            Directory.CreateDirectory("C:\\proj Tool\\Validation Results proj");
                        }

                        Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

                        if (xlApp == null)
                        {
                            backgroundWorker.ReportProgress(0);
                            MessageBox.Show("Excel is not properly installed!!");
                            return;
                        }

                        try
                        {
                            Excel.Workbook xlWorkBook;
                            Excel.Worksheet xlWorkSheet;
                            object misValue = System.Reflection.Missing.Value;
                            xlWorkBook = xlApp.Workbooks.Add(misValue);
                            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

                            xlWorkBook.SaveAs("C:\\proj Tool\\Validation Results proj\\TechnicalValidation" + "_" + Date + ".xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

                            string filePath = @"C:\\proj Tool\\Validation Results proj\\TechnicalValidation" + "_" + Date + ".xls";
                            xlWorkBook = xlApp.Workbooks.Open(filePath, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
                            //It supperesses the Microsoft Compatibility message
                            xlWorkBook.CheckCompatibility = false;
                            //For sheet1------------------------------>
                            Excel.Worksheet excelWorkSheet = xlWorkBook.Sheets.Add();
                            excelWorkSheet.Name = "DataSeries Validation";
    foreach (DataTable table in ds.Tables)
                            {
                                if (loopcount > 3)
                                {
                                    break;
                                }
                                if (table.TableName == "DataseriesNotinDatavalues")
                                {

                                    for (int i = 1; i < table.Columns.Count + 1; i++)
                                    {
                                        excelWorkSheet.Cells[3, i] = table.Columns[i - 1].ColumnName;
                                        Excel.Range formatRange;
                                        formatRange = excelWorkSheet.get_Range("A3");
                                        formatRange.EntireRow.Font.Bold = true;

                                    }
                                }

                                if (table.TableName == "DS-IDvalidation")
                                {
                                    int li = 6;
                                    for (int i = 1; i < table.Columns.Count + 1; i++)
                                    {
                                        excelWorkSheet.Cells[3, li] = table.Columns[i - 1].ColumnName;
                                        Excel.Range formatRange;
                                        formatRange = excelWorkSheet.get_Range("A3");
                                        formatRange.EntireRow.Font.Bold = true;
                                        li = li + 1;
                                    }

                                }

                                if (table.TableName == "DV-IDvalidation")
                                {
                                    int ki = 11;
                                    for (int i = 1; i < table.Columns.Count + 1; i++)
                                    {
                                        excelWorkSheet.Cells[3, ki] = table.Columns[i - 1].ColumnName;
                                        Excel.Range formatRange;
                                        formatRange = excelWorkSheet.get_Range("A3");
                                        formatRange.EntireRow.Font.Bold = true;
                                        ki = ki + 1;
                                    }
                                }


                                if (table.Rows.Count == 0)
                                {
                                    if (table.TableName == "DataseriesNotinDatavalues")
                                    {
                                        excelWorkSheet.Cells[5, 2].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                                        excelWorkSheet.get_Range("B5", "D5").Merge();
                                        excelWorkSheet.Cells[5, 2] = excelWorkSheet.get_Range("B5", "D5");
                                        excelWorkSheet.Cells[5, 2].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                                        excelWorkSheet.Cells[5, 2] = "No mismatch found in Datavalues";
                                    }

                                    if (table.TableName == "DS-IDvalidation")
                                    {
                                        excelWorkSheet.Cells[5, 7].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                                        excelWorkSheet.get_Range("G5", "I5").Merge();
                                        excelWorkSheet.Cells[5, 7] = excelWorkSheet.get_Range("G5", "I5");
                                        excelWorkSheet.Cells[5, 7].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                                        excelWorkSheet.Cells[5, 7] = "No IDmismatch found in Dataseries";
                                    }
                                    if (table.TableName == "DV-IDvalidation")
                                    {
                                        excelWorkSheet.Cells[5, 12].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                                        excelWorkSheet.get_Range("L5", "N5").Merge();
                                        excelWorkSheet.Cells[5, 12] = excelWorkSheet.get_Range("L5", "N5");
                                        excelWorkSheet.Cells[5, 12].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                                        excelWorkSheet.Cells[5, 12] = "No IDmismatch found in Datavalues";
                                    }
                                }
                                if (table.TableName == "DataseriesNotinDatavalues")
                                {
                                    int l = 4;
                                    excelWorkSheet.get_Range("A2", "C2").Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
                                    excelWorkSheet.Cells[2, 1] = "Data Series not in Data Values";

                                    for (int j = 0; j < table.Rows.Count; j++)
                                    {
                                        for (int k = 0; k < table.Columns.Count; k++)
                                        {
                                            excelWorkSheet.Cells[l, k + 1] = table.Rows[j].ItemArray[k].ToString();
                                            excelWorkSheet.Cells[l, k + 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);

                                        }
                                        l = l + 1;
                                    }

                                }


                                if (table.TableName == "DS-IDvalidation")
                                {
                                    int l = 4;
                                    int la = 6;
                                    excelWorkSheet.get_Range("F2", "H2").Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
                                    excelWorkSheet.Cells[2, 6] = "Validate each ID in data series to match its definition in the LU tables";

                                    for (int j = 0; j < table.Rows.Count; j++)
                                    {
                                        for (int k = 0; k < table.Columns.Count; k++)
                                        {
                                            excelWorkSheet.Cells[l, la] = table.Rows[j].ItemArray[k].ToString();
                                            excelWorkSheet.Cells[l, la].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.RosyBrown);
                                            la = la + 1;
                                        }
                                        l = l + 1;
                                        la = 6;

                                    }

                                }

                                if (table.TableName == "DV-IDvalidation")
                                {
                                    int l = 4;
                                    int lb = 11;
                                    excelWorkSheet.get_Range("K2", "M2").Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
                                    excelWorkSheet.Cells[2, 11] = "Validate each ID in data values to match its definition in the LU tables";

                                    for (int j = 0; j < table.Rows.Count; j++)
                                    {
                                        for (int k = 0; k < table.Columns.Count; k++)
                                        {
                                            excelWorkSheet.Cells[l, lb] = table.Rows[j].ItemArray[k].ToString();
                                            excelWorkSheet.Cells[l, lb].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightCyan);
                                            lb = lb + 1;
                                        }
                                        l = l + 1;
                                        lb = 11;
                                    }

                                }

                                loopcount = loopcount + 1;

                            }

                            //For sheet2------------------------------>
                            Excel.Worksheet excelWorkSheet1 = xlWorkBook.Sheets.Add();
                            excelWorkSheet1.Name = "Geo Validation";
                            loopcount = 1;
                            foreach (DataTable table in ds.Tables)
                            {
                                if (loopcount > 3)
                                {
                                    if (loopcount > 8)
                                    {
                                        break;
                                    }


                                    if (table.TableName == "CountryCount")
                                    {

                                        for (int i = 1; i < table.Columns.Count + 1; i++)
                                        {
                                            excelWorkSheet1.Cells[3, i] = table.Columns[i - 1].ColumnName;
                                            Excel.Range formatRange;
                                            formatRange = excelWorkSheet1.get_Range("A3");
                                            formatRange.EntireRow.Font.Bold = true;

                                        }
                                    }

                                    if (table.TableName == "CountryList")
                                    {

                                        for (int i = 1; i < table.Columns.Count + 1; i++)
                                        {
                                            excelWorkSheet1.Cells[4, i] = table.Columns[i - 1].ColumnName;
                                            Excel.Range formatRange;
                                            formatRange = excelWorkSheet1.get_Range("A4");
                                            formatRange.EntireRow.Font.Bold = true;

                                        }
                                    }

                                    if (table.TableName == "StateCount")
                                    {

                                        for (int i = 1; i < table.Columns.Count + 1; i++)
                                        {
                                            excelWorkSheet1.Cells[3, 3] = table.Columns[i - 1].ColumnName;
                                            Excel.Range formatRange;
                                            formatRange = excelWorkSheet1.get_Range("A3");
                                            formatRange.EntireRow.Font.Bold = true;

                                        }
                                    }

                                    if (table.TableName == "StateList")
                                    {
                                        int ji = 3;
                                        for (int i = 1; i < table.Columns.Count + 1; i++)
                                        {
                                            excelWorkSheet1.Cells[4, ji] = table.Columns[i - 1].ColumnName;
                                            Excel.Range formatRange;
                                            formatRange = excelWorkSheet1.get_Range("A4");
                                            formatRange.EntireRow.Font.Bold = true;
                                            ji = ji + 1;
                                        }
                                    }

                                    if (table.TableName == "GeoDuplicates")
                                    {
                                        int jh = 5;
                                        for (int i = 1; i < table.Columns.Count + 1; i++)
                                        {
                                            excelWorkSheet1.Cells[4, jh] = table.Columns[i - 1].ColumnName;
                                            Excel.Range formatRange;
                                            formatRange = excelWorkSheet1.get_Range("A4");
                                            formatRange.EntireRow.Font.Bold = true;
                                            jh = jh + 1;
                                        }
                                    }

                                    if (table.Rows.Count == 0)
                                    {
                                        if (table.TableName == "CountryList")
                                        {
                                            excelWorkSheet1.Cells[6, 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                                            excelWorkSheet1.Cells[6, 1] = "No Countries are availabel";
                                        }
                                        if (table.TableName == "StateList")
                                        {
                                            excelWorkSheet1.Cells[6, 3].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                                            excelWorkSheet1.Cells[6, 3] = "No states are availabel";
                                        }
                                        if (table.TableName == "GeoDuplicates")
                                        {
                                            excelWorkSheet1.Cells[6, 5].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                                            excelWorkSheet1.Cells[6, 5] = "No geoduplicates are availabel";
                                        }

                                    }

                                    if (table.TableName == "CountryList")
                                    {
                                        int l = 5;
                                        excelWorkSheet1.get_Range("A2", "C2").Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
                                        excelWorkSheet1.Cells[2, 1] = "Geograpy Country Validation";

                                        for (int j = 0; j < table.Rows.Count; j++)
                                        {
                                            for (int k = 0; k < table.Columns.Count; k++)
                                            {
                                                excelWorkSheet1.Cells[l, k + 1] = table.Rows[j].ItemArray[k].ToString();
                                                excelWorkSheet1.Cells[l, k + 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);

                                            }
                                            l = l + 1;
                                        }

                                    }

                                    if (table.TableName == "CountryCount")
                                    {
                                        int l = 3;
                                        for (int j = 0; j < table.Rows.Count; j++)
                                        {
                                            for (int k = 0; k < table.Columns.Count; k++)
                                            {
                                                excelWorkSheet1.Cells[l, 2] = table.Rows[j].ItemArray[k].ToString();

                                            }
                                            l = l + 1;
                                        }
                                    }


                                    if (table.TableName == "StateCount")
                                    {
                                        int l = 3;
                                        for (int j = 0; j < table.Rows.Count; j++)
                                        {
                                            for (int k = 0; k < table.Columns.Count; k++)
                                            {
                                                excelWorkSheet1.Cells[l, 4] = table.Rows[j].ItemArray[k].ToString();

                                            }
                                            l = l + 1;
                                        }
                                    }



                                    if (table.TableName == "StateList")
                                    {
                                        int lj = 5;
                                        excelWorkSheet1.get_Range("C2", "E2").Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
                                        excelWorkSheet1.Cells[2, 3] = "Geography State Validation";

                                        for (int j = 0; j < table.Rows.Count; j++)
                                        {
                                            for (int k = 0; k < table.Columns.Count; k++)
                                            {
                                                excelWorkSheet1.Cells[lj, 3] = table.Rows[j].ItemArray[k].ToString();
                                                excelWorkSheet1.Cells[lj, 3].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.RosyBrown);

                                            }
                                            lj = lj + 1;
                                        }
                                    }



                                    if (table.TableName == "GeoDuplicates")
                                    {
                                        int lk = 5;
                                        excelWorkSheet1.get_Range("E2", "H2").Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
                                        excelWorkSheet1.Cells[2, 5] = "Geograpy Dimension Duplicates";

                                        for (int j = 0; j < table.Rows.Count; j++)
                                        {
                                            for (int k = 0; k < table.Columns.Count; k++)
                                            {
                                                excelWorkSheet1.Cells[lk, 5] = table.Rows[j].ItemArray[k].ToString();
                                                excelWorkSheet1.Cells[lk, 5].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Linen);

                                            }
                                            lk = lk + 1;
                                        }

                                    }




                                }
                                loopcount = loopcount + 1;
                            }

                        xlWorkBook.Sheets["Sheet1"].Delete();
                        xlWorkBook.Close(true, misValue, misValue);
                        xlApp.Quit();

                        releaseObject(xlWorkSheet);
                        releaseObject(xlWorkBook);
                        releaseObject(xlApp);
                    }


                    catch (Exception ex)
                    {
                        backgroundWorker.ReportProgress(0);
                        MessageBox.Show(" There was a problem in creating excel file. Please contact the techincal team " + ex.Message);
                        return;
                    }

                }
                backgroundWorker.ReportProgress(2);
            }
        }

1 个答案:

答案 0 :(得分:1)

导出到excel的最快方法我发现将数据放入一个数组然后基本上&#34;粘贴&#34;它变得优秀。这是一个例子:

public void ExportToExcel(DataTable dt)
{
    Excel.Application xlApp = default(Excel.Application);
    Excel.Workbook xlWorkbook = default(Excel.Workbook);
    Excel.Worksheet xlWorkSheet = default(Excel.Worksheet);
    object misValue = Reflection.Missing.Value;
    int i = 0;
    int j = 0;
    int maxRow = dt.Rows.Count;
    int maxColumn = dt.Columns.Count;
    string[,] arr = new string[maxRow + 1, maxColumn + 1];
    string callingAssembly = Reflection.Assembly.GetCallingAssembly().GetName.Name;

    if (dt == null)
        throw new Exception("Passed Data Table is set to nothing");

    while (!(j > maxColumn - 1)) {
        arr(0, j) = dt.Columns(j).ColumnName;
        j += 1;
    }

    while (!(i > maxRow - 1)) {
        j = 0;
        while (!(j > maxColumn - 1)) {
            arr(i + 1, j) = dt.Rows(i).Item(j).ToString;
            j += 1;
        }
        i += 1;
    }

    xlApp = new Excel.Application[];
    xlWorkbook = xlApp.Workbooks.Add(misValue);
    xlWorkSheet = (Excel.Worksheet)xlWorkbook.Sheets("sheet1");

    xlApp.Visible = false;
    xlApp.ScreenUpdating = false;
    xlApp.DisplayAlerts = false;

    xlWorkSheet.Range("A1").Resize(maxRow + 1, maxColumn).Value = arr;
    xlWorkSheet.Cells.Columns.AutoFit();
    xlWorkSheet.Name = callingAssembly;

    xlApp.ScreenUpdating = true;
    xlWorkbook.SaveCopyAs("somefile.xlsx");
    xlApp.Quit();

    ReleaseObject(xlWorkSheet);
    ReleaseObject(xlWorkbook);
    ReleaseObject(xlApp);
}

private void ReleaseObject(object obj)
{
    Runtime.InteropServices.Marshal.ReleaseComObject(obj);
    obj = null;
}

这一行是&#34;粘贴&#34;:

xlWorkSheet.Range("A1").Resize(maxRow + 1, maxColumn).Value = arr;