EPPlus库,另存为
本文关键字:另存为 EPPlus | 更新日期: 2023-09-27 18:11:48
使用EPPlus,可以很容易地将文档保存到预定义的路径,但我需要每次保存到不同的路径,所以我需要EPPlus显示标准的保存对话框。
我的代码可以从一个数据表创建一个Excel文件,稍微格式化一下,然后保存在指定的位置:
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();
}
如何显示手动选择目标文件夹的对话框?我和帕克试过了。保存,但是我不能使它工作,并且没有太多关于这个的信息…
更新:当从项目内部或从服务器执行时,应用程序可以工作。如果使用快捷方式执行或将exe复制/粘贴到我的桌面,则会崩溃。
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通过Microsoft.Win32
命名空间提供标准文件对话框。您可以使用SaveFileDialog来显示对话框,并选择或创建要保存的文件。
一旦你选择了一个文件,你从路径中传递创建一个FileInfo并传递给SaveAs
,例如:
// 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);
}
或者您可以使用SaveFileDialog。OpenStream让对话框自己创建一个流保存到:
if (result == true)
{
// Save document
using(var stream=dlg.OpenStream())
{
package.SaveAs(stream);
}
}
尝试使用SaveFileDialog
:
private static bool? saveExcelPackageDialog(string fileName, string workingDirectory, byte[] rawBinaryObject)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = EXCEL_FILTER_FILE_DIALOG;
sfd.InitialDirectory = workingDirectory;
sfd.FileName = fileName;
bool? result = sfd.ShowDialog();
if (result == true)
{
File.WriteAllBytes(sfd.FileName, rawBinaryObject);
System.Diagnostics.Process.Start(sfd.FileName);
}
return result;
}
你必须这样使用它:
saveExcelPackageDialog(newFile.FullName, outputDir.FullName, pkg.GetAsByteArray());