poi使用心得

1、页面传参注意事项

在页面箱后台请求导出excel的时候,需要注意的时,不能使用ajax来发送请求,因为ajax是局部刷新,而导出文件是整个页面的事情,可以使用: 在这里插入图片描述
在使用window.location.href时要注意编码的问题,否则很容易传递乱码。

2、后台代码的编写

首先要明白poi的基本概念。
excel对象:HSSFWorkbook
sheet对象:HSSFSheet
row对象:HSSFRow
cell对象:HSSFCell

编程的思路:我们的exce都是先去获取excel,再通过这个对象去创建表对象(sheet),在去创建行对象(row),由行对象在去创建列对象(cell)

注意:excel坐标以(0,0)开头。下面是列子

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POI(Apache POI)是一个用于读写 Microsoft Office 格式文件的 Java 库。使用 POI 可以方便地读取、创建和修改 Word、Excel 和 PowerPoint 文档。 使用 POI 创建 Word 文档时,可以使用 Word 模板来实现快速生成具有相同格式和布局的文档。 以下是使用 POI 使用 Word 模板创建 Word 文档的步骤: 1. 创建一个空的 Word 文档对象 ``` XWPFDocument doc = new XWPFDocument(); ``` 2. 打开 Word 模板文件 ``` FileInputStream inputStream = new FileInputStream("template.docx"); XWPFDocument templateDoc = new XWPFDocument(inputStream); ``` 3. 遍历模板中的段落,并复制到新的 Word 文档对象中 ``` for (XWPFParagraph templateParagraph : templateDoc.getParagraphs()) { XWPFParagraph newParagraph = doc.createParagraph(); newParagraph.getCTP().setPPr(templateParagraph.getCTP().getPPr()); for (XWPFRun templateRun : templateParagraph.getRuns()) { XWPFRun newRun = newParagraph.createRun(); newRun.getCTR().setRPr(templateRun.getCTR().getRPr()); newRun.setText(templateRun.getText(0)); } } ``` 4. 遍历模板中的表格,并复制到新的 Word 文档对象中 ``` for (XWPFTable templateTable : templateDoc.getTables()) { XWPFTable newTable = doc.createTable(); newTable.getCTTbl().setTblPr(templateTable.getCTTbl().getTblPr()); for (int i = 0; i < templateTable.getRows().size(); i++) { XWPFTableRow templateRow = templateTable.getRow(i); XWPFTableRow newRow = newTable.createRow(); newRow.getCTRow().setTrPr(templateRow.getCTRow().getTrPr()); for (int j = 0; j < templateRow.getTableCells().size(); j++) { XWPFTableCell templateCell = templateRow.getCell(j); XWPFTableCell newCell = newRow.getCell(j); newCell.getCTTc().setTcPr(templateCell.getCTTc().getTcPr()); newCell.setText(templateCell.getText()); } } } ``` 5. 关闭 Word 模板文件 ``` templateDoc.close(); ``` 6. 保存新的 Word 文档对象到文件 ``` FileOutputStream outputStream = new FileOutputStream("output.docx"); doc.write(outputStream); outputStream.close(); ``` 注意:以上代码中的 `template.docx` 是 Word 模板文件的路径,`output.docx` 是生成的新 Word 文档文件的路径。在复制段落和表格时,需要注意复制段落和表格的格式和样式,以确保生成的新文档与模板文档的格式和样式一致。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值