1
votes

Is there any possibility to create and open XLSX (not macro) file generated by EPPlus with forced show "Save as dialog" before closing Excel?

My scenario:
I create XLSX file with EPPlus and store it to temporary folder of my application. Then I immediately call this file to run (Excel will open and my file is loaded). Without any changes I close Excel - and in this moment I want to force dialog "Save as" to save this file into other directory (not my temporary). Can you force display this dialog by any property of file (or other way)?

Solved for XLSM:
If someone of you need show this "Save as" dialog for Excel macro file (XLSM) using EPPlus, this is the way:

OfficeOpenXml.ExcelPackage Package;
  Package.Workbook.CreateVBAProject();
  OfficeOpenXml.VBA.ExcelVBAModule excelVbaModule =
    Package.Workbook.VbaProject.Modules.AddModule("Module1");
  StringBuilder mac = new StringBuilder();
  mac.AppendLine("Sub Auto_Close()");
  mac.AppendLine("Application.Dialogs(xlDialogSaveAs).Show");
  mac.AppendLine("End Sub" );
  excelVbaModule.Code = mac.ToString();
Package.Save()

Can you help me with not macro file? (But maybe it is not possible)
Thanks.

What I did based on your responses?
I try using SaveAs (instead of the original .Save()) from EPPlus:

System.IO.FileInfo fileInfo = new System.IO.FileInfo(targetTemporaryFile);
if (fileInfo.Exists) { fileInfo.Delete(); }
Package.SaveAs(fileInfo);

But no dialog was displayed.

1
for which you want to do it? windows or web applicationRam Singh
it is for windows applicationAtiris
there is saveas fucntion comes you can use thatRam Singh
@raman this approach not work for me. I try rewrite it as show above in question. But dialog to save did not appear.Atiris
Have you considered opening that dialog box to prompt for a filename first? Then with that filename, you generate the Excel file with EPPlus. Then you just save directly to the folder that the user wanted.Vincent Tan

1 Answers

2
votes

There is a way to flag the FileInfo object corresponding to the Package as Read Only - myFile.IsReadOnly = True

In this way if the user tries to just save the file, they will be given the Save As.. dialog. I use this on web pages for our intranet by saving the file then handing the user a link to the FileInfo.FullName property