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

C# 在C中将excel电子表格转换为PDF Interop.excel替代文件#,c#,pdf-generation,excel-interop,C#,Pdf Generation,Excel Interop,(C#)我使用Microsoft.Office.Interop.Excel保存为PDF文件。 但是这个解决方案对于我的大多数Excel文件(非常复杂的文件,其中一些大小为35Mb,另一些带有宏…)都不起作用 我不想把Excel文件转换成PDF格式 除了Interop.Excel,还有其他免费的解决方案吗?您检查过这个链接了吗? 已编辑 首先,您必须下载Ghostscript并安装它。 继续添加本地打印机并取消选中“自动检测并安装我的即插即用打印机” 创建一个新的本地端口,并输入“C:\outp

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

我不想把Excel文件转换成PDF格式


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

您检查过这个链接了吗?

已编辑

首先,您必须下载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{}

您可以使用xltx生成文档。你需要pdf文档生成或Excel到pdf转换的免费解决方案吗?我需要一个Excel到pdf转换软件。你不应该只提供一个链接根据。关于
的部分为链接提供上下文。哦,对不起。我是新来的,所以,我已经按照你的建议阅读和做了。谢谢你的评论。