how to export datagridview data to pipe separated text using c# .net windows application?
I want to export datagridview data to psv format using c# .net windows application.
How it can be possible?
Thanks in Advance.
Ankit Agarwal
Software Engineer
Know the answer? Post it. Somebody with the same question will find it here.
Former memberPosted Aug 29, 2013, 11:31 PM EDT
string temp = string.Empty; foreach (DataRow item in table.Rows) { foreach(string str in item.ItemArray) { temp = temp + str + "|"; } temp = temp + Environment.NewLine; } System.IO.File.AppendAllText(@"c:\temp.psv",temp);Former memberPosted Aug 29, 2013, 11:33 PM EDT
Note : The table variable should be your data source of the gridview control. Or you can use gridview.DataSource(with type casting).
string temp = string.Empty;
foreach (DataColumn colx in table.Columns)
{
temp = temp + colx.ColumnName + "|";
}
temp = temp + Environment.NewLine;
foreach (DataRow item in table.Rows)
{
foreach(string str in item.ItemArray)
{
temp = temp + str + "|";
}
temp = temp + Environment.NewLine;
}
System.IO.File.AppendAllText(@"c:\temp.psv",temp);
Ankit AgarwalRank #5,528 · 294 pointsPosted Aug 31, 2013, 3:40 AM EDT
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 = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"];
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
// changing the name of active sheet
worksheet.Name = "UUSProducts";
// 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:\\UUSProducts.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// Exit from the application
app.Quit();
but this code successfully run but when i importing this excel file so getting exception: "External table is not in the expected format" i am using .xls format for excel.
So, how can we remove this exception?
Please help me.
Ankit Agarwal
Softeware Engineer
2 short replies
Former memberPosted Aug 29, 2013, 11:21 PM EDT
Ankit AgarwalRank #5,528 · 294 pointsPosted Aug 29, 2013, 11:30 PM EDT