Adding data to a new worksheet in an existing workbook c#
-
Hey everyone. I've been scrounging around on here and finally figured out how to export the data to an excel document from my dataset, the only problem i'm having now is I can't add a new worksheet. I have a data set with that i've filled with three different tables and I need each table to go into a seperate worksheet (so the marketing guys can then use that for their photoshop stuff.) what I have so far is just adding different headers on the same sheet...
Excel.ApplicationClass excel = new Excel.ApplicationClass();
excel.Application.Workbooks.Add(true);
System.Data.DataTable table = data1.Tables["table1"];int Collindex = 0; int rowIndex = 2; excel.Cells\[1, 1\] = "table 1"; foreach (System.Data.DataColumn col in table.Columns) { Collindex ++; excel.Cells\[2, Collindex\] = col.ColumnName; } foreach (DataRow row in table.Rows) { rowIndex++; progbar2.PerformStep(); progbar2.Refresh(); Collindex = 0; foreach (DataColumn col in table.Columns) { Collindex ++; excel.Cells\[rowIndex + 1, Collindex\] = row\[col.ColumnName\]; } } progbar1.PerformStep(); progbar1.Refresh(); table.Clear(); table = data1.Tables\["table2"\]; progbar2.Value = 0; progbar2.Refresh(); progbar2.Maximum = data1.Tables\["table2"\].Rows.Count; progbar2.Refresh(); rowIndex = rowIndex + 3; excel.Cells\[rowIndex, 1\] = "table 2"; foreach (DataRow row in table.Rows) { rowIndex++; progbar2.PerformStep(); progbar2.Refresh(); Collindex = 0; foreach (DataColumn col in table.Columns) { Collindex++; excel.Cells\[rowIndex + 1, Collindex\] = row\[col.ColumnName\]; } } progbar1.PerformStep(); progbar1.Refresh(); table.Clear(); table = data1.Tables\["table3"\]; progbar2.Value = 0; progbar2.Refresh(); progbar2.Maximum = data1.Tables\["table3"\].Rows.Count; progbar2.Ref
-
Hey everyone. I've been scrounging around on here and finally figured out how to export the data to an excel document from my dataset, the only problem i'm having now is I can't add a new worksheet. I have a data set with that i've filled with three different tables and I need each table to go into a seperate worksheet (so the marketing guys can then use that for their photoshop stuff.) what I have so far is just adding different headers on the same sheet...
Excel.ApplicationClass excel = new Excel.ApplicationClass();
excel.Application.Workbooks.Add(true);
System.Data.DataTable table = data1.Tables["table1"];int Collindex = 0; int rowIndex = 2; excel.Cells\[1, 1\] = "table 1"; foreach (System.Data.DataColumn col in table.Columns) { Collindex ++; excel.Cells\[2, Collindex\] = col.ColumnName; } foreach (DataRow row in table.Rows) { rowIndex++; progbar2.PerformStep(); progbar2.Refresh(); Collindex = 0; foreach (DataColumn col in table.Columns) { Collindex ++; excel.Cells\[rowIndex + 1, Collindex\] = row\[col.ColumnName\]; } } progbar1.PerformStep(); progbar1.Refresh(); table.Clear(); table = data1.Tables\["table2"\]; progbar2.Value = 0; progbar2.Refresh(); progbar2.Maximum = data1.Tables\["table2"\].Rows.Count; progbar2.Refresh(); rowIndex = rowIndex + 3; excel.Cells\[rowIndex, 1\] = "table 2"; foreach (DataRow row in table.Rows) { rowIndex++; progbar2.PerformStep(); progbar2.Refresh(); Collindex = 0; foreach (DataColumn col in table.Columns) { Collindex++; excel.Cells\[rowIndex + 1, Collindex\] = row\[col.ColumnName\]; } } progbar1.PerformStep(); progbar1.Refresh(); table.Clear(); table = data1.Tables\["table3"\]; progbar2.Value = 0; progbar2.Refresh(); progbar2.Maximum = data1.Tables\["table3"\].Rows.Count; progbar2.Ref
MSDN How to add new worksheets to workbooks[^] That help?
Life, family, faith: Give me a visit. From my latest post: "When Constantine severed the Hebrew origins of this faith in Messiah, a new religion was officially created. It is this religion that Orthodox Jews fear their relatives and friends becoming members of." Judah Himango
-
MSDN How to add new worksheets to workbooks[^] That help?
Life, family, faith: Give me a visit. From my latest post: "When Constantine severed the Hebrew origins of this faith in Messiah, a new religion was officially created. It is this religion that Orthodox Jews fear their relatives and friends becoming members of." Judah Himango
Well, I've seen this before and tried it but the compiler yells at me saying missing isn't found in the current context. I even tried replacing globals.thisworkbook with excel.application.workbook but it still gave me the same problem. What I don't understand is why we can't just add a worksheet without passing any parameters.
-
Well, I've seen this before and tried it but the compiler yells at me saying missing isn't found in the current context. I even tried replacing globals.thisworkbook with excel.application.workbook but it still gave me the same problem. What I don't understand is why we can't just add a worksheet without passing any parameters.
workbook.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value); The MS example was incomplete. The line above works.