Excel 到 PDF C# 库 [关闭]
Posted
技术标签:
【中文标题】Excel 到 PDF C# 库 [关闭]【英文标题】:Excel to PDF C# library [closed] 【发布时间】:2011-07-26 20:29:34 【问题描述】:我正在寻找一个 MsExcel(.xsl 和 .xlsx)到 PDF 转换器/库或 API。我想要它用于我的 C# .Net 应用程序。
我喜欢商业图书馆,但买不起。
【问题讨论】:
只是想添加一个替代方案GemBox.Spreadsheet,它也可以进行所需的转换,请参阅here。简而言之,它实际上是一行代码:ExcelFile.Load("Input.xlsx").Save("Input.output"); 这里有一个完整的IIS配置解决方案。mcuslu.medium.com/… 【参考方案1】:我尝试过通过第三方包通过 Interop 直接进行 COM 交互,但如果出于成本考虑而无法选择,我将使用 Office 2007/2010 的内置导出功能来完成此操作。
你需要调用的方法是Workbook.ExportAsFixedFormat()
以下是我如何将其用作导出功能的示例:
public bool ExportWorkbookToPdf(string workbookPath, string outputPath)
// If either required string is null or empty, stop and bail out
if (string.IsNullOrEmpty(workbookPath) || string.IsNullOrEmpty(outputPath))
return false;
// Create COM Objects
Microsoft.Office.Interop.Excel.Application excelApplication;
Microsoft.Office.Interop.Excel.Workbook excelWorkbook;
// Create new instance of Excel
excelApplication = new Microsoft.Office.Interop.Excel.Application();
// Make the process invisible to the user
excelApplication.ScreenUpdating = false;
// Make the process silent
excelApplication.DisplayAlerts = false;
// Open the workbook that you wish to export to PDF
excelWorkbook = excelApplication.Workbooks.Open(workbookPath);
// If the workbook failed to open, stop, clean up, and bail out
if (excelWorkbook == null)
excelApplication.Quit();
excelApplication = null;
excelWorkbook = null;
return false;
var exportSuccessful = true;
try
// Call Excel's native export function (valid in Office 2007 and Office 2010, AFAIK)
excelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, outputPath);
catch (System.Exception ex)
// Mark the export as failed for the return value...
exportSuccessful = false;
// Do something with any exceptions here, if you wish...
// MessageBox.Show...
finally
// Close the workbook, quit the Excel, and clean up regardless of the results...
excelWorkbook.Close();
excelApplication.Quit();
excelApplication = null;
excelWorkbook = null;
// You can use the following method to automatically open the PDF after export if you wish
// Make sure that the file actually exists first...
if (System.IO.File.Exists(outputPath))
System.Diagnostics.Process.Start(outputPath);
return exportSuccessful;
【讨论】:
不要忘记安装 Office 插件 SaveAs PDF 和 XPS(Office 2007 的插件)。可以从微软官网下载。 大崩溃!谢谢 workbookPath有多个怎么办【参考方案2】:这些文章会对你有所帮助!
PDF Converter Services
iTextSharp
Excel to PDF .NET
EDIT:我找到了这个类函数。
public DataSet GetExcel(string fileName)
Application oXL;
Workbook oWB;
Worksheet oSheet;
Range oRng;
try
// creat a Application object
oXL = new ApplicationClass();
// get WorkBook object
oWB = oXL.Workbooks.Open(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
// get WorkSheet object
oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Sheets[1];
System.Data.DataTable dt = new System.Data.DataTable("dtExcel");
DataSet ds = new DataSet();
ds.Tables.Add(dt);
DataRow dr;
StringBuilder sb = new StringBuilder();
int jValue = oSheet.UsedRange.Cells.Columns.Count;
int iValue = oSheet.UsedRange.Cells.Rows.Count;
// get data columns
for (int j = 1; j <= jValue; j++)
dt.Columns.Add("column" + j, System.Type.GetType("System.String"));
//string colString = sb.ToString().Trim();
//string[] colArray = colString.Split(':');
// get data in cell
for (int i = 1; i <= iValue; i++)
dr = ds.Tables["dtExcel"].NewRow();
for (int j = 1; j <= jValue; j++)
oRng = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[i, j];
string strValue = oRng.Text.ToString();
dr["column" + j] = strValue;
ds.Tables["dtExcel"].Rows.Add(dr);
return ds;
catch (Exception ex)
Label1.Text = "Error: ";
Label1.Text += ex.Message.ToString();
return null;
finally
Dispose();
编辑 2:我还发现这篇文章对你有帮助!
http://www.c-sharpcorner.com/UploadFile/psingh/PDFFileGenerator12062005235236PM/PDFFileGenerator.aspx
【讨论】:
第一次用代码编辑对于将excel转换为pdf的目的完全没有用。【参考方案3】:尝试以下方法:
http://www.sautinsoft.com/convert-excel-xls-to-pdf/spreadsheet-xls-excel-to-pdf-export-component-asp.net.php
或
http://www.html-to-pdf.net/excel-library.aspx
我认为您也可以操纵 IText 来执行此操作:http://www.itextpdf.com/
还有http://www.aspose.com,但不是特别便宜。
以下关于堆栈溢出的答案可能会有所帮助。 https://***.com/questions/891531/convert-xls-doc-files-to-pdf-with-c 和 https://***.com/questions/769246/xls-to-pdf-conversion-inside-net .第二个答案有一个有趣的解决方案自动化开放办公室!
【讨论】:
最简单的 //导出为PDF var x = new SautinSoft.ExcelToPdfOutputFormat = SautinSoft.ExcelToPdf.eOutputFormat.Pdf; var pdfpath = string.Format("0\\1.pdf", currentDirectorypath, fileName); x.ConvertFile(finalXcelPath, pdfpath);以上是关于Excel 到 PDF C# 库 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章