【问题标题】:convert excel spreadsheet to PDF Interop.Excel alternative in C#在 C# 中将 excel 电子表格转换为 PDF Interop.Excel 替代品
【发布时间】:2013-11-15 08:59:57
【问题描述】:

(C#) 正在使用 Microsoft.Office.Interop.Excel 保存为 PDF 文件。
但是这个解决方案不适用于我的大多数 Excel 文件(非常复杂的文件,其中一些 35Mb 大小,其他带有宏......)

我想将 Excel 文件转换为 PDF 格式。

除了Interop.Excel,还有其他免费解决方案吗?

【问题讨论】:

  • 您可以使用closedxml 生成 xltx 文档。您需要免费的 pdf 文档生成或 Excel 到 pdf 转换解决方案吗?
  • 我需要一个 Excel 到 pdf 的转换 johny

标签: c# pdf-generation excel-interop


【解决方案1】:

你检查过这个链接吗? http://www.codeproject.com/Articles/17574/Programmatically-Convert-Documents-to-PDFs-the-Eas

已编辑

首先您必须下载 Ghostscript 并安装它。 继续添加本地打印机并取消选中“自动检测并安装我的即插即用打印机”。

创建一个新的本地端口并输入“C:\output.ps”作为端口名称。

为了让 GhostScript 正确解析 PostScript,必须将其设置为打印机驱动程序。您可以在 lib 文件夹的 GhostScript 安装目录中找到 GhostScript 的打印机驱动程序。

要使附加的代码正常工作,请将打印机命名为 Ghostscript。附加的应用程序将所有谜题放在适当的位置。首先,应用程序存储当前的默认打印机。这将在稍后用于设置打印机。

public static string GetDefaultPrinterName(){
   PrintDocument pd = new PrintDocument();
   return pd.PrinterSettings.PrinterName;
}

其次,我们之前设置的 Ghostscript 打印机被设置为默认值,因此用户不必选择打印机。

public static long SetDefaultPrinterName(string name){
    return SetDefaultPrinter(name);
}

第三,我们对文件调用 print 命令。对我来说,这里的诀窍是检测何时打印 Excel 文件。我主要使用 Excel 文档,需要一种快速的方法来打印整个工作簿,而不仅仅是打开的工作表。

public static void CreatePdf(string action, string file, string directory){
if (file.EndsWith("xls")){
    Excel.ApplicationClass excel = new Excel.ApplicationClass();
    excel.Visible = false;

    Excel.Workbook workbook = excel.Workbooks.Open(Path.Combine(directory, file),
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing);

    workbook.PrintOut(Type.Missing, Type.Missing, 1, false, Type.Missing, 
        false, Type.Missing, Type.Missing);

    excel.Quit();
}else{
    Process p = new Process();

    p.StartInfo.FileName = file;
    p.StartInfo.Verb = action;
    p.StartInfo.WorkingDirectory = directory;
    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    p.StartInfo.CreateNoWindow = true;
    Console.WriteLine("Starting process");
    p.Start();
    //p.Kill();
}
Console.WriteLine("Creating Pdf");
CreatePdf(file);
}

最后,我们调用 GhostScript 可执行文件,为它提供我们希望它输出到的文件名以及在哪里可以找到 PostScript。为了向可执行文件传递一条语句,我们只需将输入重定向到我们创建的命令,并获取输出。

private static string CreatePdf(string fileName){

        string command = "gswin32c -q -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=\"" + 
                fileName + ".pdf\"  -fc:\\output.ps";

        Console.WriteLine(command);
        Process p = new Process();

        StreamWriter sw;
        StreamReader sr;

        ProcessStartInfo info = new ProcessStartInfo("cmd");
        info.WorkingDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
        Console.WriteLine("Current directory: " + info.WorkingDirectory);
        info.CreateNoWindow = true;
        info.UseShellExecute = false;

        info.RedirectStandardInput = true;
        info.RedirectStandardOutput = true;

        p.StartInfo = info;
        p.Start();

        sw = p.StandardInput;
        sr = p.StandardOutput;
        sw.AutoFlush = true;

        sw.WriteLine(command);

        sw.Close();

        string ret = sr.ReadToEnd();

        Console.WriteLine(ret);
        return ret;
    }

GhostScript 运行后,我们只是将打印机设置回之前的默认设置,用户一点也不聪明。

try{
    Printers.SetDefaultPrinterName(currentPrinterName);
}catch{}

【讨论】:

  • 根据this,您不应仅提供链接。关于Provide context for links的部分。
  • 哦,对不起。我对SO很陌生,我已经阅读并按照您的建议完成了。感谢您的评论。
猜你喜欢
  • 2021-05-27
  • 2020-07-19
  • 2017-05-28
  • 2012-04-23
  • 2015-12-11
  • 2011-09-28
  • 1970-01-01
  • 1970-01-01
  • 2013-05-04
相关资源
最近更新 更多