Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法保存Excel文件_C#_Winforms_Visual Studio 2010 - Fatal编程技术网

C# 无法保存Excel文件

C# 无法保存Excel文件,c#,winforms,visual-studio-2010,C#,Winforms,Visual Studio 2010,我正在尝试使用窗口窗体生成excel文件。我试图保存excel时出错 来自buttom的方法调用 ExportToExcel(dtResult, "C:\\Excel/test.xls"); 生成Excel的代码 public void ExportToExcel( DataTable Tbl, string ExcelFilePath = null) { try { if (Tbl == nul

我正在尝试使用窗口窗体生成excel文件。我试图保存excel时出错

来自buttom的方法调用

 ExportToExcel(dtResult, "C:\\Excel/test.xls");
生成Excel的代码

public void ExportToExcel( DataTable Tbl, string ExcelFilePath = null)
        {
            try
            {
                if (Tbl == null || Tbl.Columns.Count == 0)
                    throw new Exception("ExportToExcel: Null or empty input table!\n");

                // load excel, and create a new workbook
                Excel.Application excelApp = new Excel.Application();
                excelApp.Workbooks.Add();

                // single worksheet
                Excel._Worksheet workSheet = excelApp.ActiveSheet;

                // column headings
                for (int i = 0; i < Tbl.Columns.Count; i++)
                {
                    workSheet.Cells[1, (i + 1)] = Tbl.Columns[i].ColumnName;
                }

                // rows
                for (int i = 0; i < Tbl.Rows.Count; i++)
                {
                    // to do: format datetime values before printing
                    for (int j = 0; j < Tbl.Columns.Count; j++)
                    {
                        workSheet.Cells[(i + 2), (j + 1)] = Tbl.Rows[i][j];
                    }
                }

                // check fielpath
                if (ExcelFilePath != null && ExcelFilePath != "")
                {
                    try
                    {
                        workSheet.SaveAs(ExcelFilePath); //throws error here
                        excelApp.Quit();
                        MessageBox.Show("Excel file saved!");
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n"
                            + ex.Message);
                    }
                }
                else    // no filepath is given
                {
                    excelApp.Visible = true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("ExportToExcel: \n" + ex.Message);
            }
        }

为了保存文件,您需要确保在文件路径的末尾有一个文件扩展名

此处方法中的文件路径
ExportToExcel(dtResult,“C:\\Excel/test.xls”)不正确
还要确保正确释放excelApp对象

而不是
excelApp.Quit()将其替换为类似的内容

 System.Runtime.InteropServices.Marshal.ReleaseComObject( excelApp ); 

你试过“C:\\Excel”吗?我试过了…它运行得很好,没有错误…但没有在特定路径中保存任何内容..如果在这行工作表上放置断点,精确的文件路径是什么?另存为(ExcelFilePath);?你需要确保你有一个文件路径和一个文件名。。扩展名为..的文件名,如C:\\Excel\test.xls。。?或者不带扩展名…您还需要更改此excelApp.Quit();要使用诸如System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)之类的东西来正确处理/释放Interop Com对象;
 System.Runtime.InteropServices.Marshal.ReleaseComObject( excelApp );