本帖最后由 dexteryao 于 2020-4-29 17:23 编辑
SpreadJS和Excel一样支持设置“每页上边打印重复区域”,但是现阶段还无法实现页面下部重复打印。https://demo.grapecity.com.cn/spreadjs/SpreadJSTutorial/features/print/custom-print/purejs
为了实现此需求,可以通过使用SpreadJS提供的pageInfo 接口获取到打印分页信息,动态插入重复区域到页面下方,来实现尾部的重复打印。
https://demo.grapecity.com.cn/spreadjs/help/latest/content/GetPageInformation.html
因为要动态插入行,所以首先需要通过toJSON 和fromJSON的方式复制一个workbook用于打印。
其次,针对pageInfo获取到的打印信息,每页的起始单元格的行列,行数还有列数。通过计算页面尾部单元格行的高度,在高度大于尾部重复行高度时,插入空行,并复制样式和内容,设置行高。
Demo中处理未考虑合并单元格和强制换页等情况,可以根据实际业务情况调整策略。
核心代码如下
- adjustRepeatEndRowForPrint(spread1, JSON.stringify(spread.toJSON()), 0, 3);
- spread1.print(0);
复制代码
- function adjustRepeatEndRowForPrint(spread1, workbookJSON, index, endRowCount){
- spread1.suspendPaint()
- spread1.fromJSON(JSON.parse(workbookJSON))
- var sheet = spread1.getSheet(index)
- sheet.isPrintLineVisible(true)
- var pageInfo = spread1.pageInfo(0);
- var pageIndex = 0;
- var endRowTotalHeight = 0, endRowHeight = [];
- for(var i = 0; i < endRowCount; i++){
- endRowHeight[i] = sheet.getRowHeight(sheet.getRowCount() - i - 1);
- endRowTotalHeight += endRowHeight[i];
- }
- while(pageIndex < pageInfo.pages.length - 1){
- var range = pageInfo.pages[pageIndex];
- var pageEndRowTotalHeight = 0, lastRow = range.row + range.rowCount - 1;
- while(true){
- pageEndRowTotalHeight += sheet.getRowHeight(lastRow);
- if(pageEndRowTotalHeight >= endRowTotalHeight){
- break
- }
- lastRow--;
- }
- sheet.addRows(lastRow, endRowCount);
- sheet.setRowPageBreak(lastRow + endRowCount, true);
- for(var i = 0; i < endRowCount; i++){
- sheet.setRowHeight(lastRow + endRowCount - i -1, endRowHeight[i])
- }
- sheet.copyTo(sheet.getRowCount() - endRowCount, -1, lastRow, -1, endRowCount, -1, GC.Spread.Sheets.CopyToOptions.all)
- var printInfoRowEnd = sheet.printInfo().rowEnd();
- if(printInfoRowEnd > 0){
- sheet.printInfo().rowEnd(printInfoRowEnd + endRowCount);
- }
- pageInfo = spread1.pageInfo(0);
- console.log(pageInfo)
- pageIndex++;
- }
-
- spread1.resumePaint()
- }
复制代码
SpreadJS打印页尾重复区域.zip
(1.75 MB, 下载次数: 439)
|
|