Dynamically add TabPages to my form depending on the number of worksheets in a spreadsheet
-
Hi, I would like to dynamically add TabPages to my form depending on a number of Sheets$ that are contained in a spredsheet I'm loading to a datagrid. At the moment I have an application that loads a spreadsheet to a datagrid, but it only loads one (the default) worksheet. How can I dynamically create tabpages depending on the number of worksheets in the spreasheet and load them to the datagrid. Below is the code of what the application can do so far: //This takes the Excel file from a specific location //and displays it in the datagrid private void btnOpenFile_Click(object sender, EventArgs e) { // prepare open file dialog to only search for excel files this.ofd.FileName = "*.xls"; if (this.ofd.ShowDialog() == DialogResult.OK) { try { // Here is the call to Open a Workbook in Excel // It uses most of the default values (except for the read-only which we set to true) Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(ofd.FileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, true, true); // get the collection of sheets in the workbook Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets; // get the first and only worksheet from the collection of worksheets Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1); //get start and end rows Object[] startRow = { worksheet.get_Range("A4", "O4") }; int endRow = worksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row; //Check if all columns are according to spec if ((worksheet.get_Range("A3", "A3").Text.ToString() == "Template") && (worksheet.get_Range("B3", "B3").Text.ToString() == "Error Code") && (worksheet.get_Range("C3", "C3").Text.ToString() == "Error Name") && (worksheet.get_Range("D3", "D3").Text.ToString() == "Error Description") && (worksheet.get_Range("E3", "E3").Text.ToString() == "Error Cause") && (worksheet.get_Ra
-
Hi, I would like to dynamically add TabPages to my form depending on a number of Sheets$ that are contained in a spredsheet I'm loading to a datagrid. At the moment I have an application that loads a spreadsheet to a datagrid, but it only loads one (the default) worksheet. How can I dynamically create tabpages depending on the number of worksheets in the spreasheet and load them to the datagrid. Below is the code of what the application can do so far: //This takes the Excel file from a specific location //and displays it in the datagrid private void btnOpenFile_Click(object sender, EventArgs e) { // prepare open file dialog to only search for excel files this.ofd.FileName = "*.xls"; if (this.ofd.ShowDialog() == DialogResult.OK) { try { // Here is the call to Open a Workbook in Excel // It uses most of the default values (except for the read-only which we set to true) Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(ofd.FileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, true, true); // get the collection of sheets in the workbook Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets; // get the first and only worksheet from the collection of worksheets Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1); //get start and end rows Object[] startRow = { worksheet.get_Range("A4", "O4") }; int endRow = worksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row; //Check if all columns are according to spec if ((worksheet.get_Range("A3", "A3").Text.ToString() == "Template") && (worksheet.get_Range("B3", "B3").Text.ToString() == "Error Code") && (worksheet.get_Range("C3", "C3").Text.ToString() == "Error Name") && (worksheet.get_Range("D3", "D3").Text.ToString() == "Error Description") && (worksheet.get_Range("E3", "E3").Text.ToString() == "Error Cause") && (worksheet.get_Ra
I'm not terribly familiar with the Microsoft.Office namespace but after you get the collection of WorkSheets called sheets, I would think you can loop through them all and create pages. Something like this:
TabPage page; DataGridView dgv; foreach(Worksheet sheet in sheets) { page = new TabPage(); dgv = new DataGridView(); //loop thru records in worksheet. foreach record, add row to datagridview --enter code here-- //after adding records, add datagridview to tab page page.Controls.Add(dgv); //add the page to your TabControl //this line each page to the first position this.tabControl1.TabPages.Insert(0, page); }
Hope this helps. Lester http://www.lestersconyers.com modified on Monday, April 7, 2008 10:25 AMmodified on Monday, April 7, 2008 11:07 AM
-
I'm not terribly familiar with the Microsoft.Office namespace but after you get the collection of WorkSheets called sheets, I would think you can loop through them all and create pages. Something like this:
TabPage page; DataGridView dgv; foreach(Worksheet sheet in sheets) { page = new TabPage(); dgv = new DataGridView(); //loop thru records in worksheet. foreach record, add row to datagridview --enter code here-- //after adding records, add datagridview to tab page page.Controls.Add(dgv); //add the page to your TabControl //this line each page to the first position this.tabControl1.TabPages.Insert(0, page); }
Hope this helps. Lester http://www.lestersconyers.com modified on Monday, April 7, 2008 10:25 AMmodified on Monday, April 7, 2008 11:07 AM
-
I'm not terribly familiar with the Microsoft.Office namespace but after you get the collection of WorkSheets called sheets, I would think you can loop through them all and create pages. Something like this:
TabPage page; DataGridView dgv; foreach(Worksheet sheet in sheets) { page = new TabPage(); dgv = new DataGridView(); //loop thru records in worksheet. foreach record, add row to datagridview --enter code here-- //after adding records, add datagridview to tab page page.Controls.Add(dgv); //add the page to your TabControl //this line each page to the first position this.tabControl1.TabPages.Insert(0, page); }
Hope this helps. Lester http://www.lestersconyers.com modified on Monday, April 7, 2008 10:25 AMmodified on Monday, April 7, 2008 11:07 AM
Hi, Thanks for the reply. Should I have put this code in the constructor or should I put it under the button that loads the spreadsheet? Should I put the tabControl from the ToolBox or should I only use the dynamically created one? I have put in your suggested code but when I load the spreadsheet my form just becomes blank and the tabs do not get created. :doh:
Mvelo Walaza Developer Telkom SA
-
Hi, Thanks for the reply. Should I have put this code in the constructor or should I put it under the button that loads the spreadsheet? Should I put the tabControl from the ToolBox or should I only use the dynamically created one? I have put in your suggested code but when I load the spreadsheet my form just becomes blank and the tabs do not get created. :doh:
Mvelo Walaza Developer Telkom SA
I would put this code under the button that loads the spreadsheet. If not, you'll get a problem. And I need to modify the code I submitted. In the code I had you create a new TabControl without adding it to the form. Hopefully, you already have a tab control on the page so the designer can set a bunch of properties. I'm going to modify my original code based on that assumption. If not, you'll have to do something like
this.Controls.Add(tabControl1);
-
I would put this code under the button that loads the spreadsheet. If not, you'll get a problem. And I need to modify the code I submitted. In the code I had you create a new TabControl without adding it to the form. Hopefully, you already have a tab control on the page so the designer can set a bunch of properties. I'm going to modify my original code based on that assumption. If not, you'll have to do something like
this.Controls.Add(tabControl1);
Thank you so much, I am making so much progress in one day! The problem is, it only shows the data of only one worksheet. It shows the tabs and their worksheets names but does not add the datagrid and the data to the tabpage. What could I be doing wrong, here's the code: //This takes the Excel file from a specific location //and displays it in the datagrid private void btnOpenFile_Click(object sender, EventArgs e) { // prepare open file dialog to only search for excel files this.ofd.FileName = "*.xls"; if (this.ofd.ShowDialog() == DialogResult.OK) { try { // Here is the call to Open a Workbook in Excel // It uses most of the default values (except for the read-only which we set to true) Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(ofd.FileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, true, true); // get the collection of sheets in the workbook Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets; //theWorkbook.Worksheets[sheets]; // get the first and only worksheet from the collection of worksheets Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1); TabControl tbControl = new TabControl(); this.Controls.Add(tbControl); tbControl.Dock = DockStyle.Bottom; tbControl.Height = 660; TabPage page; //DataGridView dgv; foreach (Worksheet sheet in sheets) { page = new TabPage(sheet.Name.ToString()); //dgOutput = new DataGridView(); //get start and end rows Object[] startRow = { worksheet.get_Range("A4", "O4") }; int endRow = worksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row; //Check the columns...Removed // loop through the rows of the spreadsheet and place each row in the datagrid for (int i = 5; i <= endRo
-
Thank you so much, I am making so much progress in one day! The problem is, it only shows the data of only one worksheet. It shows the tabs and their worksheets names but does not add the datagrid and the data to the tabpage. What could I be doing wrong, here's the code: //This takes the Excel file from a specific location //and displays it in the datagrid private void btnOpenFile_Click(object sender, EventArgs e) { // prepare open file dialog to only search for excel files this.ofd.FileName = "*.xls"; if (this.ofd.ShowDialog() == DialogResult.OK) { try { // Here is the call to Open a Workbook in Excel // It uses most of the default values (except for the read-only which we set to true) Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(ofd.FileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, true, true); // get the collection of sheets in the workbook Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets; //theWorkbook.Worksheets[sheets]; // get the first and only worksheet from the collection of worksheets Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1); TabControl tbControl = new TabControl(); this.Controls.Add(tbControl); tbControl.Dock = DockStyle.Bottom; tbControl.Height = 660; TabPage page; //DataGridView dgv; foreach (Worksheet sheet in sheets) { page = new TabPage(sheet.Name.ToString()); //dgOutput = new DataGridView(); //get start and end rows Object[] startRow = { worksheet.get_Range("A4", "O4") }; int endRow = worksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row; //Check the columns...Removed // loop through the rows of the spreadsheet and place each row in the datagrid for (int i = 5; i <= endRo
You have to create a new datagridview object for each new page. Then add that datagridview to the page. Now, you're just adding dgOutput to each page. Then reseting it and adding it to the next page. It'll be on the last page you inserted, which will be the first page when you .Insert(0, page). Uncomment the //dgOutput = new DataGridView();
-
You have to create a new datagridview object for each new page. Then add that datagridview to the page. Now, you're just adding dgOutput to each page. Then reseting it and adding it to the next page. It'll be on the last page you inserted, which will be the first page when you .Insert(0, page). Uncomment the //dgOutput = new DataGridView();
When I uncomment the line: dgOutput = new DataGridView(); I get the ff error: 'No row can be added to a DataGridView control that does not have columns. Columns must be added first.' Is there anything else I need to add to the code below: TabControl tbControl = new TabControl(); this.Controls.Add(tbControl); tbControl.Dock = DockStyle.Bottom; tbControl.Height = 660; TabPage page; foreach (Worksheet sheet in sheets) { page = new TabPage(sheet.Name.ToString()); dgOutput = new DataGridView(); //get start and end rows Object[] startRow = { worksheet.get_Range("A4", "O4") }; int endRow = worksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row; //Check if all columns are according to spec if ((worksheet.get_Range("A3", "A3").Text.ToString() == "Template") && (worksheet.get_Range("B3", "B3").Text.ToString() == "Error Code") && (worksheet.get_Range("C3", "C3").Text.ToString() == "Error Name") && (worksheet.get_Range("D3", "D3").Text.ToString() == "Error Description") && (worksheet.get_Range("E3", "E3").Text.ToString() == "Error Cause") && (worksheet.get_Range("F3", "F3").Text.ToString() == "Source") && (worksheet.get_Range("G3", "G3").Text.ToString() == "Destination") && (worksheet.get_Range("H3", "H3").Text.ToString() == "Category") && (worksheet.get_Range("I3", "I3").Text.ToString() == "Severity") && (worksheet.get_Range("J3", "J3").Text.ToString() == "Threshold") && (worksheet.get_Range("K3", "K3").Text.ToString() == "Responsible for Resolution") && (worksheet.get_Range("L3", "L3").Text.ToString() == "Action") && (worksheet.get_Range("M3", "M3").Text.ToString() == "ECS Resubmit Y/N") && (worksheet.get_Range("N3", "N3").Text.ToString() == "ECS Auto Resubmit Y/N") &&
-
When I uncomment the line: dgOutput = new DataGridView(); I get the ff error: 'No row can be added to a DataGridView control that does not have columns. Columns must be added first.' Is there anything else I need to add to the code below: TabControl tbControl = new TabControl(); this.Controls.Add(tbControl); tbControl.Dock = DockStyle.Bottom; tbControl.Height = 660; TabPage page; foreach (Worksheet sheet in sheets) { page = new TabPage(sheet.Name.ToString()); dgOutput = new DataGridView(); //get start and end rows Object[] startRow = { worksheet.get_Range("A4", "O4") }; int endRow = worksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row; //Check if all columns are according to spec if ((worksheet.get_Range("A3", "A3").Text.ToString() == "Template") && (worksheet.get_Range("B3", "B3").Text.ToString() == "Error Code") && (worksheet.get_Range("C3", "C3").Text.ToString() == "Error Name") && (worksheet.get_Range("D3", "D3").Text.ToString() == "Error Description") && (worksheet.get_Range("E3", "E3").Text.ToString() == "Error Cause") && (worksheet.get_Range("F3", "F3").Text.ToString() == "Source") && (worksheet.get_Range("G3", "G3").Text.ToString() == "Destination") && (worksheet.get_Range("H3", "H3").Text.ToString() == "Category") && (worksheet.get_Range("I3", "I3").Text.ToString() == "Severity") && (worksheet.get_Range("J3", "J3").Text.ToString() == "Threshold") && (worksheet.get_Range("K3", "K3").Text.ToString() == "Responsible for Resolution") && (worksheet.get_Range("L3", "L3").Text.ToString() == "Action") && (worksheet.get_Range("M3", "M3").Text.ToString() == "ECS Resubmit Y/N") && (worksheet.get_Range("N3", "N3").Text.ToString() == "ECS Auto Resubmit Y/N") &&
There's nothing else you can do. You have to create a new datagridview for each tab. Within the foreach loop, set up the columns for the datagridview. You can't use a global datagridview. Within that foreach loop, do everything you need to do for a datagridview then add it to the tabpage.
-
There's nothing else you can do. You have to create a new datagridview for each tab. Within the foreach loop, set up the columns for the datagridview. You can't use a global datagridview. Within that foreach loop, do everything you need to do for a datagridview then add it to the tabpage.
I did as you told me and the datagrid is visible in each tabpage but now the problem is that it only shows the data that is in the first sheet in all the tabs. Each tabpage should have its own data and should display its own worksheet in the datagrid.
Mvelo Walaza Developer Telkom SA
-
When I uncomment the line: dgOutput = new DataGridView(); I get the ff error: 'No row can be added to a DataGridView control that does not have columns. Columns must be added first.' Is there anything else I need to add to the code below: TabControl tbControl = new TabControl(); this.Controls.Add(tbControl); tbControl.Dock = DockStyle.Bottom; tbControl.Height = 660; TabPage page; foreach (Worksheet sheet in sheets) { page = new TabPage(sheet.Name.ToString()); dgOutput = new DataGridView(); //get start and end rows Object[] startRow = { worksheet.get_Range("A4", "O4") }; int endRow = worksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row; //Check if all columns are according to spec if ((worksheet.get_Range("A3", "A3").Text.ToString() == "Template") && (worksheet.get_Range("B3", "B3").Text.ToString() == "Error Code") && (worksheet.get_Range("C3", "C3").Text.ToString() == "Error Name") && (worksheet.get_Range("D3", "D3").Text.ToString() == "Error Description") && (worksheet.get_Range("E3", "E3").Text.ToString() == "Error Cause") && (worksheet.get_Range("F3", "F3").Text.ToString() == "Source") && (worksheet.get_Range("G3", "G3").Text.ToString() == "Destination") && (worksheet.get_Range("H3", "H3").Text.ToString() == "Category") && (worksheet.get_Range("I3", "I3").Text.ToString() == "Severity") && (worksheet.get_Range("J3", "J3").Text.ToString() == "Threshold") && (worksheet.get_Range("K3", "K3").Text.ToString() == "Responsible for Resolution") && (worksheet.get_Range("L3", "L3").Text.ToString() == "Action") && (worksheet.get_Range("M3", "M3").Text.ToString() == "ECS Resubmit Y/N") && (worksheet.get_Range("N3", "N3").Text.ToString() == "ECS Auto Resubmit Y/N") &&
Where does that variable named worksheet come from? Instead of using worksheet to check validity and get range, you should be using the variable name sheet. Replace the variable named worksheet with the variable named sheet inside of the foreach loop.
-
Where does that variable named worksheet come from? Instead of using worksheet to check validity and get range, you should be using the variable name sheet. Replace the variable named worksheet with the variable named sheet inside of the foreach loop.
-
Where does that variable named worksheet come from? Instead of using worksheet to check validity and get range, you should be using the variable name sheet. Replace the variable named worksheet with the variable named sheet inside of the foreach loop.
Hi, Would you happen to know how I could take the data from all the dynamically created tabpages’ datagrids and export the data to excel and create a new worksheet for each tabpage as it is exporting. Currently it takes data from one dynamically created tabpage (the default)‘s datagrid and exports it to a spreadsheet. Here's the code I have so far: Microsoft.Office.Interop.Excel._Application _appClass = new ApplicationClass(); _appClass.Visible = true; _appClass.Caption = "Exported ECS Errors"; Microsoft.Office.Interop.Excel.Workbook wbk = _appClass.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); Worksheet obj = (Worksheet)wbk.ActiveSheet; // get the collection of sheets in the workbook Microsoft.Office.Interop.Excel.Sheets sheets = wbk.Worksheets; Range r = (Range)obj.Cells; try { //Load the Headers and make them BOLD for (int i = 0; i < ((DataGridView)this.dgOutput).Columns.Count; i++) { r[1, i + 1] = ((DataGridView)this.dgOutput).Columns[i].HeaderText.ToString(); ((Range)r.Cells[1, i + 1]).Font.Bold = true; } //Load the data from the datagrid to the new spreadsheet for (int i = 1; i < ((DataGridView)this.dgOutput).Rows.Count - 1; i++) { for (int j = 0; j < ((DataGridView)this.dgOutput).Columns.Count; j++) { r[i + 1, j + 1] = ((DataGridView)this.dgOutput).Rows[i].Cells[j].Value.ToString(); //Do the colors if (((DataGridView)this.dgOutput).Rows[i].Cells[j].Value.ToString() == "Update OK") { ((Range)r.Cells[i + 1, 1]).Font.Color = Color.Green.G; } } } } catch (Exception EX) //user closes the Excel file while it is loading data from the datagrid { MessageBox.Show(EX.Message + "The Excel file was closed while loading data from the datagrid.", "Error : Loading file!!", MessageBoxButtons.OK, MessageB
-
Hi, Would you happen to know how I could take the data from all the dynamically created tabpages’ datagrids and export the data to excel and create a new worksheet for each tabpage as it is exporting. Currently it takes data from one dynamically created tabpage (the default)‘s datagrid and exports it to a spreadsheet. Here's the code I have so far: Microsoft.Office.Interop.Excel._Application _appClass = new ApplicationClass(); _appClass.Visible = true; _appClass.Caption = "Exported ECS Errors"; Microsoft.Office.Interop.Excel.Workbook wbk = _appClass.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); Worksheet obj = (Worksheet)wbk.ActiveSheet; // get the collection of sheets in the workbook Microsoft.Office.Interop.Excel.Sheets sheets = wbk.Worksheets; Range r = (Range)obj.Cells; try { //Load the Headers and make them BOLD for (int i = 0; i < ((DataGridView)this.dgOutput).Columns.Count; i++) { r[1, i + 1] = ((DataGridView)this.dgOutput).Columns[i].HeaderText.ToString(); ((Range)r.Cells[1, i + 1]).Font.Bold = true; } //Load the data from the datagrid to the new spreadsheet for (int i = 1; i < ((DataGridView)this.dgOutput).Rows.Count - 1; i++) { for (int j = 0; j < ((DataGridView)this.dgOutput).Columns.Count; j++) { r[i + 1, j + 1] = ((DataGridView)this.dgOutput).Rows[i].Cells[j].Value.ToString(); //Do the colors if (((DataGridView)this.dgOutput).Rows[i].Cells[j].Value.ToString() == "Update OK") { ((Range)r.Cells[i + 1, 1]).Font.Color = Color.Green.G; } } } } catch (Exception EX) //user closes the Excel file while it is loading data from the datagrid { MessageBox.Show(EX.Message + "The Excel file was closed while loading data from the datagrid.", "Error : Loading file!!", MessageBoxButtons.OK, MessageB
I don't see why not. You just have to loop through the tabpages. You have to know your logic. I don't have time to work on the code but here is what should work:
//loop through each tabpage in tabcontrol.tabpages foreach(TabPage page in tabcontrol.TabPages) { //get the datagrid from the tabpage's control. It may not be at the 0 index but it should be in there DataGridView dgv = (DataGridView)page.Controls[0]; //loop through each datarow in datagridview foreach(DataGridViewRow row in dgv.Rows) { //create new worksheet //load data from datagrid to worksheet //add worksheet to workbook } }
Good luckLester http://www.lestersconyers.com
modified on Wednesday, April 9, 2008 9:40 AM
-
I don't see why not. You just have to loop through the tabpages. You have to know your logic. I don't have time to work on the code but here is what should work:
//loop through each tabpage in tabcontrol.tabpages foreach(TabPage page in tabcontrol.TabPages) { //get the datagrid from the tabpage's control. It may not be at the 0 index but it should be in there DataGridView dgv = (DataGridView)page.Controls[0]; //loop through each datarow in datagridview foreach(DataGridViewRow row in dgv.Rows) { //create new worksheet //load data from datagrid to worksheet //add worksheet to workbook } }
Good luckLester http://www.lestersconyers.com
modified on Wednesday, April 9, 2008 9:40 AM
Hi, These are the errors I get when I try the code you suggested: 1. foreach statement cannot operate on variables of type 'System.Windows.Forms.TabControl' because 'System.Windows.Forms.TabControl' does not contain a public definition for 'GetEnumerator' 2. foreach statement cannot operate on variables of type 'System.Windows.Forms.DataGridView' because 'System.Windows.Forms.DataGridView' does not contain a public definition for 'GetEnumerator' Is there anything I am missing?
Mvelo Walaza Developer Telkom SA
-
Hi, These are the errors I get when I try the code you suggested: 1. foreach statement cannot operate on variables of type 'System.Windows.Forms.TabControl' because 'System.Windows.Forms.TabControl' does not contain a public definition for 'GetEnumerator' 2. foreach statement cannot operate on variables of type 'System.Windows.Forms.DataGridView' because 'System.Windows.Forms.DataGridView' does not contain a public definition for 'GetEnumerator' Is there anything I am missing?
Mvelo Walaza Developer Telkom SA
I edited the code. Try it.
-
I don't see why not. You just have to loop through the tabpages. You have to know your logic. I don't have time to work on the code but here is what should work:
//loop through each tabpage in tabcontrol.tabpages foreach(TabPage page in tabcontrol.TabPages) { //get the datagrid from the tabpage's control. It may not be at the 0 index but it should be in there DataGridView dgv = (DataGridView)page.Controls[0]; //loop through each datarow in datagridview foreach(DataGridViewRow row in dgv.Rows) { //create new worksheet //load data from datagrid to worksheet //add worksheet to workbook } }
Good luckLester http://www.lestersconyers.com
modified on Wednesday, April 9, 2008 9:40 AM
I tried the code but when I click the button nothing happens. What could I be doing wrong? Here is the code: //This exports data from the datagrid to a new EXCEL spreadsheet private void btnExport_Click(object sender, EventArgs e) { //Check if the file has been added to the datagrid if (dgOutput.RowCount == 1) { MessageBox.Show("Please load the file first!!", "Error : Load file!!", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { TabControl tbControl = new TabControl(); //loop through each tabpage in tabcontrol.tabpages foreach (TabPage page in tbControl.TabPages) { //get the datagrid from the tabpage's control. It may not be at the 0 index but it should be in there DataGridView dgv = (DataGridView)page.Controls[0]; //loop through each datarow in datagridview foreach (DataGridViewRow row in dgv.Rows) { //create new worksheet Microsoft.Office.Interop.Excel._Application _appClass = new ApplicationClass(); _appClass.Visible = true; _appClass.Caption = "Exported ECS Errors"; Microsoft.Office.Interop.Excel.Workbook wbk = _appClass.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); Worksheet obj = (Worksheet)wbk.ActiveSheet; // get the collection of sheets in the workbook Microsoft.Office.Interop.Excel.Sheets sheets = wbk.Worksheets; Range r = (Range)obj.Cells; //_appClass.Workbooks.Add(obj); //load data from datagrid to worksheet try { //Load the Headers and make them BOLD for (int i = 0; i < ((DataGridView)this.dgOutput).Columns.Count; i++) { r[1, i + 1] = ((DataGridView)this.dgOutput).Columns[i].HeaderText.ToString(); ((Range)r.Cells[1, i + 1]).Font.Bold = true; } //Load the data from the datagrid to the new spreadsheet
-
I tried the code but when I click the button nothing happens. What could I be doing wrong? Here is the code: //This exports data from the datagrid to a new EXCEL spreadsheet private void btnExport_Click(object sender, EventArgs e) { //Check if the file has been added to the datagrid if (dgOutput.RowCount == 1) { MessageBox.Show("Please load the file first!!", "Error : Load file!!", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { TabControl tbControl = new TabControl(); //loop through each tabpage in tabcontrol.tabpages foreach (TabPage page in tbControl.TabPages) { //get the datagrid from the tabpage's control. It may not be at the 0 index but it should be in there DataGridView dgv = (DataGridView)page.Controls[0]; //loop through each datarow in datagridview foreach (DataGridViewRow row in dgv.Rows) { //create new worksheet Microsoft.Office.Interop.Excel._Application _appClass = new ApplicationClass(); _appClass.Visible = true; _appClass.Caption = "Exported ECS Errors"; Microsoft.Office.Interop.Excel.Workbook wbk = _appClass.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); Worksheet obj = (Worksheet)wbk.ActiveSheet; // get the collection of sheets in the workbook Microsoft.Office.Interop.Excel.Sheets sheets = wbk.Worksheets; Range r = (Range)obj.Cells; //_appClass.Workbooks.Add(obj); //load data from datagrid to worksheet try { //Load the Headers and make them BOLD for (int i = 0; i < ((DataGridView)this.dgOutput).Columns.Count; i++) { r[1, i + 1] = ((DataGridView)this.dgOutput).Columns[i].HeaderText.ToString(); ((Range)r.Cells[1, i + 1]).Font.Bold = true; } //Load the data from the datagrid to the new spreadsheet
Just about your whole code needs to be rewritten. I think you're making this harder on yourself than you needed to. The reason nothing is happening is because tbControl is a new tabcontrol with no tabpages. Instead of instantiating a new tabcontrol, you need to loop through the tabpages of the original tabcontrol that you were working with at the beginning. To save yourself some trouble, let the designer work for you. Simply drag and drop a tabcontrol on the page. Now, it can be accessed globally. Also, ditch that global datagridview named dgOutput. You don't need it and it shouldn't be being used. It shouldn't be being used because you create a new datagridview for each tab page that's created dynamically. Once you get all that rewired, take a look at your code. Think about exactly what's going to happen. Right now, if that foreach loop would execute, you would only be getting data from this.dgOutput but your looping through the rows of dgv which is the datagridview on each tabpage. You want to loop through the rows of dgv to get values. You want to loop through the columns of dgv to load the headers. Then, after the headers and values are in the worksheet, you want to add it to the workbook. Set some breakpoints within your loops and the rest of your code to debug. It'll save you alot of time and you'll learn a whole lot quicker than somebody helping you.