How to fix file format and extension do not match?

I created code in C # that creates and saves an excel file. The code can successfully create and save the excel file, but when I open the excel file, it displays a warning message with the message "File format and extension" filename.xls "do not match. The file may be damaged or unsafe. If you do not trust its source, do not open it. Do you still want to open it? " I am using the following code:

private void button1_Click(object sender, EventArgs e) { saveFileDialogSummary.Filter = "Excel Flie|*.xls"; saveFileDialogSummary.FilterIndex = 0; saveFileDialogSummary.RestoreDirectory = true; saveFileDialogSummary.CreatePrompt = true; saveFileDialogSummary.Title = "Export Excel File To"; Excel.Application ExcelApp = new Excel.Application(); ExcelApp.Application.Workbooks.Add(Type.Missing); ExcelApp.Columns.ColumnWidth = 30; for (int i = 0; i < dataGridViewSummary.Rows.Count; i++) { DataGridViewRow row = dataGridViewSummary.Rows[i]; for (int j = 0; j < row.Cells.Count; j++) { ExcelApp.Cells[i + 1, j + 1] = row.Cells[j].ToString(); } } DialogResult res = saveFileDialogSummary.ShowDialog(); if(res == DialogResult.OK){ ExcelApp.ActiveWorkbook.SaveCopyAs(saveFileDialogSummary.FileName); ExcelApp.ActiveWorkbook.Saved = true; ExcelApp.Quit(); } } 

What to do to not receive this warning?

+6
source share
4 answers

Just change .xls to .xlsx if you have the latest office installed.

+2
source

I know that this problem can be solved by now, but just trying to help you without changing the code can still use the .xls format in yours and suppress this warning by opening the file by installing the registry. Open reg reg, go to HKEY_CURRENT_USER\Software\Microsoft\Office\14\Excel\Security

Create a DWord named ExtensionHardening and set the value to 0.

This can lead to the vulnerability of your system, but it is not very important when working in the organizationโ€™s network, at least when you are confident in downloading the document type and source.

+1
source

The solution "file format and extension do not match" is to close the workbook ** ($ workbook-> close;) **, finally, after all the necessary entries are made to the file.

I encountered the same problem when opening the "XLS" file from mail. I created a file and pasted all my init stuff and without closing the book, I send mail as an attachment. Later I realized that I needed to close the book and send it as an application.

0
source

The .xls and .xlsx file extensions contain a different layout. the .xls extension is used in version 2003, while the .xlsx version extension will be used.
You must export the excel file to the .xlsx format. It will be supported in all versions as I used.

Add below dlls to bin folder
1. ClosedXML.dll
2. DocumentFormat.OpenXml.dll

Code for export to .xlsx

  DataTable dt = new DataTable(); //Create column and inser rows using (XLWorkbook wb = new XLWorkbook()) { var ws = wb.Worksheets.Add(dt, Sheetname); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.Charset = ""; HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + p_FileName + ".xlsx"); using (MemoryStream MyMemoryStream = new MemoryStream()) { wb.SaveAs(MyMemoryStream); MyMemoryStream.WriteTo(HttpContext.Current.Response.OutputStream); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); } } 
0
source

Source: https://habr.com/ru/post/982187/


All Articles