ExcelToPDF.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using Aspose.Cells;
  2. using Bowin.Common.Utility;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. namespace Bowin.Common.Office
  9. {
  10. public static class ExcelToPDF
  11. {
  12. public static MemoryStream Export(Stream excelStream)
  13. {
  14. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  15. Workbook wb = new Workbook(excelStream, new LoadOptions(LoadFormat.Auto));
  16. var pdfStream = new MemoryStream();
  17. wb.Save(pdfStream, new PdfSaveOptions(SaveFormat.Pdf) {
  18. });
  19. return pdfStream;
  20. }
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. /// <param name="excelStream"></param>
  25. /// <param name="filePath">相对路径</param>
  26. /// <returns></returns>
  27. public static string ExportToFile(MemoryStream excelStream, string filePath)
  28. {
  29. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  30. var pdfFileName = Guid.NewGuid().ToString() + ".pdf";
  31. var fullPath = filePath + (!filePath.EndsWith("/") ? "/" : "") + pdfFileName;
  32. var physicalFullPath = HttpHelper.MapPath(fullPath);
  33. Workbook wb = new Workbook(excelStream, new LoadOptions(LoadFormat.Auto));
  34. wb.Save(physicalFullPath, SaveFormat.Pdf);
  35. return fullPath;
  36. }
  37. }
  38. }