我在c#中创建了一个代码,用于创建和保存excel文件。代码可以成功创建并保存excel文件,但是当我打开创建的excel文件时,它会显示一条警告消息,告诉“文件格式和'filename.xls'的扩展名不匹配,文件可能已损坏或不安全。除非你信任它的来源,否则不要打开它,你还想打开它吗?“我正在使用以下代码:如何修复文件格式和扩展名不符?
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();
}
}
我应该怎么做才能避免接收到该警告消息?
可你刚才使用CSV格式? – chouaib 2015-02-09 05:59:43
EXCEL文件的每种风格都有FormatCode值 - 使用这些值,您不必担心客户端安装了哪个版本。 – 2015-02-09 06:16:02
这是什么:'ExcelApp.Application.Workbooks.Add(Type.Missing);'? – DrKoch 2015-02-09 06:34:14