简体   繁体   中英

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

(C#) am using Microsoft.Office.Interop.Excel to save as PDF File.
but this solution not working fine for the most of my Excel files (very complex files, some of them 35Mb size, others with macros...)

i whant to transform Excel File to PDF format.

are there any other free solution than Interop.Excel?

Have you checked this link? http://www.codeproject.com/Articles/17574/Programmatically-Convert-Documents-to-PDFs-the-Eas

EDITED

First you have to download Ghostscript and install it. Continue by adding a local printer and uncheck "Automatically detect and install my Plug and Play printer".

Create a new local port and enter "C:\\output.ps" for the port name.

In order for GhostScript to correctly parse the PostScript, it must be set as the printer driver. You can find the printer driver for GhostScript in the GhostScript installation directory in the lib folder.

For the attached code to work, name the printer Ghostscript. The attached application puts all the puzzles in place. First the application stores the current default printer. This will be used later to set the printer back.

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

Second, the Ghostscript printer that we set up earlier is set as the default so the user doesn't have to select a printer.

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

Third, we call the print command on a file. The trick here for me was detecting when an Excel file was printed. I work primarily with Excel documents and needed a quick way to print out the entire workbook and not just the opened worksheet.

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);
}

Finally, we call the GhostScript executable, giving it the filename we want it outputted to and where to find the PostScript. In order to pass the executable a statement, we just redirect the input to a command we create and we also grab the output.

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;
    }

After GhostScript runs, we just set the printer back to the previous default and the user is none the wiser.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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