简体   繁体   English

无法使用C#保存Excel文件

[英]Unable to Save Excel File Using C#

I'm trying to modify and save data in Excel. 我正在尝试修改和保存Excel中的数据。 With the code below, I'm accessing the sheet, performing modifications, then saving the file. 使用下面的代码,我正在访问工作表,执行修改,然后保存文件。 I'm unable to save the file. 我无法保存文件。 Here is my code: 这是我的代码:

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();

I'm receiving this error: "the file named 'BookExcel1.xlsx' already exists in this location. Do you want to replace it?" 我收到此错误:“名为'BookExcel1.xlsx'的文件已存在于此位置。您要替换它吗?”

So I changed the code to: 所以我将代码更改为:

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););

Then error is: "BookExcel1.xlsx is being modified by user_name.open as read only". 然后错误是:“BookExcel1.xlsx被user_name.open修改为只读”。 If I click the 'cancel' button, I receive the exception above with "Exception from HRESULT: 0x800A03EC" 如果我单击“取消”按钮,我会收到上面的例外“HRESULT异常:0x800A03EC”

I have also tried: 我也尝试过:

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

From that I receive the same error, with the above file showing the modifications. 从那里我收到相同的错误,上面的文件显示修改。

Can anybody tell me how can I save the file with modifications? 任何人都可以告诉我如何保存文件并进行修改?

Check that you don't already have an Excel.exe process runnning. 检查您是否已经运行Excel.exe进程。 Also, you should open the workbook so that it's editable. 此外,您应该打开工作簿,以便它是可编辑的。

This code works: 此代码有效:

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();

This happens because the file I was trying to open was already open in excel application. 发生这种情况是因为我尝试打开的文件已在excel应用程序中打开。 In my code, I was not closing the excel application for some conditions. 在我的代码中,我没有关闭某些条件的excel应用程序。 That's why it was opening it with read only permissions. 这就是它以只读权限打开它的原因。 So I was not able to save that file after updating. 所以我在更新后无法保存该文件。 To open it with read-write, you have to first close that file. 要使用读写打开它,您必须先关闭该文件。 After that, you could open it and easily perform read and write operations on the file. 之后,您可以打开它并轻松地对文件执行读写操作。

You can close and see all the excel applications which are already open by using following code: 您可以使用以下代码关闭并查看已打开的所有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();

At the top, add the following code and make reference to Microsoft.Office.Interop.Excel 在顶部,添加以下代码并引用Microsoft.Office.Interop.Excel

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

This problem is for backwards compatible sheet (a .xls) instead of a .xlsx. 问题适用于向后兼容的工作表(.xls)而不是.xlsx。

To allow sheets to be opened in pre office 2007 version it can't contain more than 65k rows. 要允许在Office 2007版本中打开工作表,它不能包含超过65k行。 You can check the number of rows in your sheet by using ctrl+arrowdown till you hit the bottom. 您可以使用ctrl + arrowdown检查工作表中的行数,直到您触及底部。 If you try to get a range larger than that number of rows it will create an error 如果您尝试获得大于该行数的范围,则会产生错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM