i m using this code for exporting data from datagridview to excel . but one problem is here, the date value is not inserted into excel ,it display in excel like ######. how resolve it?plz help?
private void button2_Click(object sender, EventArgs e)
{
// export data into excel shhet
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
// int i = 0;
//int j = 0;
for (int i = 1; i < displaygrid.Columns.Count + 1; i++)
{
xlWorkSheet.Cells[1, i] = displaygrid.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < displaygrid.Rows.Count - 1; i++)
{
for (int j = 0; j < displaygrid.Columns.Count; j++)
{
xlWorkSheet.Cells[i + 2, j + 1] = displaygrid.Rows[i].Cells[j].Value.ToString();
}
}
xlWorkBook.SaveAs("students.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Excel file created , you can find the file d:\\students.xls");
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}