使用poi增加word行和列
先上一点简单的dome:复制指定行
注意、不管是表格数量、还是行列数量、都是从0计算。
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
public static void main(String[] args) throws IOException {
XWPFDocument document = new XWPFDocument(new FileInputStream("C:\\upload\\dome\\1.docx"));
XWPFTable xwpfTable = document.getTables().get(0);//这里代表获取word里面第一个表格。
//xwpfTable.createRow();增加列
xwpfTable.addRow(xwpfTable.getRow(5),9);//代表复制第5行,复制到第9行
xwpfTable.addRow(xwpfTable.getRow(6),10);
xwpfTable.addRow(xwpfTable.getRow(7),11);
xwpfTable.addRow(xwpfTable.getRow(8),12);
String string ="C:\\file\\12.docx";
FileOutputStream out = new FileOutputStream(string);
document.write(out);
document.close();
out.close();
}
合并单元格dome
public static void main(String[] args) throws IOException {
XWPFDocument document = new XWPFDocument(new FileInputStream("C:\\file\\1.docx"));
XWPFTable xwpfTable = document.getTables().get(0);
//行合并参数:表格,列,开始行,结束行
mergeCellsVertically(xwpfTable, 0, 5, 12);
///列合并参数:表格,行,开始列,结束列
//mergeCellsHorizontal(table, 4, 0, 2);
String string ="C:\\file\\12.docx";
FileOutputStream out = new FileOutputStream(string);
document.write(out);
document.close();
out.close();
}
/**
* word单元格行合并
* @param table 表格
* @param col 合并行所在列
* @param fromRow 开始行
* @param toRow 结束行
*/
public static void mergeCellsVertically(XWPFTable table, int col, int startRow, int endRow) {
for (int i = startRow; i <= endRow; i++) {
XWPFTableCell cell = table.getRow(i).getCell(col);
if (i == startRow) {
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
} else {
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
}
}
}
/**
* word单元格列合并
* @param table 表格
* @param row 合并列所在行
* @param startCell 开始列
* @param endCell 结束列
*/
public static void mergeCellsHorizontal(XWPFTable table, int row, int startCell, int endCell) {
for (int i = startCell; i <= endCell; i++) {
XWPFTableCell cell = table.getRow(row).getCell(i);
if (i == startCell) {
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
} else {
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
}
}
循环增加标签、以便于后期插入数据dome
public static void main(String[] args) throws IOException {
XWPFDocument document = new XWPFDocument(new FileInputStream("C:\\file\\123.docx"));
XWPFTable xwpfTable = document.getTables().get(0);
//直接增加文字
xwpfTable.getRow(0).getCell(1).setText("隐患"+w);
//增加文字标签
xwpfTable.getRow(0).getCell(3).setText("{{att}}");
//增加插入图片标签 一定要加@
xwpfTable.getRow(0).getCell(3).setText("{{@att1}}");
String string ="C:\\file\\12.docx";
FileOutputStream out = new FileOutputStream(string);
document.write(out);
document.close();
out.close();
}
对指定标签增加数据与图片
。
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.8.2</version>
</dependency>
public static void main(String[] args) throws IOException {
Map<String,Object> map = new HashMap<>();
try {
//测试代码
String destination = RestConfig.PATH+":\\upload\\dome\\jpgzgq\\12.jpg";
File file=new File(destination);
if(file.exists()){
file.delete();
}
FileOutputStream downloadFile = new FileOutputStream(destination);
downloadFile.write(b);
downloadFile.flush();
downloadFile.close();
File inputFile = new File(destination);
InputStream input;
input = new FileInputStream(inputFile);
byte[] byteArrayImage=readBytesFromIS(input);
input.read(byteArrayImage);
InputStream input3 = new ByteArrayInputStream(byteArrayImage);
BufferedImage inputBufImage = ImageIO.read(input3);
int w = inputBufImage.getWidth();
int h = inputBufImage.getHeight();
map.put("photo", new PictureRenderData(450, 450, destination));
} catch (Exception e) {
e.printStackTrace();
//response.setContentType("application/text;charset=UTF-8");
}finally {
}
map.put("att1","123");
String dome = "C:\\upload\\dome\\12.docx";
String dome1 = "C:\\upload\\dome\\file\\123.docx";
map.put("dome",dome);
map.put("dome1",dome1);
String path = saveWord(map);
}
public String saveWord(Map<String, Object> map) throws Exception
File file = new File(map.get("dome").toString());
XWPFTemplate template = XWPFTemplate.compile(file).render(map);
String path = map.get("dome1").toString();
FileOutputStream out = new FileOutputStream(new File(path));
template.write(out);
out.flush();
out.close();
template.close();
return path;
}
如果是图片的为{{@picture}}
代码是从业务逻辑删除过来的、基本是可用的、只要了解poi的属性、很容易可以做到、如果代码有不通的地方、欢迎留言。