Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I made a method about Export excel file from DataSet.

I have a 5 Datables in DataSet and each name is below

tables[0] - Web Application, tables[1] - Content Database, tables[2] - Site Collection,

tables[3] - Webs, tables[4] - Lists

And after the export, the result is like this

[Click]

It works well, but I want to do like this.- reverse worksheet

Web Application, Content Database, Site Collection, Webs, Lists

My code is below, Please tell me how can I change the code.

What I have tried:

C#
private bool ExportToExcel(DataSet ds, string fileName)
   {
       string saveFileName = "";
       string worksheetName = string.Empty;

       SaveFileDialog saveDialog1 = new SaveFileDialog();
       this.Invoke(new MethodInvoker(delegate()
       {
           saveDialog1.DefaultExt = "xlsx";
           saveDialog1.Filter = "Excel file|*.xlsx";
           saveDialog1.FileName = fileName;
           saveDialog1.ShowDialog();
       }));
       saveFileName = saveDialog1.FileName;

       if (saveFileName.IndexOf(":") < 0)
           return false;

       Excel.Application xlApp = new Excel.Application();

       if (xlApp == null)
       {
           MessageBox.Show("Can`t create Excel");
           return false;
       }

       Excel.Workbooks workBooks = xlApp.Workbooks;
       Excel.Workbook workBook = xlApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
       Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets[1];

       try
       {
           if (ds.Tables.Count > 0)
           {
               //total Tables Count in Dataset
               for (int index = 0; index < ds.Tables.Count; index++)
               {
                   //Count the Tables Column
                   for (int i = 0; i < ds.Tables[index].Columns.Count; i++)
                   {
                       //Write Column Name to worksheet
                       workSheet.Cells[1, i + 1] = ds.Tables[index].Columns[i].ColumnName;
                   }
                   //Rows Total Count
                   for (int r = 0; r < ds.Tables[index].Rows.Count; r++)
                   {
                       for (int i = 0; i < ds.Tables[index].Columns.Count; i++)
                       {
                           //Write data to each rows
                           workSheet.Cells[r + 2, i + 1] = ds.Tables[index].Rows[r][i].ToString();
                       }
                       Application.DoEvents();
                   }
                   //Column Name - Bold
                   ((Excel.Range)workSheet.Rows[1, Type.Missing]).Font.Bold = true;

                   //Auto Fit the column
                   workSheet.Columns.EntireColumn.AutoFit();

                   //set WorkSheet Name
                   if (index == 0)
                   {
                       workSheet.Name = "Web Application";
                   }
                   else if (index == 1)
                   {
                       workSheet.Name = "Content Database";
                   }
                   else if (index == 2)
                   {
                       workSheet.Name = "Site Collection";
                   }
                   else if (index == 3)
                   {
                       workSheet.Name = "Webs";
                   }
                   else if (index == 4)
                   {
                       workSheet.Name = "Lists";
                   }
                   else
                   {
                       workSheet.Name = ds.Tables[index].TableName;
                   }

                   if (index < ds.Tables.Count - 1)
                   {
                       workSheet = (Excel.Worksheet)workBook.Worksheets.Add();
                   }

               }
           }
           else
           {
               MessageBox.Show("No tables returned.");
           }

       }

       catch (Exception ex)
       {
           logManager.WriteLog(ex.ToString());
       }

       if (saveFileName != null)
       {
           try
           {


               workBook.Saved = true;
               workBook.SaveCopyAs(saveFileName);
           }
           catch (Exception ex)
           {
               MessageBox.Show("Error, File is already Opened!\n" + ex.Message);
           }
       }
       xlApp.Quit();
       GC.Collect();

       this.Invoke(new MethodInvoker(delegate()
       {
           pgstatusbar.MarqueeAnimationSpeed = 0;
           pgstatusbar.Visible = false;
       }));
       MessageBox.Show("Excel file saved!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);

       return true;
   }
Posted
Comments
Dave Kreskowiak 18-Dec-16 21:36pm    
This may seem stupid, but have to tried exporting the tables in reverse order? Reverse the loop, like from 4 to 0.
RydenChoi 18-Dec-16 21:44pm    
Okay, I`m stupid, So why don`t you show me your beautiful code about that?
Dave Kreskowiak 18-Dec-16 22:01pm    
You have a loop going through the tables in the DataSet:
//total Tables Count in Dataset
    for (int index = 0; index < ds.Tables.Count; index++)
    {

Reverse the loop:
//total Tables Count in Dataset
    for (int index = ds.Tables.Count - 1; index >= 0; index--)
    {
RydenChoi 18-Dec-16 23:47pm    
Yeah. I changed it and also modified other codes, finally works well.
It could be a easy thing, but I`m a just newbie about this......
And I realized that I need a lot of practice.. Thank you your advice Dave Kreskowiak.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900