无法使用C#保存Excel文件

时间:2013-08-27 12:35:09

标签: c# excel save

我试图在Excel中修改和保存数据。使用下面的代码,我访问工作表,执行修改,然后保存文件。我无法保存文件。这是我的代码:

Application excel = new Application();
        excel.Visible=true;
        Workbook wb = (Workbook)excel.Workbooks.Open(@"C:\Users\dnyanesh.wagh\Desktop\BookExcel1.xlsx");
        Worksheet ws = (Worksheet)wb.Worksheets[1];
        ws.Cells[1, 1] = "sagar";
        ws.Cells[2, 1] = "sagar";
        ws.Cells[3, 1] = "sagar";
        wb.Save();
        wb.close();

我收到此错误:"文件名为' BookExcel1.xlsx'已存在于此位置。你想替换它吗?"

所以我将代码更改为:

Workbook wb = (Workbook)excel.Workbooks.Open(@"C:\Users\dnyanesh.wagh\Desktop\BookExcel1.xlsx",0, false, 5, "", "",
            false, XlPlatform.xlWindows, "", true, false,
            0, true, false, false););

然后错误是:" BookExcel1.xlsx被user_name.open修改为只读"。如果我点击“取消”'按钮,我收到上面的例外"来自HRESULT的异常:0x800A03EC"

我也尝试过:

wb.SaveAs(@"C:\Users\dnyanesh.wagh\Desktop\BookExcel1.xlsx");
wb.Close(true,null,null);

由此我收到相同的错误,上面的文件显示了修改。

有人可以告诉我如何通过修改保存文件?

3 个答案:

答案 0 :(得分:2)

检查您是否已经运行了Excel.exe进程。 此外,您应该打开工作簿以使其可编辑。

此代码有效:

string txtLocation = Path.GetFullPath(InputFile);

object _missingValue = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application excel = new   Microsoft.Office.Interop.Excel.Application();
Excel.Workbook theWorkbook = excel.Workbooks.Open(txtLocation,
                                                        _missingValue,
                                                        false,
                                                        _missingValue,
                                                        _missingValue,
                                                        _missingValue,
                                                        true,
                                                        _missingValue,
                                                        _missingValue,
                                                        true,
                                                        _missingValue,
                                                        _missingValue,
                                                        _missingValue);

//refresh and calculate to modify
theWorkbook.RefreshAll();
excel.Calculate();
theWorkbook.Save();
theWorkbook.Close(true);
excel.Quit();

答案 1 :(得分:1)

这是因为我尝试打开的文件已在excel应用程序中打开。在我的代码中,我没有关闭某些条件的excel应用程序。这就是它以只读权限打开它的原因。所以我在更新后无法保存该文件。要使用读写打开它,您必须先关闭该文件。之后,您可以打开它并轻松地对文件执行读写操作。

您可以使用以下代码关闭并查看已打开的所有Excel应用程序:

Application excel = (Application)Marshal.GetActiveObject("Excel.Application");
Workbooks wbs = excel.Workbooks;
foreach (Workbook wb in wbs)
{
    Console.WriteLine(wb.Name); // print the name of excel files that are open
    wb.Save();
    wb.Close();
}
excel.Quit();

在顶部,添加以下代码并引用Microsoft.Office.Interop.Excel

using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;

答案 2 :(得分:0)

problem适用于向后兼容的工作表(.xls)而不是.xlsx。

  

要允许在办公室2007版本中打开工作表,它不能   包含超过65k行。您可以检查您的行数   使用ctrl + arrowdown直到你击中底部。如果你试着   得到一个大于该行数的范围,它将产生错误