使用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();
}
如何显示手动选择目标文件夹的对话框? 我试过pck.SaveAs,但是我无法使它工作,而且关于这个的信息不多......
更新: 该应用程序在从项目内部或从服务器执行时工作。如果使用shourcut执行或将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());
}
答案 0 :(得分:1)
尝试使用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());
答案 1 :(得分:1)
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);
}
}