C#实现将excel转换成pdf的三种方法

C#实现将excel转换成pdf的三种方法,第1张

文章目录
    • 1.使用免费版的spire
    • 2.使用Microsoft.Office.Interop.Excel
    • 3.使用Aspose.Cells

本人经过一上午的搜索,总结了C#将excel转pdf的三种方法(导出的excel转化成pdf下载下来)。
设计文章数量较多,没有转载请见谅。

1.使用免费版的spire

下载地址https://www.e-iceblue.cn/Downloads/Free-Spire-XLS-NET.html

//前面是用NPOI处理导出的数据
Workbook workbook = new Workbook();
workbook.LoadFromFile("Excel路径");
Worksheet sheetpdf = workbook.Worksheets[sheetIndexs];
sheetpdf.SaveToPdf("pdf路径");

附带MemoryStream与FileStream的相互转换

//MemoryStream  ->  FileStream
MemoryStream ms = new MemoryStream();
wk.Write(ms);
ms.Seek(0, SeekOrigin.Begin);
FileStream fs = new FileStream("路径", FileMode.OpenOrCreate);
BinaryWriter w = new BinaryWriter(fs);
w.Write(ms.ToArray());
fs.Close();
ms.Close();
//FileStream->  MemoryStream  
byte[] data = File.ReadAllBytes(mainURL + @"\testPdf.pdf");
MemoryStream msPdf = new MemoryStream(data);
2.使用Microsoft.Office.Interop.Excel
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;
//Office 命名空间
namespace OfficeToPdf
{
   //excel 类
   class ExcelConverter
   {
       //构造函数
       public ExcelConverter()
       { }
       /// 
       /// 转换excel 成PDF文档
       /// 
       /// 原文件路径
       /// pdf文件输出路径
       /// true 成功
       public bool ConverterToPdf(string _lstrInputFile, string _lstrOutFile)
       {
           Microsoft.Office.Interop.Excel.Application lobjExcelApp = null;
           Microsoft.Office.Interop.Excel.Workbooks lobjExcelWorkBooks = null;
           Microsoft.Office.Interop.Excel.Workbook lobjExcelWorkBook = null;
           string lstrTemp = string.Empty;
           object lobjMissing = System.Reflection.Missing.Value;
           try
           {
               lobjExcelApp = new Microsoft.Office.Interop.Excel.Application();
               lobjExcelApp.Visible = true;
               lobjExcelWorkBooks = lobjExcelApp.Workbooks;
               lobjExcelWorkBook = lobjExcelWorkBooks.Open(_lstrInputFile, true, true, lobjMissing, lobjMissing, lobjMissing, true,
                 lobjMissing, lobjMissing, lobjMissing, lobjMissing, lobjMissing, false, lobjMissing, lobjMissing);
               //Microsoft.Office.Interop.Excel 12.0.0.0之后才有这函数     
               lstrTemp = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".xls" + (lobjExcelWorkBook.HasVBProject ? 'm' : 'x');
               //lstrTemp = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".xls";
               lobjExcelWorkBook.SaveAs(lstrTemp, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel4Workbook, Type.Missing, Type.Missing, Type.Missing, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing,
                 false, Type.Missing, Type.Missing, Type.Missing);
               //输出为PDF 第一个选项指定转出为PDF,还可以指定为XPS格式
               lobjExcelWorkBook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, _lstrOutFile, Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard, Type.Missing, false, Type.Missing, Type.Missing, false, Type.Missing);
               lobjExcelWorkBooks.Close();
               lobjExcelApp.Quit();
           }
           catch (Exception ex)
           {
               //其他日志 *** 作;
               return false;
           }
           return true;
       }
   }
}
3.使用Aspose.Cells
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook("excel路径");  
wb.Save("pdf路径", SaveFormat.Pdf);

以上三种方法经过试验是可以使用的。
据说还有用python去转换的,然后再用C#掉用,由于对python不太了解,这里只提供一个思路。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/916989.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存