要求:将多个table导出到一个PDF里,然后打印。
问题分析:要求将四个table放一个PDF打印,四个table的列各不相同,第一个是表头,其他三个是列表,列比表头多很多,如果直接生成一个excel,然后再直接导出会发现有些列在PDF中换行了。
原因:因为excle可打印的区域是有限制的,可打印的地方如下方法可见:文件-打印-设置-打印选择区域(如果打印要宽一点,选择A4,横向打印,)这时你点开始(2010版)回到sheet里,发现里面有一条虚线。这个虚线就是excel生成PDF后(相应纸张格式)最多显示的宽。
解决方法:
一,为了解决上面所说情况,可以设好四个table对应的excle模板,然后生成四个pdf。
二,再将四个PDF全并,注意(打印的方向,纸张大小一定要一样)
合并方法:
/// <summary> /// PDF合并 /// </summary> /// <param name="arrFileList">要合并的PDF名称集</param> /// <param name="outMergeFile">合并后的PDF</param> public static void MergePDFFiles(List<string> arrFileList, string strOutMergeFile) { PdfReader reader; Document document = new Document(); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(strOutMergeFile, FileMode.Create)); document.SetPageSize(PageSize.A4.Rotate()); //如果你的模板是A4的,这里一定要是A4 document.Open(); PdfContentByte cb = writer.DirectContent; PdfImportedPage newPage; foreach (string strFileName in arrFileList) { reader = new PdfReader(strFileName); int iPageNum = reader.NumberOfPages; for (int j = 1; j <= iPageNum; j++) { document.NewPage(); newPage = writer.GetImportedPage(reader, j); cb.AddTemplate(newPage, 0, 0); } } document.Close(); }
调用:
List<string> arrFileList= new List<string>(); arrFileList.Add(strFileName); MergePDFFiles(arrFileList, strFileName);
table根据模板生成excel
Workbook excel = new Workbook(); string strFilePath = ExcelTemplatePath + strTableName + ".xls"; //建立excel并打开模板文件 excel.Open(strFilePath); Worksheet sheet = excel.Worksheets["Sheet1"]; 控制sheet1 sheet.Cells.DeleteColumn((int.Parse(c.OrderNo.ToString()) - intDeleteCount));删除指定列 excel.Save(strFileName, FileFormatType.Default); ///转成PDF ConvertXlsToPdf(strFileName, strFileNamePdf);
excel转PDF
/// 将Xls文件转换为PDF文件 /// </summary> /// <param name="strSourceFile">源文件</param> /// <param name="strTargetFile">目标文件</param> /// <returns>是否成功</returns> public static bool ConvertXlsToPdf(string strSourceFile, string strTargetFile) { if (File.Exists(strTargetFile)) { File.Delete(strTargetFile); } FileInfo fiPdf = new FileInfo(strTargetFile); if (!fiPdf.Directory.Exists) { fiPdf.Directory.Create(); } bool blnResult = false; #region 微软式导出 Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF; object missing = Type.Missing; Excel.ApplicationClass application = null; Excel.Workbook workBook = null; object paramFromPage = Type.Missing; object paramToPage = Type.Missing; try { application = new Excel.ApplicationClass(); object target = strTargetFile; //object type = targetType; System.IO.FileInfo fi = new System.IO.FileInfo(strSourceFile); workBook = application.Workbooks.Open(fi.FullName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing); blnResult = true; } catch (Exception ex) { blnResult = false; throw ex; } finally { if (workBook != null) { workBook.Close(false, missing, missing); workBook = null; } if (application != null) { application.Quit(); application = null; } GC.Collect(); GC.WaitForPendingFinalizers(); } #endregion return blnResult; }
PDF合并
时间: 2024-10-23 20:40:55