The below code exporting the data from dataset to excel sheet and it will open as Book1.xls but i want to save in my local drive instead of opening. but i am getting below error when i try to save the file
I am getting error like "System.Runtime.InteropServices.COMException(0x800A03EC):Exception from HRESULT:0x800A03EC at Microsoft.Office.Interop.Excel.WorkbookClasss.SaveAs" when i am trying to save excel file in my local machine using below code .
private void GenerateReport(DataSet ds)
{
Excel.Application objApp;
Excel._Workbook objBook;
Excel.Workbooks objBooks;
Excel.Sheets objSheets;
Excel._Worksheet objSheet;
try
{
objApp = new Excel.Application();
objBooks = objApp.Workbooks;
objBook = objBooks.Add(System.Reflection.Missing.Value);
objSheets = objBook.Worksheets;
objSheet = (Excel._Worksheet)objSheets.get_Item(1);
Excel.Range range;
DataTable table = ds.Tables[0];
int ColumnIndex = 0;
foreach (DataColumn col in table.Columns)
{
ColumnIndex++;
switch (col.ColumnName)
{
case "PRINCIPAL": objSheet.Cells[2, ColumnIndex] = "Principal"; break;
case "EVENTGROUP": objSheet.Cells[2, ColumnIndex] = "Eventgroup"; break;
}
}
objSheet.Cells[1, 1] = "Sentry Month End Report.";
range = objSheet.get_Range("A1", "I2");
range.Font.Bold = true;
range.Columns.AutoFit();
int rowIndex = 1;
foreach (DataRow row in table.Rows)
{
rowIndex++;
ColumnIndex = 0;
foreach (DataColumn col in table.Columns)
{
ColumnIndex++;
objSheet.Cells[rowIndex + 1, ColumnIndex] = row[col.ColumnName];
}
}
objSheet.Columns.AutoFit();
//objApp.Visible = true;
objBook.SaveAs("C:\\SentryReport.xls", "xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
}
finally
{
objApp = null;
objBook = null;
objBooks = null;
objSheets = null;
objSheet = null;
GC.Collect();
}
}
Please any one help me where i making mistake in code
Thanks in Advance..