本人经过一上午的搜索,总结了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的相互转换文章来源:https://www.toymoban.com/news/detail-439868.html
//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()
{ }
/// <summary>
/// 转换excel 成PDF文档
/// </summary>
/// <param name="_lstrInputFile">原文件路径</param>
/// <param name="_lstrOutFile">pdf文件输出路径</param>
/// <returns>true 成功</returns>
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不太了解,这里只提供一个思路。文章来源地址https://www.toymoban.com/news/detail-439868.html
到了这里,关于C#实现将excel转换成pdf的三种方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!