Edit a excel file while Exporting data from ms access to other excel file using c# without error occur

48 views Asked by At

i wrote a program to export data from MS access to excel. This working properly. But there is some error occur while editing or open any other excel files. How to solve this error.

" Exception from HRESULT: 0x800AC472 "

private void WriteExcel(System.Data.DataTable DT, string FileName)
    {
        Microsoft.Office.Interop.Excel.ApplicationClass excel = new ApplicationClass();
        Microsoft.Office.Interop.Excel.Workbook WB = excel.Workbooks.Add(true);
        Microsoft.Office.Interop.Excel.Worksheet WSht;
        int ColId = 0;
        int RowId = 0;
        WSht = (Microsoft.Office.Interop.Excel.Worksheet)WB.Sheets[1];
        Range Rng = WSht.get_Range("A1", "IV" + (DT.Rows.Count + 1).ToString());
        Rng.NumberFormat = "@";
        //Fill Column Heading
        foreach (DataColumn Col in DT.Columns)
        {
            ColId++;         
            excel.Cells[1, ColId] = Col.ColumnName;                }
        }

        //Fill Row Values
        foreach (DataRow DR in DT.Rows)
        {
            RowId++;
            ColId = 0;
            foreach (DataColumn Col in DT.Columns)
            {
                ColId++;
                excel.Cells[RowId + 1, ColId] = DR[Col.ColumnName];                    
            }
        }
        WSht.Activate();
        int Rcount = WSht.UsedRange.Rows.Count;
        Rng = WSht.get_Range("D1", "IV" + Rcount.ToString());
        Rng.NumberFormat = "@";
        WB.SaveAs(FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, "", "", false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, false, true, true, true, 1);
        excel.Quit();
    }
0

There are 0 answers