0

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

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

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

4

1 回答 1

0

你检查过这个链接吗? 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{}
于 2013-11-15T10:21:45.730 回答