package com.example.project.project.car.util;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.*;
public class ExportWordUtil {
public static void main(String[] args) throws IOException {
LinkedHashMap<String, List<Map<String,String>>> objectObjectLinkedHashMap = new LinkedHashMap<>();
ArrayList<Map<String,String>> objects = new ArrayList<>();
Map<String, String> map = new HashMap<>();
map.put("title","title1");
map.put("content","content1");
objects.add(map);
map.put("title","title2");
map.put("content","content2");
objects.add(map);
objectObjectLinkedHashMap.put("aa",objects);
exportWord("1111111111111111111", objectObjectLinkedHashMap);
}
public static void exportWord(String str, LinkedHashMap<String, List<Map<String,String>>> maps) throws IOException {
XWPFDocument document= new XWPFDocument();
//添加标题
XWPFParagraph titleParagraph = document.createParagraph();
//设置段落居中
titleParagraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun titleParagraphRun = titleParagraph.createRun();
titleParagraphRun.setText("title");
titleParagraphRun.setColor("000000");
titleParagraphRun.setFontSize(20);
//段落
XWPFParagraph firstParagraph = document.createParagraph();
XWPFRun run = firstParagraph.createRun();
run.setText(str);
run.setColor("696969");
run.setFontSize(10);
//换行
XWPFParagraph paragraph1 = document.createParagraph();
XWPFRun paragraphRun1 = paragraph1.createRun();
paragraphRun1.setText("\r");
//工作经历表格
XWPFTable ComTable = document.createTable();
//列宽自动分割
CTTblWidth comTableWidth = ComTable.getCTTbl().addNewTblPr().addNewTblW();
comTableWidth.setType(STTblWidth.DXA);
comTableWidth.setW(BigInteger.valueOf(9072));
//表格第一行
XWPFTableRow comTableRowOne = ComTable.getRow(0);
comTableRowOne.getCell(0).setText("开始时间");
comTableRowOne.addNewTableCell().setText("结束时间");
comTableRowOne.addNewTableCell().setText("公司名称");
comTableRowOne.addNewTableCell().setText("title");
int i=0;
for(String key:maps.keySet()){
List<Map<String, String>> maps1 = maps.get(key);
int size = maps1.size();
for(Map<String,String> map2:maps1){
i++;
XWPFTableRow comTableRowTwo = ComTable.createRow();
comTableRowTwo.getCell(0).setText(key);
comTableRowTwo.getCell(1).setText(map2.get("title"));
comTableRowTwo.getCell(2).setText(map2.get("content"));
}
if(size>1){
//合并单元格建立在需要合并的单元格已经创建的基础上
//行合并参数:表格,列,开始行,结束行
mergeCellsVertically(ComTable, 0, i-size+1, i);
}
}
FileOutputStream output=new FileOutputStream("D://a.doc");
document.write(output);
output.close();
System.out.println("create_table document written success.");
}
/**
* word单元格列合并
* @param table 表格
* @param row 合并列所在行
* @param startCell 开始列
* @param endCell 结束列
* @date 2020年4月8日 下午4:43:54
*/
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) {
// The first merged cell is set with RESTART merge value
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
} else {
// Cells which join (merge) the first one, are set with CONTINUE
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
}
}
/**
* word单元格行合并
* @param table 表格
* @param col 合并行所在列
* @param startRow 开始行
* @param endRow 结束行
* @date 2020年4月8日 下午4:46:18
*/
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) {
// The first merged cell is set with RESTART merge value
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
} else {
// Cells which join (merge) the first one, are set with CONTINUE
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
}
}
}
}
poi导出word,合并word单元格
最新推荐文章于 2024-05-13 17:47:15 发布