-1

I'm trying to export millions of data from different data table to single excel file with multiple sheets. I was able to achieve this for thousands of records but my approach seems not efficient for the millions of data export.

Is there any alternate way to achieve the same for millions of records export to single excel file ? (I checked so many forums but all seem to follow the same approach of what I did in the below code)

Below is my code for exporting data from different data table to multiple sheets in a single excel file.

I know the code is little big but trust me it has so many parts listed which i didnt mention here. My point is to show the technique i used to push data into excel file.

Let me know if anyone have some better idea on this one. Thanks in advance.

 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);
            }
        }
Gowtham Ramamoorthy
  • 896
  • 4
  • 15
  • 36
  • 6
    Exporting millions of rows in one excel file, sound like a bad idea – Jeroen van Langen Sep 20 '16 at 19:31
  • what about 2 lakh records ? does it works for this number ? – Gowtham Ramamoorthy Sep 20 '16 at 19:33
  • 2
    You could export them to CSV files. – Jeroen van Langen Sep 20 '16 at 19:33
  • 1
    I'd recommend saving a plain, Tab-separated text file instead that you can import into Excel (maybe one file per sheet). It's much faster than trying to automate Excel. http://stackoverflow.com/a/12807447/682404 – xxbbcc Sep 20 '16 at 19:33
  • 5
    @JeroenvanLangen it does. [Excel 2010 supports 1,048,576 rows per worksheet](https://support.office.com/en-us/article/Excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3). If you do have worksheets with that volume of data, though, you're clearly doing something wrong. – Geeky Guy Sep 20 '16 at 19:34
  • I assume you identified the bottleneck? I would stay away from Excel interop for working with that much data. You can export to simple text files (CSV) or you could work directly with OpenXML to create your files that way (although that can be a nightmare - but there are libraries that make it less painful). Or, as others have said, *rethink what you are doing*, because that much data in an Excel file is going to be a real pain to work with. – Matt Burland Sep 20 '16 at 19:35
  • Thanks for your input guys . I appreciate it :) – Gowtham Ramamoorthy Sep 20 '16 at 19:40

1 Answers1

1

Fastest way to export to excel I've found is to put your data into an array then essentially "pasting" it into excel. Here is an example:

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;
}

This line being the "paste":

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