繁体   English   中英

在C#中将excel电子表格转换为PDF Interop.Excel替代品

[英]convert excel spreadsheet to PDF Interop.Excel alternative in C#

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

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

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

您检查过此链接了吗? 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{}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM