我在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();
}
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做以避免收到该警告信息?
小智 6
我知道此问题现在可能已解决,但是仅尝试在不修改代码的情况下为您提供帮助,仍然可以在您的格式中使用.xls格式,并在通过设置注册表打开文件时取消显示此警告。打开reg编辑,导航至HKEY_CURRENT_USER\Software\Microsoft\Office\14\Excel\Security
创建一个DWord具有名称ExtensionHardening并将值设置为0。
这可能会使您的系统容易受到攻击,但是在组织网络中工作时并没有什么大不了的,至少在您确定要下载文档和源代码的类型时才是最重要的。