c#ExportToExcel创建损坏的xlsx文件

c#ExportToExcel创建损坏的xlsx文件,c#,excel,datagridview,C#,Excel,Datagridview,我正在使用以下库: 这是我的密码: private void exportExcelBtn_Click(object sender, EventArgs e) { if (dataGridView1.Rows.Count == 0) { MessageBox.Show("There's nothing to export", "Info", MessageBoxButtons.OK); return; } DataTable s

我正在使用以下库:

这是我的密码:

private void exportExcelBtn_Click(object sender, EventArgs e)
{
    if (dataGridView1.Rows.Count == 0)
    {
        MessageBox.Show("There's nothing to export", "Info", MessageBoxButtons.OK);
        return;
    }

    DataTable save = Utilities.DataGridView2DataTable(dataGridView1, "TestingReport");

    string outputFile = Utilities.saveDoc(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), save);
    if (outputFile != null)
    {
        DialogResult opt = MessageBox.Show("Export successful. Would you like to view the file?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
        if (opt == DialogResult.Yes)
        {
            System.Diagnostics.Process.Start(outputFile);
        }
    }
}
我在两个不同的项目中使用相同的代码,但是在一个项目中,它创建的excel文件已损坏/无法读取

提取.xlsx文件的内容时,我注意到的第一件事是工作项目中存在的
[Content\u Types].xml
文件不在不工作项目的输出中。此外,创建的workbook.xml文件都是0kb,而工作项目文件中实际上包含数据

我尝试过使用
控制台.WriteLine

我还使用此代码将我的
DataGridView
转换为
DataTable
,以便将其写入excel:

我已经检查并添加了
Console.WriteLine(…)
,以验证数据实际上是从这个函数返回的,并且一切看起来都很好

编辑:


我注意到的唯一区别是,当我单击按钮保存excel,然后选择不查看它时,当我去解压excel文档时,返回有效excel文档的应用程序允许我解压它,但是当我尝试解压不起作用的文档时,它说文件当前已打开

如果您的第一列标题名为“id”,则可能会导致问题。您可以尝试在任何名为“id”的列前面加“`”

问题是我在
CreateExcelFile.cs


这与完成StreamWriter后不关闭它,然后因此丢失一堆文本具有相同的效果。我更改了这段代码b/c没有WindowsBase.dll它就无法工作。在引用该DLL并更改代码后,我能够成功地将我的datagridview导出到excel

我也使用datagridview做了类似的事情

首先,我从我需要的列中获取值(在我的例子中,它只有1列)


您必须使用Microsoft.Office.Interop.Excel引用。

尝试刷新并关闭HttpResponse请求。这对我有用

response.OutputStream.Flush();
response.OutputStream.Close();
response.Flush();
response.Close();
private void export()
        {
            Microsoft.Office.Interop.Excel.Application oXL;
            Microsoft.Office.Interop.Excel._Workbook oWB;
            Microsoft.Office.Interop.Excel._Worksheet oSheet;
            object misvalue = System.Reflection.Missing.Value;

          try
          {
              oXL = new Microsoft.Office.Interop.Excel.Application();
              oXL.Visible = true;
              oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Open(libro));
              oWB.Sheets[x].Select(); 
              //x is the number of the sheet where you want to save data.
              oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet;

//This goes into a for
oSheet.get_Range(tmp, misvalue).Value2 = values[x]; //tmp is a string, which contains letter and number of the cell "A1". misvalue is an object variable
    oWB.Save();
                    oWB.Close();
                    oXL.Visible = false;
                    oXL.UserControl = false;
                    oXL.Application.Quit();
    }
    catch(Exception exc){}
    }
response.OutputStream.Flush();
response.OutputStream.Close();
response.Flush();
response.Close();