How to create new sheets on condition in excel file using c#

Neo

Following code works for me to generate only single excel file and single sheet in that.

I want to generate separate sheets for every CustomerOrders how can I do it?

List<MyData> Data = //code to get list of data

DataTable reportDataTable = new DataTable();
reportDataTable.Columns.Add("no");
reportDataTable.Columns.Add("Code");
int count =0

if (Data != null)
{
    foreach (MyData dataobj in Data)
    {
        count++;
        foreach (var innerdata in dataobj.CustomerData.OrderBy(t => t.Number))
        {
            foreach (var orderobj in dataobj.CustomerOrders)
            {
                DataRow row = reportDataTable.NewRow();
                row[0] = "No";
                row[1] = "Code"+count;                            
                reportDataTable.Rows.Add(row);
            }
        }
    }
}

GridView grid = new GridView();
grid.DataSource = reportDataTable;
grid.DataBind();

return new DownloadFileResult(grid, "MYEXCELFILE.xls");

public DownloadFileResult(GridView gv, string FileName)
{
    GridView = gv; // property
    fileName = FileName; //property
}

How do I generate those multiple sheets in one MYEXCELFILE.xls file?

For above i have used following link code 
http://www.codeproject.com/Articles/325103/MVC-Grid-to-Excel-file-download
alex.pulver

Try this source code:

//Create an instance of the object that generates the Excel file
ExcelDocument xls = new ExcelDocument();

//Loop for each CustomerOrders
{
    //Add an worksheet for each CustomerOrders sheet
    ExcelWorksheet xlsSheet = new ExcelWorksheet();
    xlsSheet.setSheetName("CustomerOrders #no");
    xls.easy_addWorksheet(xlsSheet);

    //Create a dataset that contains the gridview datatable
    DataSet dataSet = new DataSet();
    dataSet.Tables.Add((DataTable)gridView.DataSource);

    //Add the datatable
    xlsSheet.easy_insertDataSet(dataSet, "A1", true);
}

//Export Excel file
xls.easy_WriteXLSFile("MYEXCELFILE.xls ");

For formatting the cells see this link: http://www.easyxls.com/manual/FAQ/export-gridview-to-excel.html

This code uses EasyXLS library for generating Excel files.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to append a new row to an Excel file using C# and ClosedXML?

From Dev

Create excel file with two sheets in the same book - C#

From Dev

How to create second sheet in Excel file using C#

From Dev

How to create and write Excel .xls file using C#

From Dev

How to create second sheet in Excel file using C#

From Dev

How to create a blank excel file using C# forms?

From Dev

How to create a new text file using Python

From Dev

How to create a new file / directory using ranger?

From Dev

Create an Excel file with multiple sheets with Python/Pandas

From Dev

How to create a dashboard using excel and .bat file

From Dev

How to create "Text Contains" FormattingConditional (Format Condition) for Excel with C#

From Dev

PHPExcel How to automatically create new excel file in every week routine?

From Dev

How to create a new file?

From Dev

How can I create a method that retrieves the list of spreadsheets in excel file and return back the sheets as a dictionary?

From Dev

How to add new sheets to existing excel workbook using apache POI and PrimeFaces

From Dev

How to create Excel file using OpenXML without creating a local file?

From Dev

How to open an excel file with multiple sheets in pandas?

From Dev

How do I create several different sheets in excel using jxl.jar and a while loop?

From Dev

How do I create several different sheets in excel using jxl.jar and a while loop?

From Dev

how to create CSV file on Linux with different sheets

From Java

How to save a new sheet in an existing excel file, using Pandas?

From Dev

How to save a new sheet in an existing excel file, using Pandas?

From Dev

Excel: how to match two columns from two sheets with condition?

From Dev

How to create file inside a directory using C

From Dev

How to create a .txt file using c#?

From Dev

Create a new file in any desktop using an excel file with a command button to execute that task

From Dev

How to create empty file with no new line in it using batch script?

From Java

How to create a new variable on condition of others in R

From Dev

Create Excel File Using jExcel

Related Related

  1. 1

    How to append a new row to an Excel file using C# and ClosedXML?

  2. 2

    Create excel file with two sheets in the same book - C#

  3. 3

    How to create second sheet in Excel file using C#

  4. 4

    How to create and write Excel .xls file using C#

  5. 5

    How to create second sheet in Excel file using C#

  6. 6

    How to create a blank excel file using C# forms?

  7. 7

    How to create a new text file using Python

  8. 8

    How to create a new file / directory using ranger?

  9. 9

    Create an Excel file with multiple sheets with Python/Pandas

  10. 10

    How to create a dashboard using excel and .bat file

  11. 11

    How to create "Text Contains" FormattingConditional (Format Condition) for Excel with C#

  12. 12

    PHPExcel How to automatically create new excel file in every week routine?

  13. 13

    How to create a new file?

  14. 14

    How can I create a method that retrieves the list of spreadsheets in excel file and return back the sheets as a dictionary?

  15. 15

    How to add new sheets to existing excel workbook using apache POI and PrimeFaces

  16. 16

    How to create Excel file using OpenXML without creating a local file?

  17. 17

    How to open an excel file with multiple sheets in pandas?

  18. 18

    How do I create several different sheets in excel using jxl.jar and a while loop?

  19. 19

    How do I create several different sheets in excel using jxl.jar and a while loop?

  20. 20

    how to create CSV file on Linux with different sheets

  21. 21

    How to save a new sheet in an existing excel file, using Pandas?

  22. 22

    How to save a new sheet in an existing excel file, using Pandas?

  23. 23

    Excel: how to match two columns from two sheets with condition?

  24. 24

    How to create file inside a directory using C

  25. 25

    How to create a .txt file using c#?

  26. 26

    Create a new file in any desktop using an excel file with a command button to execute that task

  27. 27

    How to create empty file with no new line in it using batch script?

  28. 28

    How to create a new variable on condition of others in R

  29. 29

    Create Excel File Using jExcel

HotTag

Archive