简体   繁体   English

使用C#在横向模式下将Excel文件打印为pdf

[英]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. 我有一个项目,它应该同时以excel和pdf格式打印支票。 Originally I use NPOI to print excel and Spire to print pdf using the excel file: 最初,我使用NPOI来打印excel,使用Spire来使用excel文件来打印pdf:

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. 但是Spire只会以纵向打印pdf,这笔帐单看起来又小又丑。 I want to know if there's a way I can print the pdf in landscape. 我想知道是否有一种方法可以横向打印pdf。

Or I should use iText to create a pdf format by self? 还是我应该使用iText自行创建pdf格式?

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

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

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