简体   繁体   中英

Print excel file to pdf in landscape mode using C#

I have a project that it should print check bill in both excel and pdf format. Originally I use NPOI to print excel and Spire to print pdf using the excel file:

public HSSFWorkbook Workbook;
public void SaveExcelFile(string FilePath)
{
    this.AutoSizeColumn();
    FilePath = FilePath == "" ? AppDomain.CurrentDomain.BaseDirectory + "sample.xls" : FilePath;
    var File = new FileStream(FilePath, FileMode.Create);
    Workbook.Write(File);
    File.Close();
}

public void SavePdfFileFromExcel(string ExcelFilePath, string PdfFilePath)
{
    Workbook pdfWorkbook = new Workbook();
    pdfWorkbook.LoadFromFile(ExcelFilePath);
    pdfWorkbook.SaveToFile(PdfFilePath, Spire.Xls.FileFormat.PDF); 
}

But Spire will just print the pdf in portrait, and the bill will look small and ugly. I want to know if there's a way I can print the pdf in landscape.

Or I should use iText to create a pdf format by self?

Try like this:

public void ExcelLandscapeFormat()
{
 try 
 { 
    ((Microsoft.Office.Interop.Excel._Worksheet)  
    excelWbook.ActiveSheet).PageSetup.Orientation =  
    Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;

    excelWbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF,  
    Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + output); 
 } 
 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