I am using a datagridview in my application and I have filled it with dataset which returns two columns ID and Name, but in gridview I'm displaying the name column alone hiding the ID column. Is it possible to export the rows alone from datagridview to excel. Eg If the follow. names appear as each row,
A
B
C
D
My excel should be in a format that each row must display as a column
A B C D
Code Which I have tried for export
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Excel files |*.xls|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.CreatePrompt = true;
saveFileDialog1.Title = "Export Excel File To";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
worksheet.Name = "Exported from Data Template";
for (int i = 0; i < dgDtParameters.Rows.Count - 1; i++)
{
for (int j = 0; j < dgDtParameters.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dgDtParameters.Rows[i].Cells[j].Value.ToString();
}
}
fileName = saveFileDialog1.FileName;
workbook.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
MessageBox.Show("Data template exported successfully","Data Template Export);
}
else
return;
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
app.Quit();
workbook = null;
app = null;
GC.Collect();
}