How to prompt user to save an automated Excel file

左心房为你撑大大i 提交于 2019-12-22 09:40:33

问题


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

回答1:


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

Here's an example:

  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.




回答2:


This is the easy way. :-)

myFilename = xlapp.GetSaveAsFilename
xlWorkbook.SaveAs filename:=myFilename



回答3:


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

    {

        SaveFileDialog sfd = new SaveFileDialog();// Create save the CSV
        sfd.Filter = "Text File|*.xls";// filters for text files only
        sfd.DefaultExt = "xls";
        sfd.AddExtension = true;
        sfd.FileName = "AutodeskScripts.xls";
        sfd.Title = "Save Excel File";
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            return sfd.FileName;
        }
        else
        {
            return null;
        }


来源:https://stackoverflow.com/questions/9369535/how-to-prompt-user-to-save-an-automated-excel-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!