Export C# Windows Datagridview to excel
-
Hi I want to export Datagridview to excel. I have formatted my datagridview such that he negative values are shown as red, some values are bold and other formatting styles are applied. I want when I export it to Excel then it export with same formatted type. Currently I Am using the below function to export. But unable to export with formatting. private void exportAsExcel() { Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); // creating new WorkBook within Excel application Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); // creating new Excelsheet in workbook Microsoft.Office.Interop.Excel._Worksheet worksheet = null; // see the excel sheet behind the program //Funny app.Visible = true; // get the reference of first sheet. By default its name is Sheet1. // store its reference to worksheet try { //Fixed:(Microsoft.Office.Interop.Excel.Worksheet) worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"]; worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet; // changing the name of active sheet worksheet.Name = "Sheet1"; // storing header part in Excel for (int i = 1; i < dgvDetails.Columns.Count + 1; i++) { worksheet.Cells[1, i] = dgvDetails.Columns[i - 1].HeaderText; } worksheet.get_Range("A1", "AZ1").Font.Bold = true; worksheet.get_Range("A1", "A" + (dgvDetails.RowCount + 2).ToString()).Font.Bold = true; // storing Each row and column value to excel sheet for (int i = 0; i < dgvDetails.Rows.Count; i++) { for (int j = 0; j < dgvDetails.Columns.Count; j++) { worksheet.Cells[i + 2, j + 1] = dgvDetails.Rows[i].Cells[j].Value.ToString(); } } } catch (System.Exception ex) { } finally { app.Quit(); workbook = null; app = null; } } Thanks in advance
-
Hi I want to export Datagridview to excel. I have formatted my datagridview such that he negative values are shown as red, some values are bold and other formatting styles are applied. I want when I export it to Excel then it export with same formatted type. Currently I Am using the below function to export. But unable to export with formatting. private void exportAsExcel() { Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); // creating new WorkBook within Excel application Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); // creating new Excelsheet in workbook Microsoft.Office.Interop.Excel._Worksheet worksheet = null; // see the excel sheet behind the program //Funny app.Visible = true; // get the reference of first sheet. By default its name is Sheet1. // store its reference to worksheet try { //Fixed:(Microsoft.Office.Interop.Excel.Worksheet) worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"]; worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet; // changing the name of active sheet worksheet.Name = "Sheet1"; // storing header part in Excel for (int i = 1; i < dgvDetails.Columns.Count + 1; i++) { worksheet.Cells[1, i] = dgvDetails.Columns[i - 1].HeaderText; } worksheet.get_Range("A1", "AZ1").Font.Bold = true; worksheet.get_Range("A1", "A" + (dgvDetails.RowCount + 2).ToString()).Font.Bold = true; // storing Each row and column value to excel sheet for (int i = 0; i < dgvDetails.Rows.Count; i++) { for (int j = 0; j < dgvDetails.Columns.Count; j++) { worksheet.Cells[i + 2, j + 1] = dgvDetails.Rows[i].Cells[j].Value.ToString(); } } } catch (System.Exception ex) { } finally { app.Quit(); workbook = null; app = null; } } Thanks in advance
Please format your code with < Pre > tag first. For solution, Please refer to this article: How to export GridView to Excel, render cells, add WordArt, and fill a custom template[^]
// ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫
-
Hi I want to export Datagridview to excel. I have formatted my datagridview such that he negative values are shown as red, some values are bold and other formatting styles are applied. I want when I export it to Excel then it export with same formatted type. Currently I Am using the below function to export. But unable to export with formatting. private void exportAsExcel() { Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); // creating new WorkBook within Excel application Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); // creating new Excelsheet in workbook Microsoft.Office.Interop.Excel._Worksheet worksheet = null; // see the excel sheet behind the program //Funny app.Visible = true; // get the reference of first sheet. By default its name is Sheet1. // store its reference to worksheet try { //Fixed:(Microsoft.Office.Interop.Excel.Worksheet) worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"]; worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet; // changing the name of active sheet worksheet.Name = "Sheet1"; // storing header part in Excel for (int i = 1; i < dgvDetails.Columns.Count + 1; i++) { worksheet.Cells[1, i] = dgvDetails.Columns[i - 1].HeaderText; } worksheet.get_Range("A1", "AZ1").Font.Bold = true; worksheet.get_Range("A1", "A" + (dgvDetails.RowCount + 2).ToString()).Font.Bold = true; // storing Each row and column value to excel sheet for (int i = 0; i < dgvDetails.Rows.Count; i++) { for (int j = 0; j < dgvDetails.Columns.Count; j++) { worksheet.Cells[i + 2, j + 1] = dgvDetails.Rows[i].Cells[j].Value.ToString(); } } } catch (System.Exception ex) { } finally { app.Quit(); workbook = null; app = null; } } Thanks in advance
try this :) http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/27a9002e-9d53-440d-9750-e6c64406fdd6[^]
-ambarish changanacherry-