本文主要是介绍Aspose.Cells转换excel为pdf时,表格撕裂为第二页问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/// <summary>
/// 将excel转换为PDF
/// </summary>
/// <param name="excelFile">".xls", ".xlsx"类型的文件路径</param>
/// <param name="pdfFilePath">生成的PDF文件</param>
/// <returns></returns>
public static async Task<(bool, string)> ExcelToPdf(string excelFile, string pdfSavePath)
{bool isPass = false;//string pdfSavePath = string.Empty;string msg = string.Empty;await Task.Run(() =>{try{//excel转换为pdfWorkbook document = new Workbook(excelFile);Aspose.Cells.Style style = document.Styles[document.Styles.Add()];style.ShrinkToFit = true;int cnt = document.Worksheets.Count;for (int i = 0; i < cnt; i++){Aspose.Cells.Worksheet sheet = document.Worksheets[i];sheet.IsPageBreakPreview = true;//sheet.AutoFitColumns();//sheet.AutoFitRows();sheet.PageSetup.FooterMargin = 0;sheet.PageSetup.HeaderMargin = 0;//2019-10-12 17:55:55 修改,解决excel文件预览表格时撕裂到第二页了sheet.PageSetup.RightMargin = 0;sheet.PageSetup.LeftMargin = 0;sheet.PageSetup.CenterHorizontally = true;}//string time = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss");//pdfSavePath = "d:\\" + time + ".pdf";document.Save(pdfSavePath, Aspose.Cells.SaveFormat.Pdf);isPass = true;}catch (Exception ex){pdfSavePath = string.Empty;isPass = false;msg = ex.Message;LogHelpter.AddLog(ex.ToString());}});return (isPass, msg);
}
这篇关于Aspose.Cells转换excel为pdf时,表格撕裂为第二页问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!