Title

Thursday, 15 January 2015

Exporting excel error : File you are trying to open FileName is in different format than specified extension


I have used following code to export data from dataset to excel file:

 public void ExportRecords(System.Data.DataTable dt, String fileName)   {   try   {   if (dt.Rows.Count > 0)   {   string path = AllinAudio_Temp;   string timestamp = Convert.ToString(DateTime.Now);   timestamp = timestamp.Replace(" ", "");   timestamp = timestamp.Replace("/", "");   timestamp = timestamp.Replace(":", "");   fileName = fileName.Replace(" ", "");     string filename = path + @"\" + timestamp + "_" + fileName + ".xls";   CarlosAg.ExcelXmlWriter.Workbook book = new CarlosAg.ExcelXmlWriter.Workbook();     //// Add a Worksheet with some data   CarlosAg.ExcelXmlWriter.Worksheet sheet = book.Worksheets.Add("Sheet1");   WorksheetStyle style = book.Styles.Add("HeaderStyle");   style.Font.Bold = true;   WorksheetStyle style1 = book.Styles.Add("HeaderStyle1");   style1.Font.Bold = true;   style1.Font.Color = "Red";     WorksheetRow row;// = sheet.Table.Rows.Add();     row = sheet.Table.Rows.Add();   foreach (DataColumn column in dt.Columns)   {   row.Cells.Add(new WorksheetCell(column.ColumnName, "HeaderStyle"));   }           for (int i = 0; i < dt.Rows.Count; i++)   {   row = sheet.Table.Rows.Add();     for (int l = 0; l < dt.Columns.Count; l++)   {   row.Cells.Add(dt.Rows[i][l].ToString());       }       }     book.Save(filename);   Response.ContentType = "text/excel";   Response.AppendHeader("Content-Disposition", "attachment; filename=" + timestamp + "_" + fileName + ".xls");   Response.TransmitFile(filename);   Response.End();       }   }   catch (Exception)   {     throw;   }   }

When the file gets exported and we try to open it, it gives me message:

enter image description here

I want the file in xls format only and i am also exxporting it in xls format.

But why this kind of error comes when i try to open it.

When i tried with XLSX

enter image description here

Answer

The biggest hint is in the name of the component you're using to create the files. ExcelXmlWriter appears to write files in the Excel XML format, which should have the file extension XML. When you use the XLS extension, Excel is expecting the content to be in the older format. When it fails to open the file as a classic Excel file it tries the alternatives, finds that the Excel XML works and warns you of the problem.

Solution: change your output code to use the file extension XML instead of XLS.

(Not XLSX as previously stated.)

Answer2

try this function

 public static void ExportToExcelOnece(DataSet dsExcel, string ExcelFilePath)   {     if (dsExcel == null || dsExcel.Tables.Count == 0)   throw new Exception("ExportToExcel: Null or empty input table!\n");     Excel.Application excelApp = new Excel.Application();   excelApp.Workbooks.Add();   Excel.Worksheet newWorksheet;   Excel.Worksheet objSheet = (Excel.Worksheet)excelApp.ActiveWorkbook.Sheets["Sheet1"];     for (int m = 0; m <= dsExcel.Tables.Count - 1; m++)   {   newWorksheet = (Excel.Worksheet)excelApp.Worksheets.Add(objSheet, Missing.Value, Missing.Value, Missing.Value);   newWorksheet.Name = dsExcel.Tables[m].TableName;     // column headings   for (int i = 0; i < dsExcel.Tables[m].Columns.Count; i++)   {   newWorksheet.Cells[1, (i + 1)] = dsExcel.Tables[m].Columns[i].ColumnName;   }     // rows   for (int i = 0; i < dsExcel.Tables[m].Rows.Count; i++)   {   for (int j = 0; j < dsExcel.Tables[m].Columns.Count; j++)   {   newWorksheet.Cells[(i + 2), (j + 1)] = dsExcel.Tables[m].Rows[i][j];   }   }   }     ((Excel.Worksheet)excelApp.ActiveWorkbook.Sheets[1]).Activate();     objSheet.Delete();   objSheet = (Excel.Worksheet)excelApp.ActiveWorkbook.Sheets["Sheet2"];   objSheet.Delete();   objSheet = (Excel.Worksheet)excelApp.ActiveWorkbook.Sheets["Sheet3"];   objSheet.Delete();     // check fielpath   if (ExcelFilePath != null && ExcelFilePath != "")   {   try   {   excelApp.ActiveWorkbook.SaveAs(ExcelFilePath);   excelApp.Quit();   MessageBox.Show("Excel file saved!");   }   catch (Exception ex)   {   throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n"   + ex.Message);   }   }   else // no filepath is given   {   excelApp.Visible = true;   }       //Quit the Application.   excelApp.Quit();     }

No comments:

Post a Comment