简体   繁体   English

如何在不重新保存文件的情况下保存 C# 上现有 Excel 文件中的更改?

[英]How to Save Changes in existing Excel File on C# without re saving the file?

worksheet.Cells[1,2] = "MSC"; //Editing the file
        worksheet.Cells[1,1].Style.Font.Bold = true;
        theWorkbook.Save(); //Saving The file it throws Read only file Exception

when i use this piece of code an exception occurs telling me that the file is read only and i have to save a new copy from it and if i use当我使用这段代码时,会发生异常,告诉我该文件是只读的,我必须从中保存一个新副本,如果我使用

theWorkbook.Close(misValue, misValue, misValue);

it pop up a message window to save a new copy from the file and i want to edit the file and save it to the existing file它弹出一条消息 window 从文件中保存新副本,我想编辑文件并将其保存到现有文件中

The post found here provides code to save the workbook in a temporary file and overwrites the original file with that temporary copy. 此处找到的帖子提供了将工作簿保存在临时文件中并使用该临时副本覆盖原始文件的代码。

Relevant snippet:相关片段:

//[...]
string tmpName = Path.GetTempFileName();
File.Delete(tmpName);        
wb.SaveAs(tmpName, missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);
wb.Close(false, missing, missing);
excel.Quit();
File.Delete(Fname);
File.Move(tmpName, Fname);

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

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