0

I need assistance with a function that exports multiple CSV sheets in a directory. I'm sorry but my code is too long, I will try to add it to help me with it.

static private int rowsPerSheet = 100000;
static private DataTable ResultsData = new DataTable();

private static void ExportToCSV(bool firstTime)
{
    const string fileName = @"C:\Export\MyExcel.csv";

    //Delete the file if it exists. 
    if (firstTime && File.Exists(fileName))
    {
        File.Delete(fileName);
    }

    uint sheetId = 1;
    if (firstTime)
    {
        SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
            Create(fileName, SpreadsheetDocumentType.Workbook);
        WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
        workbookpart.Workbook = new Workbook();
        var worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
        var sheetData = new SheetData();
        worksheetPart.Worksheet = new Worksheet(sheetData);
        var bold1 = new Bold();
        CellFormat cf = new CellFormat();
        Sheets sheets;
        sheets = spreadsheetDocument.WorkbookPart.Workbook.
            AppendChild<Sheets>(new Sheets());
        var sheet = new Sheet()
        {
            Id = spreadsheetDocument.WorkbookPart.
                GetIdOfPart(worksheetPart),
            SheetId = sheetId,
            Name = "Sheet" + sheetId
        };
        sheets.Append(sheet);
        var headerRow = new Row();
        foreach (DataColumn column in ResultsData.Columns)
        {
            var cell = new Cell
            {
                DataType = CellValues.String,
                CellValue = new CellValue(column.ColumnName)
            };
            headerRow.AppendChild(cell);
        }
        sheetData.AppendChild(headerRow);

        foreach (DataRow row in ResultsData.Rows)
        {
            var newRow = new Row();
            foreach (DataColumn col in ResultsData.Columns)
            {
                var cell = new Cell
                {
                    DataType = CellValues.String,
                    CellValue = new CellValue(row[col].ToString())
                };
                newRow.AppendChild(cell);
            }
            sheetData.AppendChild(newRow);
        }
        workbookpart.Workbook.Save();
        spreadsheetDocument.Close();
    }
    else
    {
        var spreadsheetDocument = SpreadsheetDocument.Open(fileName, true);
        var workbookpart = spreadsheetDocument.WorkbookPart;
        if (workbookpart.Workbook == null)
            workbookpart.Workbook = new Workbook();
        var worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
        var sheetData = new SheetData();
        worksheetPart.Worksheet = new Worksheet(sheetData);
        var sheets = spreadsheetDocument.WorkbookPart.Workbook.Sheets;

        if (sheets.Elements<Sheet>().Any())
        {
            //Set the new sheet id
            sheetId = sheets.Elements<Sheet>().Max(s => s.SheetId.Value) + 1;
        }
        else
        {
            sheetId = 1;
        }
        var sheet = new Sheet()
        {
            Id = spreadsheetDocument.WorkbookPart.
            GetIdOfPart(worksheetPart),
            SheetId = sheetId,
            Name = "Sheet" + sheetId
        };
        sheets.Append(sheet);
        var headerRow = new Row();

        foreach (DataColumn column in ResultsData.Columns)
        {
            var cell = new Cell
            {
                DataType = CellValues.String,
                CellValue = new CellValue(column.ColumnName)
            };
            headerRow.AppendChild(cell);
        }
        sheetData.AppendChild(headerRow);
        foreach (DataRow row in ResultsData.Rows)
        {
            var newRow = new Row();
            foreach (DataColumn col in ResultsData.Columns)
            {
                var cell = new Cell
                {
                    DataType = CellValues.String,
                    CellValue = new CellValue(row[col].ToString())
                };
                newRow.AppendChild(cell);
            }
            sheetData.AppendChild(newRow);
        }
        workbookpart.Workbook.Save();
        spreadsheetDocument.Close();
    }
}

private void Button5_Click(object sender, EventArgs e)
{   
    my1();
    my2();
    string queryString = "select * from " + comboBox1.Text + "";
    using (var connection =
           new SqlConnection(@"Data Source =***"))
    {
        var command = new SqlCommand(queryString, connection);
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        int c = 0;
        bool firstTime = true;
    DataTable dtSchema = reader.GetSchemaTable();
        var listCols = new List<DataColumn>();
        if (dtSchema != null)
        {
            foreach (DataRow drow in dtSchema.Rows)
            {
                string columnName = Convert.ToString(drow["ColumnName"]);
                var column = new DataColumn(columnName, (Type)(drow["DataType"]));
                column.Unique = (bool)drow["IsUnique"];
                column.AllowDBNull = (bool)drow["AllowDBNull"];
                column.AutoIncrement = (bool)drow["IsAutoIncrement"];
                listCols.Add(column);
                ResultsData.Columns.Add(column);
            }
        }
        while (reader.Read())
        {
            DataRow dataRow = ResultsData.NewRow();
            for (int i = 0; i < listCols.Count; i++)
            {
                dataRow[(listCols[i])] = reader[i];
            }
            ResultsData.Rows.Add(dataRow);
            c++;
            if (c == rowsPerSheet)
            {
                c = 0;
                ExportToCSV(firstTime);
                ResultsData.Clear();
                firstTime = false;
            }
        }
        if (ResultsData.Rows.Count > 0)
        {
            ExportToCSV(firstTime);
            ResultsData.Clear();
        }
        reader.Close();
    }
 MessageBox.Show("Successfully Completed");
}

 private void my1()
    {
        var path = "C:\\";
        var folder = Path.Combine(path, "Export");
        Directory.CreateDirectory(folder);
    }

 private void my2()
    {
        try
        {
            var app = new Microsoft.Office.Interop.Excel.Application();
            var wb = app.Workbooks.Add();
            wb.SaveAs(@"C:\Export\MyExcel.csv");
            wb.Close();
        }
        catch (Exception )
        {
            Console.WriteLine("' You Canseld The Export'");
        }
    }

Really, it takes from me along time to write this code to export the excel sheet and spread it like I want of rows, so it work good with a small table but when I try to export a large table, it takes along of time and it didn't finish.

I have like 10.000.000 rows with 20 columns and more than that in another table, so I try to export it to CSV every sheet have 100.000 row only.

Any help or any other ideas are welcomed.

Ahmed Alkhteeb
  • 383
  • 2
  • 11
  • have you tried simply opening up the xml file using excel? – jazb Aug 01 '19 at 07:53
  • When you just want to write some data to *.csv, *.xml or *.xlsx in a fast way, take a look at this: https://github.com/danielgindi/SpreadsheetStreams.net Look at the sample in the repo to see how easy it is to switch from one format to another. – user743414 Aug 01 '19 at 07:57
  • CSV is a text format. It has no sheets nor does it need Excel. This code doesn't seem to have anything to do with the question - how to convert some unspecified XML file into a CSV – Panagiotis Kanavos Aug 01 '19 at 07:58
  • @PanagiotisKanavos i want to change it to CSV but with the same way it work can i do that ?? – Ahmed Alkhteeb Aug 01 '19 at 08:03
  • To write data to a CSV file you could join the values of each row with `String.Join` and write them to a file with `StreamWriter.WriteLine`. It's just 5 lines. Where does the XML data come from? The code doesn't show anything about XML, it just reads results from a query – Panagiotis Kanavos Aug 01 '19 at 08:04
  • @user743414 i will look at it now thx :) – Ahmed Alkhteeb Aug 01 '19 at 08:04
  • @AhmedAlkhteeb change *what* to CSV? Where is the data? Where do you read them from? You probably don't need to do anything, certainly not use a library that creates Excel files – Panagiotis Kanavos Aug 01 '19 at 08:04
  • @PanagiotisKanavos you can find it in 'Button5_Click' – Ahmed Alkhteeb Aug 01 '19 at 08:05
  • @AhmedAlkhteeb even the data loading code is overcomplicated. You can load a DataTable from an IDataReader directly, you don't need to load the schema or add lines one by one. That code doesn't show any XML files either. – Panagiotis Kanavos Aug 01 '19 at 08:06
  • @PanagiotisKanavos is it ` Create(fileName, SpreadsheetDocumentType.Workbook);` that what are you looking for or till me the better way to do that – Ahmed Alkhteeb Aug 01 '19 at 08:11
  • @AhmedAlkhteeb CSV is a text format. Excel files aren't CSVs. The `xlsx` format is a zip package containing XML files. In any case, you *can't* put more than 1M items in a single Excel sheet. What do you really want? A CSV file or an xlsx file? – Panagiotis Kanavos Aug 01 '19 at 08:29
  • @PanagiotisKanavos i have 10.000.000 record xlsx is too slow that i need to export it by CSV but i want to control the number of row in every sheet like i did – Ahmed Alkhteeb Aug 01 '19 at 08:32
  • @PanagiotisKanavos i try to do that sorry for wasting your time , just look to the end of the code now did it help ??? – Ahmed Alkhteeb Aug 01 '19 at 08:48

0 Answers0