这是一个简单的项目,用来将excel导出为pdf。
它由两个步骤组成:
- 使用
NpoiExcelHelper.ExcelToHtml
方法将excel导出为html - 使用
PdfHelper.HtmlToPdf
转换html为pdf
npoi和WkHtmlToPdf做了几乎所有的工作,也因为这样,需要查询文档时请移步到这两个项目查看。 特别感谢
本项目也对自定义做了简单的封装,参考文档自定义一节。
Install-Package ExcelToPdf
具体请参考ExcelToPdfSample/Pages/Index.cshtml.cs
。
// in StartUp.cs
services.AddExcelToPdf("temp");
private readonly ILogger<IndexModel> _logger;
private readonly ExcelToPdfService _excelToPdfService;
// inject ExcelToPdfService from controller
public IndexModel(ILogger<IndexModel> logger, ExcelToPdfService excelToPdfService)
{
_logger = logger;
_excelToPdfService = excelToPdfService;
}
// export to pdf
public void OnPostSimpleExport()
{
FileInfo excelFileInfo, htmlFileInfo, pdfFileInfo;
SetupSample(out excelFileInfo, out htmlFileInfo, out pdfFileInfo);
// export pdf
_excelToPdfService.ExportToPdf(excelFileInfo.FullName, pdfFileInfo.FullName);
this.Message = $"Export Success, Pdf file save to: {pdfFileInfo.FullName}";
}
使用NpoiExcelHelper.ExcelToHtml
方法时,可以用configOptions参数来对导出的html做自定义处理,具体定义请参考:Npoi ExcelToHtmlConverter
使用ExportToPdf
方法时,可以使用configGlobalSettings参数来定义pdf导出:
// ... other code ...
// export pdf
_excelToPdfService.ExportToPdf(excelFileInfo.FullName, pdfFileInfo.FullName, configPdfGlobalSettings: config =>
{
config.Orientation = Orientation.Landscape;
config.ColorMode = ColorMode.Grayscale;
config.Margins.Left = 150;
config.Margins.Top = 50;
});
// ... other code ...
GlobalSettings 常用属性:
参数名 | 说明 | 默认值 | 备注 |
---|---|---|---|
Orientation | 排版方向 | 竖向 | Portrait:竖排,Landscape:横排 |
ColorMode | 颜色模式 | 彩色 | Color:彩色,Grayscale:灰度 |
UseCompression | 使用压缩 | 是 | |
DPI | 打印dpi | 96 | |
DocumentTitle | 文档标题 | 空 | |
ImageDPI | 图片DPI | 600 | |
imageQuality | 图片质量 | 94 | |
PaperSize | 纸面尺寸 | ||
PaperWidth | 文档宽度 | ||
PaperHeight | 文档高度 | ||
MarginLeft | 左边距 | ||
MarginRight | 右边距 | ||
MarginTop | 上边距 | ||
MarginBottom | 下边距 |
全部参数请参考: WkHtmlToPdf GlobalSettings源码