页面打印时强行分页的处理table换行处理

本文介绍了如何使用CSS属性page-break-before实现网页内容的强制分页,并提供了导出、打印等操作的具体实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在要打印强行分页的地方加上:

<P class=normal style="PAGE-BREAK-BEFORE: always">&nbsp;</P>

即可. 解释如下:

page-break-before版本:CSS2  兼容性:IE4+ 继承性:无
语法:
page-break-before : auto | always | avoid | left | right | null
取值:

auto : 假如需要在对象之前插入页分割符
always : 始终在对象之前插入页分割符
avoid : 未支持。避免在对象之前插入页分割符
left : 未支持。在对象之前插入页分割符直到它到达一个空白的左页边
right : 未支持。在对象之前插入页分割符直到它到达一个空白的右页边
null : 空白字符串。取消页分割符设置

 

 

二 是实现导出,打印,页面设置,打印预览的功能

<object id="WebBrowser" style="z-index: 104; left: 624px; width: 0px; position: absolute;
            top: 160px; height: 0px" height="112" width="77" classid="clsid:8856F961-340A-11D0-A96B-00C04FD705A2"
            viewastext>
        </object>
          <div id="btn" style="text-align: center; width: 90%; margin-bottom: 5px">
          <button class="input-button" onclick="document.execCommand('saveas','','巡检单.html');"
                style="width: 80px; height: 20px" type="button">导出</button>
              &nbsp;<input class="input-button" onclick="btn.style.display='none';document.all.WebBrowser.ExecWB(6,6);btn.style.display='block';"
                    type="button" value="打 印" style="width: 80px; height: 20px" id="Button1">
                <input class="input-button" onclick="btn.style.display='none';document.all.WebBrowser.ExecWB(8,1);btn.style.display='block';"
                    type="button" value="页面设置" style="width: 80px; height: 20px">
                <input class="input-button" onclick="btn.style.display='none';document.all.WebBrowser.ExecWB(7,1);btn.style.display='block';"
                    type="button" value="打印预览" style="width: 80px; height: 20px">
            </div>

转载于:https://www.cnblogs.com/qihaiyan1989/archive/2010/08/05/1792863.html

public static void exportToWord(List<EntityItem> records, String outputPath) throws Exception { try (XWPFDocument doc = new XWPFDocument()) { // ========== 1. 页面设置(紧凑页边距) ========== CTSectPr sectPr = doc.getDocument().getBody().addNewSectPr(); CTPageSz pageSize = sectPr.addNewPgSz(); pageSize.setOrient(STPageOrientation.LANDSCAPE); pageSize.setW(BigInteger.valueOf(16840)); // A4横向宽度 pageSize.setH(BigInteger.valueOf(11900)); // A4横向高度 // 紧凑页边距(单位:twips,1cm≈567twips) CTPageMar pageMar = sectPr.addNewPgMar(); pageMar.setLeft(BigInteger.valueOf(390)); // 1.5cm (原1440=2.54cm) pageMar.setRight(BigInteger.valueOf(390)); // 1.5cm pageMar.setTop(BigInteger.valueOf(900)); // 2cm (原1800=3.17cm) pageMar.setBottom(BigInteger.valueOf(900));// 2cm pageMar.setFooter(BigInteger.valueOf(900)); // 页脚1.59cm // ========== 2. 创建表格 ========== XWPFTable table = doc.createTable(); table.setWidth("100%"); // 列宽设置(单位:twips) int[] columnWidths = {500, 800, 1200, 5000, 5000}; CTTbl cttbl = table.getCTTbl(); CTTblGrid tblGrid = cttbl.addNewTblGrid(); for (int width : columnWidths) { tblGrid.addNewGridCol().setW(BigInteger.valueOf(width)); } // ========== 3. 表头样式(水平居中+加粗大字) ========== XWPFTableRow headerRow = table.getRow(0); for (int i = 0; i < columnWidths.length; i++) { XWPFTableCell cell = headerRow.getCell(i); if (cell == null) cell = headerRow.createCell(); // 设置列宽 cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(columnWidths[i])); // 创建水平居中段落 cell.removeParagraph(0); XWPFParagraph para = cell.addParagraph(); para.setAlignment(ParagraphAlignment.CENTER); // 水平居中 // 设置标题样式 XWPFRun run = para.createRun(); run.setBold(true); run.setFontSize(12); // 标题行字体放大 run.setText(getHeaderText(i)); run.setColor("000000"); } // ========== 4. 数据行样式(水平居中) ========== int num = 0; for (EntityItem record : records) { XWPFTableRow row = table.createRow(); row.setCantSplitRow(true); // 序号列(水平居中) createCenteredCell(row, 0, String.valueOf(++num)); // 工装编号列(水平居中) createCenteredCell(row, 1, record.getToolNumber()); // 资产名称列(水平居中) createCenteredCell(row, 2, record.getToolName()); // 图片列1(左对齐) XWPFTableCell imageCell = row.getCell(3); imageCell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(columnWidths[3])); addImagesToCell(imageCell, record.getPropertyUri(), "D:\\imageFile\\"); // 图片列2(左对齐) XWPFTableCell nameplateCell = row.getCell(4); nameplateCell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(columnWidths[4])); addImagesToCell(nameplateCell, record.getNameplateUri(), "D:\\imageFile\\"); } // ========== 5. 添加页码 ========== addPageNumbers(doc); // ========== 6. 保存文档 ========== try (FileOutputStream out = new FileOutputStream(outputPath)) { doc.write(out); } } } // 辅助方法:创建水平居中单元格 private static void createCenteredCell(XWPFTableRow row, int pos, String text) { XWPFTableCell cell = row.getCell(pos); if (cell == null) cell = row.createCell(); cell.removeParagraph(0); XWPFParagraph para = cell.addParagraph(); para.setAlignment(ParagraphAlignment.CENTER); // 水平居中 XWPFRun run = para.createRun(); run.setText(text); } // 辅助方法:添加图片到单元格(左对齐) private static void addImagesToCell(XWPFTableCell cell, List<String> imagePaths, String basePath) throws Exception { XWPFParagraph para = cell.addParagraph(); para.setAlignment(ParagraphAlignment.LEFT); // 图片左对齐 XWPFRun run = para.createRun(); for (int i = 0; i < imagePaths.size(); i++) { try (java.io.InputStream is = new java.io.FileInputStream(basePath + imagePaths.get(i))) { run.addPicture(is, XWPFDocument.PICTURE_TYPE_PNG, imagePaths.get(i), Units.toEMU(100), Units.toEMU(100)); // 每3张图片换行 if ((i + 1) % 3 == 0 && i != imagePaths.size() - 1) { run.addBreak(); } } } } // 表头文本 private static String getHeaderText(int colIndex) { String[] headers = {"序号", "工装编号", "资产名称", "关联图片", "铭牌"}; return headers[colIndex]; } // 页码添加 private static void addPageNumbers(XWPFDocument doc) { XWPFFooter footer = doc.createFooter(HeaderFooterType.DEFAULT); XWPFParagraph para = footer.createParagraph(); para.setAlignment(ParagraphAlignment.CENTER); XWPFRun run = para.createRun(); run.getCTR().addNewFldChar().setFldCharType(STFldCharType.BEGIN); run.getCTR().addNewInstrText().setStringValue("PAGE"); run.getCTR().addNewFldChar().setFldCharType(STFldCharType.END); run.setText(" / "); run.getCTR().addNewFldChar().setFldCharType(STFldCharType.BEGIN); run.getCTR().addNewInstrText().setStringValue("NUMPAGES"); run.getCTR().addNewFldChar().setFldCharType(STFldCharType.END); }根据代码修改为每页5行数据包含表头,一个图片单元格横向最多放3张图片多了之后换行,如果图片换行也占用一行总行数,如果图片换行后不在当前页整条数据都移动至下一页
最新发布
07-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值