Using EPPlus, it's easy to save a document to a predefined path, but I need to save to diferent paths everytime, so I need EPPlus to show the standard save dialog.
My code can create an Excel file from a datatable, formats it a little bit, and saves it on the specified location:
DirectoryInfo outputDir = new DirectoryInfo(@"C:\Users\user001\Downloads");
FileInfo newFile = new FileInfo(outputDir.FullName + @"\Crit.xlsx");
if (newFile.Exists) {newFile.Delete();}
using (ExcelPackage pck = new ExcelPackage(newFile))
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Crit");
ws.Cells["A1"].LoadFromDataTable(dtCrit, true);
ws.Cells.AutoFitColumns(0);
ws.Cells["A1:Z1"].Style.Border.Top.Style = ExcelBorderStyle.Thin;
ws.Cells["A1:Z1"].Style.Font.Bold = true;
pck.Save();
}
How can I show the dialogbox to manually chose a destination folder? I tried with pck.SaveAs, but I can't make it work, and there is not much information about this...
UPDATE: The App works when executed from inside the Project, or from the server. Crashes if executed with a shourcut or copy/pasting the exe to my desktop.
string mydocpath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
try {
DirectoryInfo outputDir = new DirectoryInfo(mydocpath);
FileInfo newFile = new FileInfo(outputDir.FullName + @"\Requerimientos_" + DateTime.Now.ToString("dd-MM-yyyy_hh-mm") + ".xlsx");
if (newFile.Exists) { newFile.Delete(); }
using (ExcelPackage pck = new ExcelPackage(newFile))
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Requerimientos");
ws.Cells["A1"].LoadFromDataTable(dtReque, true);
ws.Cells.AutoFitColumns(0);
ws.Cells["A1:Z1"].Style.Border.Top.Style = ExcelBorderStyle.Thin;
ws.Cells["A1:Z1"].Style.Font.Bold = true;
var dlg = new SaveFileDialog { FileName = "Requerimientos_" + DateTime.Now.ToString("dd-MM-yyyy_hh-mm"), DefaultExt = ".xlsx", Filter = "Excel Sheet (.xlsx)|*.xlsx", InitialDirectory = mydocpath };
var result = dlg.ShowDialog();
if (result == true)
{
using (var stream = dlg.OpenFile())
{
pck.SaveAs(stream);
OpenDialog("File Created", "Export");
}
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
WPF provides the standard file dialogs through the Microsoft.Win32
namespace. You can use SaveFileDialog to display a dialog box and select or create a file to save to.
Once you have selected a file, you pass create a FileInfo from the path and pass it to SaveAs
, eg:
// Configure save file dialog box
var dlg = new Microsoft.Win32.SaveFileDialog
{
FileName = "NewSheet", // Default file name
DefaultExt = ".xlsx", // Default file extension
Filter = "Excel Sheet (.xlsx)|*.xlsx" // Filter files by extension
}
// Show save file dialog box
var result = dlg.ShowDialog();
// Process save file dialog box results
if (result == true)
{
// Save document
string filename = new FileInfo(dlg.FileName);
package.SaveAs(newFile);
}
Or you can use SaveFileDialog.OpenStream to have the dialog itself create a stream to save to:
if (result == true)
{
// Save document
using(var stream=dlg.OpenStream())
{
package.SaveAs(stream);
}
}