Answered by:
Export To Excel

Question
-
Hi All
I need to export gridview records to Excel, the gridview consists of Strikethrough records while export to excel same should reflect in excel.
For Example
In gridview SNo Name
1 Hi This is dineshMonday, July 23, 2012 11:26 AM
Answers
-
I would create a *.csv file, by using a semi colon as a columns delimiter.
To get the data from gridView, simple loop through rows and columns and add data to StringBuilder, which will be later (on the end of looping) saved into an excel file.
This is how you can do it:
private void SaveDataFromDGV_ToExcel() { string filePath = @"C:\excelFile.csv"; string delimiter = ";"; string[] rowData = new string[dataGridView1.Columns.Count]; StringBuilder sb = new StringBuilder(); foreach (DataGridViewRow row in dataGridView1.Rows) { if (!row.IsNewRow) { foreach (DataGridViewCell cell in row.Cells) { rowData[cell.ColumnIndex] = cell.Value.ToString(); } sb.AppendLine(string.Join(delimiter, rowData)); } } System.IO.File.WriteAllText(filePath, sb.ToString()); }
Mitja
- Proposed as answer by Kapul Bhatnagar Tuesday, July 24, 2012 12:21 AM
- Marked as answer by Jason Dot WangModerator Thursday, August 2, 2012 8:34 AM
Monday, July 23, 2012 5:39 PM -
Here a sample of use with a lot of function (e.g. export datatable).
http://simpleooxml.codeplex.com/
Regards, Bubu http://zsvipullo.blogspot.it/
- Proposed as answer by Jason Dot WangModerator Thursday, July 26, 2012 3:43 AM
- Marked as answer by Jason Dot WangModerator Thursday, August 2, 2012 8:34 AM
Tuesday, July 24, 2012 7:17 AM -
Hi,
if you have Excel 2007 (or above) clients I would recommend that you have a look at ClosedXML - http://closedxml.codeplex.com/ - which works really good on top of OpenXML.
Best Regards. When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer. This helps us build a healthy and positive community.
- Proposed as answer by Jason Dot WangModerator Thursday, July 26, 2012 3:44 AM
- Marked as answer by Jason Dot WangModerator Thursday, August 2, 2012 8:34 AM
Tuesday, July 24, 2012 8:28 AM
All replies
-
try exporting in csv format. will work. save
Mark Answered, if it solves your question and Vote if you found it helpful.
Rohit AroraMonday, July 23, 2012 12:17 PM -
If this is apsx try the asp forums.
Ta Ken
Monday, July 23, 2012 1:17 PM -
Hi.
You can use open xml library to create xlsx file.
Warning: you can only create Office 2007 or later files.
Regards, Bubu http://zsvipullo.blogspot.it/
- Edited by Sergio Russo Monday, July 23, 2012 5:28 PM
Monday, July 23, 2012 5:23 PM -
I would create a *.csv file, by using a semi colon as a columns delimiter.
To get the data from gridView, simple loop through rows and columns and add data to StringBuilder, which will be later (on the end of looping) saved into an excel file.
This is how you can do it:
private void SaveDataFromDGV_ToExcel() { string filePath = @"C:\excelFile.csv"; string delimiter = ";"; string[] rowData = new string[dataGridView1.Columns.Count]; StringBuilder sb = new StringBuilder(); foreach (DataGridViewRow row in dataGridView1.Rows) { if (!row.IsNewRow) { foreach (DataGridViewCell cell in row.Cells) { rowData[cell.ColumnIndex] = cell.Value.ToString(); } sb.AppendLine(string.Join(delimiter, rowData)); } } System.IO.File.WriteAllText(filePath, sb.ToString()); }
Mitja
- Proposed as answer by Kapul Bhatnagar Tuesday, July 24, 2012 12:21 AM
- Marked as answer by Jason Dot WangModerator Thursday, August 2, 2012 8:34 AM
Monday, July 23, 2012 5:39 PM -
Caution: use CSV is closely related to the culture of the client. You may have problems with decimal numbers and dates.
Take a look to this link.
http://www.microsoft.com/en-us/download/details.aspx?id=5124
Regards, Bubu http://zsvipullo.blogspot.it/
Tuesday, July 24, 2012 7:02 AM -
Here a sample of use with a lot of function (e.g. export datatable).
http://simpleooxml.codeplex.com/
Regards, Bubu http://zsvipullo.blogspot.it/
- Proposed as answer by Jason Dot WangModerator Thursday, July 26, 2012 3:43 AM
- Marked as answer by Jason Dot WangModerator Thursday, August 2, 2012 8:34 AM
Tuesday, July 24, 2012 7:17 AM -
Hi,
if you have Excel 2007 (or above) clients I would recommend that you have a look at ClosedXML - http://closedxml.codeplex.com/ - which works really good on top of OpenXML.
Best Regards. When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer. This helps us build a healthy and positive community.
- Proposed as answer by Jason Dot WangModerator Thursday, July 26, 2012 3:44 AM
- Marked as answer by Jason Dot WangModerator Thursday, August 2, 2012 8:34 AM
Tuesday, July 24, 2012 8:28 AM -
So, the need is to " need to export gridview records to Excel"?
I just stumbled across this post, about 6-months after it was put up. Normally, I wouldn’t respond to something that is, well, so old, but, I just solved this issue a couple days ago…with some help from the C# Discussion Forum...and I don’t think any of the posts put up here actually answer the questions. So, I'm posting my own response!! Remember, set a reference to Excel!!
private void button2_Click(object sender, EventArgs e)
{
// creating Excel Application
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
app.Visible = true;
// get the reference of first sheet. By default its name is Sheet1.
// store its reference to worksheet
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
// changing the name of active sheet
worksheet.Name = "Exported from gridview";
// storing header part in Excel
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
// save the application
workbook.SaveAs("c:\\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// Exit from the application
app.Quit();
}
ALSO . . .
private void button7_Click(object sender, EventArgs e)
{
Excel.Application app = new Excel.Application();
app.Visible = true;
Excel.Workbook wb = app.Workbooks.Add(1);
Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
// changing the name of active sheet
ws.Name = "Exported from gridview";
ws.Rows.HorizontalAlignment = HorizontalAlignment.Center;
// storing header part in Excel
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
ws.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
ws.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
// sizing the columns
ws.Cells.EntireColumn.AutoFit();
// save the application
wb.SaveAs("C:\\Users\\Excel\\Desktop\\Coding\\DOT.NET\\Samples C#\\Export DataGridView to SQL Server Table\\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// Exit from the application
app.Quit();
}
Ryan Shuell
Sunday, November 4, 2012 4:34 AM -
Hi,
Try the link below:
http://stackoverflow.com/questions/10411520/export-grid-view-to-excel-and-save-excel-file-to-folder
Thanks
Md. Marufuzzaman
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.- Proposed as answer by Md. Marufuzzaman Sunday, November 4, 2012 5:41 AM
Sunday, November 4, 2012 5:41 AM