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

Recommended Answers

All 11 Replies

Is anyone there to help me

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");

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

Hi! You can use ZetExcel.com. It is very helpful.

commented: What happens when your homework is late? -3

The easiest thing is to write CSV files, which follow 4 simple rules: 1) cells are separated by a comma, 2) rows are separated by a cr-lf, 3) unless the comma or cr-lf is enclosed in double quotes, and 4) a doubled double quote is a double quote in a cell, not an enclosing double quote.

There are libraries that write excel files .xls or .xlsx, visual whatever, perl, c, JAVA. I would pipe to a supporting app if it was not linkable. One would think the xlsx, which is XML, would be easy to write, but MS!

I am new here, i was reading the post. I dont have much expirience but i tried ZetExcel .com platform. My colleagues recommended it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.