如何提示用户保存自动Excel文件 [英] How to prompt user to save an automated Excel file

查看:143
本文介绍了如何提示用户保存自动Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了,但是没有找到任何东西直接打到我要寻找的东西上(或者我的搜索没有打到单词组合上).

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

在C#中,我使用Interop.Excel创建了一个Excel工作表,在其中插入了一些数据并创建了图表.当我执行xlWorkBook.SaveAs时,一切正常.

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.

我想做的是提示用户将自动化工作簿放置在具有自己文件名的位置.我已经尝试过( http://p2p.wrox .com/vb-how/63900-disabling-second-excel-save-prompt.html ),在那里,他基本上做了一个新的SaveFileDialog,然后如果它== OK,他建立了他的Excel工作表,然后他说他的工作簿. SaveAs(FilePathFromSaveAsDialog)引起提示.尝试时,出现当应用程序不在UserInteractive模式下运行时显示模式对话框不是有效操作"错误. 我将粘贴所有代码,但是它在单独的系统上,但是唯一的是:

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

推荐答案

如果文件保存后存储在某处,则可以使用Response.

If you have the file stored somewhere after you save it you can use Response.

这是一个例子:

  const string fName = @"C:\Test.xls";
  FileInfo fi = new FileInfo(fName);
  long sz = fi.Length;

  Response.ClearContent();
  Response.ContentType = Path.GetExtension(fName);
  Response.AddHeader("Content-Disposition", string.Format("attachment; filename = {0}",System.IO.Path.GetFileName(fName)));
  Response.AddHeader("Content-Length", sz.ToString("F0"));
  Response.TransmitFile(fName);
  Response.End();

这应该提示用户他们要在计算机上存储文件的位置.

This should prompt the user where they want to store the file on their machine.

这篇关于如何提示用户保存自动Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆