- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 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,但我不能让它工作,而且没有太多关于这个的信息......
更新:该应用程序在从项目内部或服务器执行时工作。如果使用快捷方式执行或将 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);
}
}
关于c# - EPPlus 库,另存为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39870552/
是否可以在 EPPlus 中为整个列设置样式?我希望我可以只使用 Column方法,但是当我这样做时,我得到了奇怪的结果: //Sets all cells in all columns to Red
有人知道如何使用 EPPLUS 设置条件格式的字体颜色吗? Using rng = ws.Cells("A7:AD29") Dim condSumLn = ws.ConditionalForm
我正在查看 POST 以使用 EPPlus 在 Excel 中创建多级可折叠分组,但我无法在现有组中创建内部组。 请参阅我在这里使用的示例文件 看来,我需要为第 14-18 行设置 OutlineLe
示例。埃普拉斯怎么样? Sheet.Cells["a1:d1"].Column.Width = 10; 在 Excel 互操作中: Sheet.get_Range("a1", "d1").Column
我在使用 EPPlus 处理 Excel 文档中的注释时遇到了一些问题。我可以添加评论并删除它们,但有时它不成功。 我正在使用注释来显示 Excel 文件处理结果的验证消息,并且当该文件被重新处理时,
我正在将 SSRS 报告导出到单独的 excel 文件中,并使用 EPPlus 将它们合并到一个文件中。合并成功,但当我尝试在 Excel 中打开文件时出现以下错误。 我删除了报告数据以生成空报告,但
我正在使用EPPlus读取excel工作表并将数据加载到数据表中以执行进一步的操作,然后将修改后的数据保存回excel文件中。 下面的代码检查单元格值是否为浮点值,然后将浮点值转换为日期时间。 当单元
通过查看EPPlus随附的示例,我已经成功创建了数据透视表,但是我无法为其设置正确的报告布局。我希望它是“表格格式”,而不是“大纲”或其他格式。在我看来,EPPlus现在不支持此功能,但是也许我缺少了
我使用 EPPlus 阅读工作表。在此工作表中,我需要的数据在列 B-D 中.在栏目A有静态值,我会忽略。 col A 中的值填写到Row 1000 .但是,我需要的数据只会在那里,例如 Row 45
在 EPPlus 中,我可以使用 workSheet.Cells[x, y].AddComment() 将注释添加到工作表中的单元格 但是如何从给定单元格中删除注释 - 没有 workSheet.Ce
设置列的 WrapText=true 后,我想查看行的新高度(即文本是否换行,行数)。行的 Height 属性似乎未更新。 ExcelPackage pkg = new ExcelPa
我目前正在与一位在 Excel 中手动生成发票的客户合作。我已经使用 epplus 自动为他们生成这个文件。 我的要求之一是每个页面周围都应该有一个边框,并以大约 7 行长的标题开始(并且每行具有不同
更改单元格的值时遇到问题。这是我的代码: FileInfo newFile = new FileInfo(file); ExcelPackage pck = new ExcelPackage(newF
当我使用 EPPlus 自动调整格式化为日期字符串的列时,自动调整机制使用未格式化的字符串作为列宽度的度量。 这是一个错误还是我做错了什么? 代码 sheet.Column(2).Style.Numb
using (var pck = new ExcelPackage(new FileInfo(wb_path))) { ExcelWorksheet ws = pck.Workbook.Wor
我有以下代码行,它根据特定列对工作表中的数据进行升序排序。问题是,它考虑了第一行(单元格 A1、B1 等)。 worksheet.Cells["A:AA"].Sort(new int[] { 5, 1
我正在.NET 4.0 命令行应用程序上使用 EPPlus 库创建电子表格。 我成功地从数据库中加载了所有数据,最后我正在创建一个图形,这是我的代码: var chart = sheet.Drawin
如何使用 EPPLus 将图表创建为单独的工作表,而不是作为现有工作表中的绘图? 最佳答案 在 4.0.4.0 版本(从 codeplex 下载代码,以纠正保存文件中的错误),您可以使用: workb
我正在运行一个带有 EPPlus 库的 C# 项目,我需要它检查各个列/行中的数据的单元格,然后找到“关键字”,可以说并将其与其他各个列/行中的数据匹配。 我需要知道一种将一个单元格中的数据与另一个单
我在使用 C# 和 EPPlus 从工作簿中读取某些工作表时遇到问题。 错误信息 name contains invalid characters 即使多次检查工作簿中的工作表名称、文件名和列标题以确
我是一名优秀的程序员,十分优秀!