0
 Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
        Workbook wb = Excel.Workbooks.Add(XlSheetType.xlWorksheet);
        Worksheet ws = (Worksheet)Excel.ActiveSheet;
        Excel.Visible = true;

        ws.Cells[1, 1] = "Tarih";
        ws.Cells[1, 2] = "Kasiyer";
        ws.Cells[1, 3] = "Ucret";
        ws.Cells[1, 4] = "Bilet No";
        ws.Cells[1, 5] = "Firma Adı";
        for (int j = 2; j < dataGridView1.Rows.Count; j++)
        {
            for (int i = 2; i <= 5; i++)
            {
                ws.Cells[j,i]=dataGridView1.Rows[j-2].Cells[i-1].Value;
            }
        }

Dates are not coming to First Cell in Excel . How can i do that ? please help me ? And also i have got a warning like "HRESULT özel durum döndürdü: 0x800AC472"

1 Answers1

0

You can't probably take date value, it returns null and gives an error. Just for date column, you try to convert.

for (int i = 1; i < dataGridView1.Rows.Count; i++)
    {
        ws.Cells[i,0] = (Convert.ToDateTime(ws.Cells[i,0]).ToOADate();
    }

Additionaly you can try that too.

Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
range = range.get_Resize(rowCount, columnCount);
range.EntireColumn.NumberFormat = "DD/MM/YYYY";
nevra
  • 408
  • 1
  • 7
  • 20