Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create excel file with multiple sheets from DataSet using C#

How to create excel file with multiple sheets from DataSet using C#?

I have successfully created an excel file with single sheet. But I am not able to do that for multiple sheets.

like image 673
HarshJain Avatar asked Nov 16 '11 18:11

HarshJain


People also ask

How do I automatically create multiple sheets in Excel?

Start Excel. A new, blank workbook appears. Click the New sheet button at the bottom of the screen. Press and hold the CTRL key, and then click Sheet1, Sheet2, and so on till you finish selecting all your worksheets.

Can I create multiple Excel sheets at once?

Insert multiple worksheets at the same time For example, if you want to add three new worksheets, select three sheet tabs of existing worksheets. On the Home tab, in the Cells group, click Insert, and then click Insert Sheet. Tip: You can also right-click the selected sheet tabs, and then click Insert.

How do you split data into multiple worksheets?

please pick the range that you want to do so. Using Home > Worksheet > Split Data to apply the utility. Please choose Fixed rows and enter the desired number of rows in the box provided in the Split Date into Multiple Worksheets dialogue box.

Can CSV save multiple sheets?

You can't have multiple sheets in CSV, because CSV doesn't have sheets. Ask your supervisor to point to the bit in ietf.org/rfc/rfc4180.txt that shows how sheets are specified. They won't be able to, because it's not there.


1 Answers

Here is a simple C# class that programatically creates an Excel WorkBook and adds two sheets to it, and then populates both sheets. Finally, it saves the WorkBook to a file in the application root directory so that you can inspect the results...

public class Tyburn1
{
    object missing = Type.Missing;
    public Tyburn1()
    {
        Excel.Application oXL = new Excel.Application();
        oXL.Visible = false;
        Excel.Workbook oWB = oXL.Workbooks.Add(missing);
        Excel.Worksheet oSheet = oWB.ActiveSheet as Excel.Worksheet;
        oSheet.Name = "The first sheet";
        oSheet.Cells[1, 1] = "Something";
        Excel.Worksheet oSheet2 = oWB.Sheets.Add(missing, missing, 1, missing) 
                        as Excel.Worksheet;
        oSheet2.Name = "The second sheet";
        oSheet2.Cells[1, 1] = "Something completely different";
        string fileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)        
                                + "\\SoSample.xlsx";
        oWB.SaveAs(fileName, Excel.XlFileFormat.xlOpenXMLWorkbook,
            missing, missing, missing, missing,
            Excel.XlSaveAsAccessMode.xlNoChange,
            missing, missing, missing, missing, missing);
        oWB.Close(missing, missing, missing);
        oXL.UserControl = true;
        oXL.Quit();
    }
}

To do this, you would need to add a reference to Microsoft.Office.Interop.Excel to your project (you may have done this already since you are creating one sheet).

The statement that adds the second sheet is...

Excel.Worksheet oSheet2 = oWB.Sheets.Add(missing, missing, 1, missing) 
                            as Excel.Worksheet;

the '1' argument specifies a single sheet, and it can be more if you want to add several sheets at once.

Final note: the statement oXL.Visible = false; tells Excel to start in silent mode.

like image 53
Gayot Fow Avatar answered Nov 08 '22 22:11

Gayot Fow