如何修复文件格式和扩展名不匹配? [英] How to fix file format and extension don't match?

查看:2327
本文介绍了如何修复文件格式和扩展名不匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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 =导出Excel文件到;

Excel.Application ExcelApp = new Excel.Application();
ExcelApp.Application.Workbooks.Add(Type.Missing);
ExcelApp.Columns.ColumnWidth = 30; (int i = 0; i< dataGridViewSummary.Rows.Count; i ++)
{
DataGridViewRow row = dataGridViewSummary.Rows [i];
(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();
}
}

我应该怎么做,以避免收到该警告消息?

解决方案

如果您安装了最新的办公室,只需将.xls更改为.xlsx。


I created a code in c# which creates and saves excel file. The code can successfully create and save excel file, but when I open the excel file created, it displays a warning message telling "The file format and extension of 'filename.xls' don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?". 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 should I do to avoid receiving that warning message?

解决方案

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

这篇关于如何修复文件格式和扩展名不匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆