java实现导出word操作
实现代码
···
package cn.ttitcn.xj.controller;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableCell.XWPFVertAlign;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcBorders;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.STBorderImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.hutool.core.convert.Convert;
import cn.ttitcn.common.annotation.Log;
import cn.ttitcn.common.constant.Constants;
import cn.ttitcn.common.core.domain.AjaxResult;
import cn.ttitcn.common.enums.BusinessType;
import cn.ttitcn.common.util.ExcelUtil;
import cn.ttitcn.common.util.StringUtils;
import cn.ttitcn.framework.web.BaseController;
@RestController
@RequestMapping(“xj/studentroll”)
public class StudentrollController extends BaseController {
/**
* 导出excel
*
* @param studentroll
* @return
*/
@Log(title = XJConstant.XJ_STUDENTROLLE, businessType = BusinessType.EXPORT)
@PreAuthorize("@ss.hasPermi('xj:studentroll:export')")
@PostMapping("export")
public AjaxResult export(@RequestBody Studentroll studentroll) {
List<Studentroll> list = studentrollService.list(studentroll);
ExcelUtil<Studentroll> util = new ExcelUtil<Studentroll>(Studentroll.class);
return util.exportExcel(list, "学籍表");
}
/**
* 导入
*
* @param studentroll
* @return
*/
@Log(title = XJConstant.XJ_STUDENTROLLE, businessType = BusinessType.IMPORT)
@PreAuthorize("@ss.hasPermi('xj:studentroll:import')")
@PostMapping("import")
public AjaxResult import1(@RequestBody Studentroll studentroll) {
return success();
}
/**
* 导出word(一个学生占一页word)
*
* @param studentroll
* @return
*/
@Log(title = XJConstant.XJ_STUDENTROLLE, businessType = BusinessType.EXPORT)
@PreAuthorize("@ss.hasPermi('xj:studentroll:export')")
@PostMapping("exportWord")
public AjaxResult exportWord(@RequestBody(required = false) Studentroll studentroll) {
try {
createSimpleTable("D:/"+ System.currentTimeMillis() + ".docx");
} catch (Exception e) {
e.printStackTrace();
}
return success();
}
public static void main(String[] args) {
try {
createSimpleTable("D:/"+ System.currentTimeMillis() + ".docx");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void createSimpleTable(String savePath) throws Exception {
XWPFDocument xdoc = new XWPFDocument();
XWPFParagraph xp = xdoc.createParagraph();
xp.setSpacingBefore(0);
XWPFRun r1 = xp.createRun();
r1.setText("学生个人学籍信息");
r1.addBreak(); // 换行
r1.setFontFamily("宋体");
r1.setFontSize(16);
r1.setTextPosition(10);
r1.setBold(true);
xp.setAlignment(ParagraphAlignment.CENTER);
Integer col_total_count = 4; // 表格最多的列数
Integer data_count = 10; // 需要创建的总条数
XWPFTable xTable = xdoc.createTable(1, col_total_count);
CTTbl ttbl = xTable.getCTTbl();
CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();
CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
tblWidth.setW(new BigInteger("8600"));
tblWidth.setType(STTblWidth.DXA);
/**创建表头数据*/
int i = 0;
xTable.getRow(i).setHeight(500);
setCellText(xdoc, xTable.getRow(i).getCell(0), "姓名:", "FFFFFF", getCellWidth(0));
setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
setCellText(xdoc, xTable.getRow(i).getCell(2), "学号:", "FFFFFF", getCellWidth(2));
setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
// 创建表格内容
i++;
/**向表中添加新行*/
XWPFTableRow rw8 = xTable.insertNewTableRow(i);
for (int j = 0; j < 4; j++) {
rw8.addNewTableCell();
}
rw8.setHeight(500);
setCellText(xdoc, xTable.getRow(i).getCell(0), "家庭成员联系电话:", "FFFFFF", getCellWidth(0));
setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
setCellText(xdoc, xTable.getRow(i).getCell(2), "家庭成员工作单位:", "FFFFFF", getCellWidth(2));
setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
/**向表中添加新行*/
XWPFTableRow rw7 = xTable.insertNewTableRow(i);
for (int j = 0; j < 4; j++) {
rw7.addNewTableCell();
}
rw7.setHeight(500);
setCellText(xdoc, xTable.getRow(i).getCell(0), "家庭成员姓名:", "FFFFFF", getCellWidth(0));
setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
setCellText(xdoc, xTable.getRow(i).getCell(2), "家庭成员关系:", "FFFFFF", getCellWidth(2));
setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
/**向表中添加新行*/
XWPFTableRow rw6 = xTable.insertNewTableRow(i);
for (int j = 0; j < 4; j++) {
rw6.addNewTableCell();
}
rw6.setHeight(500);
setCellText(xdoc, xTable.getRow(i).getCell(0), "是否建档立卡贫困户:", "FFFFFF", getCellWidth(0));
setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
setCellText(xdoc, xTable.getRow(i).getCell(2), "健康状况:", "FFFFFF", getCellWidth(2));
setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
/**向表中添加新行*/
XWPFTableRow rw5 = xTable.insertNewTableRow(i);
for (int j = 0; j < 4; j++) {
rw5.addNewTableCell();
}
rw5.setHeight(500);
setCellText(xdoc, xTable.getRow(i).getCell(0), "户口详细地址:", "FFFFFF", getCellWidth(0));
mergeCellsHorizontal(xTable,i, 1, 3);
/**向表中添加新行*/
XWPFTableRow rw15 = xTable.insertNewTableRow(i);
for (int j = 0; j < 4; j++) {
rw15.addNewTableCell();
}
rw15.setHeight(500);
setCellText(xdoc, xTable.getRow(i).getCell(0), "家庭现住址:", "FFFFFF", getCellWidth(0));
mergeCellsHorizontal(xTable,i, 1, 3);
/**向表中添加新行*/
XWPFTableRow rw14 = xTable.insertNewTableRow(i);
for (int j = 0; j < 4; j++) {
rw14.addNewTableCell();
}
rw14.setHeight(500);
setCellText(xdoc, xTable.getRow(i).getCell(0), "寄信地址:", "FFFFFF", getCellWidth(0));
mergeCellsHorizontal(xTable,i, 1, 3);
// /*测试------寄信地址添加单独一行/
// XWPFTableRow rw14 = xTable.insertNewTableRow(i);
// for (int j = 0; j < 4; j++) {
// rw14.addNewTableCell();
// }
// rw14.setHeight(500);
// setCellText(xdoc, xTable.getRow(i).getCell(0), “寄信地址:”, “FFFFFF”, getCellWidth(0));
// //mergeCellsVertically(xTable, 1, 3, 5);
// mergeCellsHorizontal(xTable,i, 1, 3);
// //mergeCellsHorizontal(xTable, i, i, i);
/**向表中添加新行*/
XWPFTableRow rw9 = xTable.insertNewTableRow(i);
for (int j = 0; j < 4; j++) {
rw9.addNewTableCell();
}
rw9.setHeight(500);
setCellText(xdoc, xTable.getRow(i).getCell(0), "出生地:", "FFFFFF", getCellWidth(0));
setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
setCellText(xdoc, xTable.getRow(i).getCell(2), "就读学校类型:", "FFFFFF", getCellWidth(2));
setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
/**向表中添加新行*/
XWPFTableRow rw13 = xTable.insertNewTableRow(i);
for (int j = 0; j < 4; j++) {
rw13.addNewTableCell();
}
rw13.setHeight(500);
setCellText(xdoc, xTable.getRow(i).getCell(0), "学生手机号码:", "FFFFFF", getCellWidth(0));
setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
setCellText(xdoc, xTable.getRow(i).getCell(2), "户口性质:", "FFFFFF", getCellWidth(2));
setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
/**向表中添加新行*/
XWPFTableRow rw12 = xTable.insertNewTableRow(i);
for (int j = 0; j < 4; j++) {
rw12.addNewTableCell();
}
rw12.setHeight(500);
setCellText(xdoc, xTable.getRow(i).getCell(0), "籍贯:", "FFFFFF", getCellWidth(0));
setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
setCellText(xdoc, xTable.getRow(i).getCell(2), "邮政编码:", "FFFFFF", getCellWidth(2));
setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
/**向表中添加新行*/
XWPFTableRow rw10 = xTable.insertNewTableRow(i);
for (int j = 0; j < 4; j++) {
rw10.addNewTableCell();
}
rw10.setHeight(500);
setCellText(xdoc, xTable.getRow(i).getCell(0), "入学方式:", "FFFFFF", getCellWidth(0));
setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
setCellText(xdoc, xTable.getRow(i).getCell(2), "就读方式:", "FFFFFF", getCellWidth(2));
setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
/**向表中添加新行*/
XWPFTableRow rw2 = xTable.insertNewTableRow(i);
for (int j = 0; j < 4; j++) {
rw2.addNewTableCell();
}
rw2.setHeight(500);
setCellText(xdoc, xTable.getRow(i).getCell(0), "身份证号:", "FFFFFF", getCellWidth(0));
setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
setCellText(xdoc, xTable.getRow(i).getCell(2), "出生日期:", "FFFFFF", getCellWidth(2));
setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
/**向表中添加新行*/
XWPFTableRow rw4 = xTable.insertNewTableRow(i);
for (int j = 0; j < 4; j++) {
rw4.addNewTableCell();
}
rw4.setHeight(500);
setCellText(xdoc, xTable.getRow(i).getCell(0), "政治面貌:", "FFFFFF", getCellWidth(0));
setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
setCellText(xdoc, xTable.getRow(i).getCell(2), "学籍号:", "FFFFFF", getCellWidth(2));
setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
/**向表中添加新行*/
XWPFTableRow rw11 = xTable.insertNewTableRow(i);
for (int j = 0; j < 4; j++) {
rw11.addNewTableCell();
}
rw11.setHeight(500);
setCellText(xdoc, xTable.getRow(i).getCell(0), "专业:", "FFFFFF", getCellWidth(0));
setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
setCellText(xdoc, xTable.getRow(i).getCell(2), "班级:", "FFFFFF", getCellWidth(2));
setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
/**向表中添加新行*/
XWPFTableRow rw = xTable.insertNewTableRow(i);
for (int j = 0; j < 4; j++) {
rw.addNewTableCell();
}
rw.setHeight(500);
setCellText(xdoc, xTable.getRow(i).getCell(0), "性别:", "FFFFFF", getCellWidth(0));
setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
setCellText(xdoc, xTable.getRow(i).getCell(2), "证件类型:", "FFFFFF", getCellWidth(2));
setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
/**向表中添加新行*/
XWPFTableRow rw3 = xTable.insertNewTableRow(i);
for (int j = 0; j < 4; j++) {
rw3.addNewTableCell();
}
rw3.setHeight(500);
setCellText(xdoc, xTable.getRow(i).getCell(0), "民族:", "FFFFFF", getCellWidth(0));
setCellText(xdoc, xTable.getRow(i).getCell(1), " ", "FFFFFF", getCellWidth(1));
setCellText(xdoc, xTable.getRow(i).getCell(2), "国籍/地区:", "FFFFFF", getCellWidth(2));
setCellText(xdoc, xTable.getRow(i).getCell(3), " ", "FFFFFF", getCellWidth(3));
FileOutputStream fos = new FileOutputStream(savePath);
xdoc.write(fos);
fos.close();
}
/**
* 设置表头内容
* @param xDocument
* @param cell
* @param text
* @param bgcolor
* @param width
*/
private static void setCellText(XWPFDocument xDocument, XWPFTableCell cell, String text, String bgcolor,
int width) {
CTTc cttc = cell.getCTTc();
CTTcPr cellPr = cttc.addNewTcPr();
cellPr.addNewTcW().setW(BigInteger.valueOf(width));
cell.setColor(bgcolor);
cell.setVerticalAlignment(XWPFVertAlign.CENTER);
CTTcPr ctPr = cttc.addNewTcPr();
ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
//cell.getCTTc().getTcPr().getTcBorders().addNewRight().setVal();
cell.setText(text);
}
/**
* 设置列宽
*
* @param index
* @return
*/
private static int getCellWidth(int index) {
int cwidth = 1000;
if (index == 0) {
cwidth = 1800;
} else if (index == 1) {
cwidth = 1800;
} else if (index == 2) {
cwidth = 1800;
} else if (index == 3) {
cwidth = 1800;
}
return cwidth;
}
/***
* 跨行合并
* @param table
* @param col 合并列
* @param fromRow 起始行
* @param toRow 终止行
*/
private 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);
}
}
}
/***
* 跨列合并
* @param table
* @param row 所合并的行
* @param fromCell 起始列
* @param toCell 终止列
*/
private static void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
if ( cellIndex == fromCell ) {
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
} else {
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
}
}
}
···