赞
踩
导入的maven依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
图片传入
// 图片写入表格的cell(单元格),传入的参数:需要传入图片的cell,图片宽度,图片高度,图片链接地址 public static void handlePhoto(XWPFTableCell cell,int width,int height,String picPath){ XWPFParagraph p = cell.getParagraphArray(0); if (p == null) { p = cell.addParagraph(); } p.setAlignment(ParagraphAlignment.CENTER); p.setVerticalAlignment(TextAlignment.CENTER); XWPFRun photos = p.createRun(); try { // 图片地址为参数的picPath // 测试用图 picPath = "https://img-blog.csdnimg.cn/img_convert/b26750936b8d00e60053e7dda335f9aa.png"; // 截取图片后缀名 String houzhui = picPath.substring(picPath.length()-3,picPath.length()); if (picPath.equals("无"))cell.setText("无"); // 调用下面网络图片流到本地字符获取的方法(1) byte[] byteArray = ImgFile(picPath); // 如果链接也找不到文件(404) 显示找不到文件 if (byteArray == null) cell.setText("找不到文件"); if (houzhui.equals("jpg")){ photos.addPicture(new ByteArrayInputStream(byteArray), Document.PICTURE_TYPE_JPEG, picPath, Units.toEMU(width), Units.toEMU(height)); }else if (houzhui.equals("png")){ photos.addPicture(new ByteArrayInputStream(byteArray), Document.PICTURE_TYPE_PNG, picPath, Units.toEMU(width), Units.toEMU(height)); } } catch (Exception e) { // 链接的图片找不到,单元格内写入"找不到文件" cell.setText("找不到文件"); } } //链接形式的图片提取 传入图片链接地址 (1) public static byte[] ImgFile(String path) throws Exception { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); InputStream inputStream = conn.getInputStream();//通过输入流获取图片数据 byte[] data = readInputStream(inputStream);//调用方法(2)得到图片的二进制数据 //最终返回处理好的数据 return data; } //拿到图片后的处理 (2) public static byte[] readInputStream(InputStream inputStream) throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while((len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len);//写到内存 } inputStream.close(); return outputStream.toByteArray(); }
合并单元格
// 横向合并单元格 // 传入的参数: (哪行进行合并,开始的单元格下标,合并到哪个单元格的下标) public static void mergeCells(XWPFTableRow row, int openCell, int endCell){ //合并单元格 for (int cellIndex = openCell; cellIndex <= endCell; cellIndex++) { XWPFTableCell cellMerge = row.getCell(cellIndex); if ( cellIndex == openCell ) { // The first merged cell is set with RESTART merge value cellMerge.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART); } else { // Cells which join (merge) the first one, are set with CONTINUE cellMerge.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE); } } } // 竖向合并单元格 传参:要合并的表格对象、合并列的下标、开始合并行的下标、合并到第几行的下标 public static void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) { for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) { XWPFTableCell cell = table.getRow(rowIndex).getCell(col); if ( rowIndex == fromRow ) { cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART); } else { cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE); } cellCenter(cell); } }
设置表格边框
// 设置表格边框(可自己传样式参数) public static void tableBorder(CTTblBorders borders,String type){ // String bolderType = "single"; // 设置上下左右外边框 // 左边框 CTBorder lBorder = borders.addNewLeft(); lBorder.setVal(STBorder.Enum.forString(type)); lBorder.setSz(new BigInteger("1")); lBorder.setColor("000000"); // 右边框 CTBorder rBorder = borders.addNewRight(); rBorder.setVal(STBorder.Enum.forString(type)); rBorder.setSz(new BigInteger("1")); rBorder.setColor("000000"); // 上边框 CTBorder tBorder = borders.addNewTop(); tBorder.setVal(STBorder.Enum.forString(type)); tBorder.setSz(new BigInteger("1")); tBorder.setColor("000000"); // 下边框 CTBorder bBorder = borders.addNewBottom(); bBorder.setVal(STBorder.Enum.forString(type)); bBorder.setSz(new BigInteger("1")); bBorder.setColor("000000"); }
设置单元格边框
// 设置单元格边框(可传样式) public static void tableBorder3(CTTcBorders border,String type){ //String bolderType = "thinThickMediumGap"; // 左边框 CTBorder lBorder = border.addNewLeft(); lBorder.setVal(STBorder.Enum.forString(type)); lBorder.setSz(new BigInteger("0")); lBorder.setColor("000000"); // 右边框 CTBorder rBorder = border.addNewRight(); rBorder.setVal(STBorder.Enum.forString(type)); rBorder.setSz(new BigInteger("0")); rBorder.setColor("000000"); // 上边框 CTBorder tBorder = border.addNewTop(); tBorder.setVal(STBorder.Enum.forString(type)); tBorder.setSz(new BigInteger("0")); tBorder.setColor("000000"); CTBorder bBorder = border.addNewBottom(); bBorder.setVal(STBorder.Enum.forString(type)); bBorder.setSz(new BigInteger("1")); bBorder.setColor("000000"); }
添加分页符
// 添加分页符 传入一个XWPFParagraph,XWPFParagraph 可以直接doc.createParagraph()来创建
public static void fenye(XWPFParagraph p){
p.setPageBreak(true);
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。