I make the windows form on which on a single button click performs like this
'

private void button1_Click(object sender, EventArgs e)
        {
Excel.Application exapp=null;
            Excel.Workbook exbook=null;
            Excel.Worksheet exsheet = null;
            object misvalue = System.Reflection.Missing.Value;
            exapp = new Excel.Application();
            exapp.SheetsInNewWorkbook = 1;
            exbook = exapp.Workbooks.Add(misvalue);

            exsheet = (Excel.Worksheet)exbook.Worksheets.get_Item(1);

            exsheet.Cells[1, 1] = "First Name";
            exsheet.Cells[1, 2] = "Last Name";
            exsheet.Cells[1, 3] = "Address1";
            exsheet.Cells[1, 4] = "Address2";
            exsheet.Cells[1, 5] = "City";
            exsheet.Cells[1, 6] = "PinCode";
            exsheet.Cells[1, 7] = "Phone Number";
            exsheet.Cells[2, 1] = textBox1.Text.ToString();
            exsheet.Cells[2, 2] = textBox2.Text.ToString();
            exsheet.Cells[2, 3] = textBox3.Text.ToString();
            exsheet.Cells[2, 4] = textBox4.Text.ToString();
            exsheet.Cells[2, 5] = textBox5.Text.ToString();
            exsheet.Cells[2, 6] = textBox6.Text.ToString();
            exsheet.Cells[2, 7] = textBox7.Text.ToString();
            exbook.SaveAs("rep.xls", Excel.XlFileFormat.xlWorkbookNormal, misvalue, misvalue, misvalue, misvalue, Excel.XlSaveAsAccessMode.xlExclusive, misvalue, misvalue, misvalue, misvalue, misvalue);
            exapp.Quit();
            releaseObject(exapp);
            releaseObject(exbook);
            releaseObject(exsheet);
            }'

but this thing imports data from windows in only one sheet
but i want to make a new sheet one each button click in excel
can anybody help me in it??????

Thanks in advance

i want to make a new sheet one each button click in excel

Can you clarify this a little please?

is the button on an spread sheet or on the C# WinForm?

Do you mean to write data in different worksheets?

How about this?

            Workbook workbook = new Workbook();
            //sheet 1
            Worksheet sheet = workbook.Worksheets[0];
            sheet.Range["A1"].Text="First name";
            //sheet 2
            Worksheet sheet2 = workbook.Worksheets[1];
            sheet2.Range["A1"].Text="Last Name";

I use .NET Excel library

If you still haven't figured it out let us know, I wrote some code awhile back that did a lot of work of reading and writing to an excel file and would be more then glad to share it if you need (I'll have to dig it up)

I used C# Excel Writer in my project to work with excel. You can use this example below to accomplish your task:

ExcelWorkbook wb = new ExcelWorkbook();
wb.Worksheets.Add("sh1");
wb.Worksheets[0].Cells["A1"].Value = "text";
wb.Worksheets.Add("sh2");
wb.Worksheets[1].Cells["A1"].Value = "text";
wb.Worksheets.Add("sh3");
wb.Worksheets[2].Cells["A1"].Value = "text";
wb.WriteXLSX(@"c:\output.xlsx");

Or, if you want to add data into existing excel file:

ExcelWorkbook Wbook = ExcelWorkbook.ReadXLSX(@"c:\test.xlsx");
Wbook.Worksheets.Add("sh1");
Wbook.Worksheets[Wbook.Worksheets.Count - 1].Cells["A1"].Value = "text";
Wbook.WriteXLSX(@"c:\test.xlsx");

I used C# Excel Writer in my project to work with excel. You can use this example below to accomplish your task:

ExcelWorkbook wb = new ExcelWorkbook();
wb.Worksheets.Add("sh1");
wb.Worksheets[0].Cells["A1"].Value = "text";
wb.Worksheets.Add("sh2");
wb.Worksheets[1].Cells["A1"].Value = "text";
wb.Worksheets.Add("sh3");
wb.Worksheets[2].Cells["A1"].Value = "text";
wb.WriteXLSX(@"c:\output.xlsx");
Or, if you want to add data into existing excel file:

ExcelWorkbook Wbook = ExcelWorkbook.ReadXLSX(@"c:\test.xlsx");
Wbook.Worksheets.Add("sh1");
Wbook.Worksheets[Wbook.Worksheets.Count - 1].Cells["A1"].Value = "text";
Wbook.WriteXLSX(@"c:\test.xlsx");

Edited 3 Years Ago by dsfgsdf

You can select excel sheets by index numbers.

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(3);

and write content like this

xlWorkSheet.Cells[1, 1] = "Sheet 1 content";

Full source.. Write data to multiple worksheet

Kerry

This article has been dead for over six months. Start a new discussion instead.