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?
source share