C#与Excel互操作问题,保存excel文件不流畅

发布于 2024-07-18 02:58:33 字数 292 浏览 9 评论 0原文

我可以打开并写入 Excel 文件,但是当我尝试通过传递路径来保存文件时,保存操作会提示“保存”对话框。 我期望它能够完全将文件保存在指定路径下,

代码如下:

excelApp.Save(exportToDirectory);
excelApp.Quit();

其中,exportToDirectory 是:“C:\files\strings.xlsx”。

PS:我已经检查过excel版本和类似问题。

谢谢

I could Open and Write to the excel file, but when I try to save the file by passing a path to it, the save operation prompts with the Save dialog. I was expecting it to quitely Save the file at the specified path

The code is as below:

excelApp.Save(exportToDirectory);
excelApp.Quit();

where, exportToDirectory is: "C:\files\strings.xlsx".

PS: I have already checked with the excel version and similar issue.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

月朦胧 2024-07-25 02:58:33

您需要使用 Workbook.SaveAs 而不是 Application.Save

Excel.Application app = new Excel.Application();
Excel.Workbook wb = app.Workbooks.Add(missing);
...
wb.SaveAs(@"C:\temp\test.xlsx", missing, missing, missing, missing,
          missing, Excel.XlSaveAsAccessMode.xlExclusive,
          missing, missing, missing, missing, missing);

You need to use Workbook.SaveAs instead of Application.Save:

Excel.Application app = new Excel.Application();
Excel.Workbook wb = app.Workbooks.Add(missing);
...
wb.SaveAs(@"C:\temp\test.xlsx", missing, missing, missing, missing,
          missing, Excel.XlSaveAsAccessMode.xlExclusive,
          missing, missing, missing, missing, missing);
平定天下 2024-07-25 02:58:33

设置以下属性也可能有帮助:

excelApp.DisplayAlerts = false;
excelApp.ScreenUpdating = false;
excelApp.Visible = false;
excelApp.UserControl = false;
excelApp.Interactive = false;

Setting the following properties might also help:

excelApp.DisplayAlerts = false;
excelApp.ScreenUpdating = false;
excelApp.Visible = false;
excelApp.UserControl = false;
excelApp.Interactive = false;
灵芸 2024-07-25 02:58:33

只需将ActiveWorkbook.Saved属性设置为true,您就可以在没有任何对话框的情况下退出();

just set the ActiveWorkbook.Saved property to true and you can Quit() without any Dialog box;

红尘作伴 2024-07-25 02:58:33

好吧,Microsoft 是这样做的:

// Save the Workbook and quit Excel.
m_objBook.SaveAs(m_strSampleFolder + "Book1.xls", m_objOpt, m_objOpt, 
    m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange, 
    m_objOpt, m_objOpt, m_objOpt, m_objOpt);
m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();

请参阅他们的一篇知识库文章

Well, here's how Microsoft does it:

// Save the Workbook and quit Excel.
m_objBook.SaveAs(m_strSampleFolder + "Book1.xls", m_objOpt, m_objOpt, 
    m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange, 
    m_objOpt, m_objOpt, m_objOpt, m_objOpt);
m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();

See one of their KB articles.

ぶ宁プ宁ぶ 2024-07-25 02:58:33

ExcelApp.Interactive = false 禁止显示任何对话框。

excelApp.ActiveWorkbook.SaveAs(exportDirectory)

ExcelApp.Interactive = false suppresses any dialog box.

excelApp.ActiveWorkbook.SaveAs(exportDirectory)

淡忘如思 2024-07-25 02:58:33

我发现 excelApp.ActiveWorkbook.save() 可以保存文件,然后我可以退出而无需询问。

I found excelApp.ActiveWorkbook.save() which can save the file, then I can quit without for asking.

瞎闹 2024-07-25 02:58:33

myBook.Saved = true;
myBook.SaveCopyAs(xlsFileName);
myBook.Close(null, null, null);
myExcel.Workbooks.Close();
myExcel.Quit();

myBook.Saved = true;
myBook.SaveCopyAs(xlsFileName);
myBook.Close(null, null, null);
myExcel.Workbooks.Close();
myExcel.Quit();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文