Aspose.Cells: excel 转 pdf

引用:Aspose.Cells

using System;
using System.IO;

namespace WIMI.BTL.ExcelToPdf
{
    public class ExcelToPdfUtil
    {
        private const string QualityReportPath = @"D:\a\";

        public static void QualityReportExcelToPdf(FileDto file)
        {
            var excelPath = QualityReportPath + file.FileName;

            if (!File.Exists(excelPath))
            {
                throw new Exception($"未能找到文件");
            }

            var pdfDirectory = QualityReportPath;

            if (!Directory.Exists(pdfDirectory))
            {
                Directory.CreateDirectory(pdfDirectory);
            }
             
            var pdfPath = QualityReportPath + file.FileName.Split('.')[0] + ".pdf";

            if (File.Exists(pdfPath)) { File.Delete(pdfPath); }

            Aspose.Cells.Workbook excel = new Aspose.Cells.Workbook(excelPath);

            excel.Settings.MemorySetting = Aspose.Cells.MemorySetting.MemoryPreference;
            excel.Settings.AutoCompressPictures = true;
            excel.Settings.EnableMacros = false;

            Aspose.Cells.PdfSaveOptions saveOptions = new Aspose.Cells.PdfSaveOptions(Aspose.Cells.SaveFormat.Pdf);
            saveOptions.AllColumnsInOnePagePerSheet = true;
            //是否单页
            saveOptions.OnePagePerSheet = false;

            saveOptions.PdfCompression = Aspose.Cells.Rendering.PdfCompressionCore.Flate;
            saveOptions.PrintingPageType = Aspose.Cells.PrintingPageType.IgnoreBlank;
            excel.Save(pdfPath, saveOptions);
        }
    }

    public class FileDto
    {
        public FileDto()
        {
        }

        public FileDto(string fileName, string fileType)
        {
            FileName = fileName;
            FileType = fileType;
            FileToken = Guid.NewGuid().ToString("N");
        }

        public string FileName { get; set; }

        public string FileToken { get; set; }

        public string FileType { get; set; }
    }
}

 

posted @ 2020-07-09 10:53  も不秃不秃  阅读(1123)  评论(0编辑  收藏  举报