如何提示用户保存自动 Excel 文件
我已经搜索过,但我发现没有任何内容直接符合我要查找的内容(或者也许我的搜索没有符合单词组合)。
在 C# 中,我使用 Interop.Excel 创建了一个 Excel 工作表,在其中插入一些数据并创建一个图表。当我执行 xlWorkBook.SaveAs 时,这一切都工作正常。
我想要做的是提示用户将自动化工作簿放在具有自己的文件名的位置。我已经尝试过(http://p2p.wrox .com/vb-how/63900-disabling-second-excel-save-prompt.html),他基本上在其中创建了一个新的 SaveFileDialog,然后如果它 == OK,他就会构建他的 Excel然后他说他的 workbook.SaveAs(FilePathFromSaveAsDialog) 导致出现提示。当我尝试时,出现“当应用程序未在 UserInteractive 模式下运行时显示模式对话框不是有效操作”错误。 我会粘贴我的所有代码,但它位于一个单独的系统上,但其目的是:
using Excel = Microsoft.Office.Interop.Office
//....then on click of link button....
Excel.Application xlApp;
Excel.Workbook xlWorbook;
Excel.Workbooks xlWorkbooks;
Excel.Sheets xlSheets;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBooks = xlApp.Workbooks;
xlWorkBook = xlWorbooks.Add(misValue);
xlSheets = xlWorkBook.Worksheets;
xlWorkSheet = (Excel.Worksheet)xlSheets.get_Item(1);
//....Now I fill my Excel sheet data and make my chart >>> then I close like below...
xlApp.DisplayAlerts = true;
//HERE IS WHERE I WANT TO EITHER PASS THE PATH AND FILE NAME FROM USER OR USE A PROMPT
xlWorkBook.SaveAs("Test.xls", Excel.XFileFormat.XlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
//finally I release all my objects via Marshal.ReleaseComObject then GC.Collect
I have searched and nothing that I have found hits directly on what I am looking for (or maybe my searches have not hit on the combo of words).
In C# I have created an Excel sheet, using Interop.Excel, in which I insert some data and create a chart. This all works fine when I do a xlWorkBook.SaveAs.
What I want to do is prompt the user to put the automated workbook somewhere with thier own file name. I have tried (http://p2p.wrox.com/vb-how/63900-disabling-second-excel-save-prompt.html) where he basically does a new SaveFileDialog then if its == OK he builds his Excel sheet then he says that his workbook.SaveAs(FilePathFromSaveAsDialog) causes a prompt. When I try it, I get the "Showing modal dialog box when application is not running in UserInteractive mode is not a valid operation" error.
I would paste all my code but it is on a seperate system, however the just of it is:
using Excel = Microsoft.Office.Interop.Office
//....then on click of link button....
Excel.Application xlApp;
Excel.Workbook xlWorbook;
Excel.Workbooks xlWorkbooks;
Excel.Sheets xlSheets;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBooks = xlApp.Workbooks;
xlWorkBook = xlWorbooks.Add(misValue);
xlSheets = xlWorkBook.Worksheets;
xlWorkSheet = (Excel.Worksheet)xlSheets.get_Item(1);
//....Now I fill my Excel sheet data and make my chart >>> then I close like below...
xlApp.DisplayAlerts = true;
//HERE IS WHERE I WANT TO EITHER PASS THE PATH AND FILE NAME FROM USER OR USE A PROMPT
xlWorkBook.SaveAs("Test.xls", Excel.XFileFormat.XlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
//finally I release all my objects via Marshal.ReleaseComObject then GC.Collect
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是最简单的方法。 :-)
This is the easy way. :-)
如果您在保存文件后将其存储在某处,则可以使用 Response.
下面是一个示例:
这应该提示用户要将文件存储在计算机上的位置。
If you have the file stored somewhere after you save it you can use Response.
Here's an example:
This should prompt the user where they want to store the file on their machine.
这就是我所做的,看起来很疯狂,但却有效。
xlWorkBook.SaveAs(saveExcel(), Excel.XlFileFormat.xlWorkbookNormal,misValue,misValue,misValue,misValue,Excel.XlSaveAsAccessMode.xlExclusive,misValue,misValue,misValue,misValue,misValue);//导入方法
内部对象saveExcel()//方法
This is what I did, it seems crazy, but worked.
xlWorkBook.SaveAs(saveExcel(), Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);//import method
internal object saveExcel()//method