玩转easypoi 就是这么简单 直接上代码。
依赖注入:
<!-- easyPoi -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
代码如下:
/**
* 生成日历
*
* @param item 索引
* @param size
*/
public void insertCalender(XWPFDocument doc, ...) {
String key = null;
if (size < 10) {//避免占位符冲突
key = "$0" + item;
} else {
key = "$" + item;
}
List<XWPFParagraph> paragraphList = doc.getParagraphs();
if (paragraphList != null && paragraphList.size() > 0) {
for (XWPFParagraph paragraph : paragraphList) {
List<XWPFRun> runs = paragraph.getRuns();
for (int i = 0; i < runs.size(); i++) {
String text = runs.get(i).getText(0).trim();
if (text != null) {
if (text.indexOf(key) >= 0) {
runs.get(i).setText(text.replace(key, ""), 0);
XmlCursor cursor = paragraph.getCTP().newCursor();
// 在指定游标位置插入表格
XWPFTable table = doc.insertNewTbl(cursor);
table.setTableAlignment(TableRowAlign.CENTER);
List<> calendarVOList = ;
insertCalendar(table, calendarVOList, localDate, item, size, beginDate, endDate);
}
}
}
}
}
}
/**
* 插入日历表
*
* @param table
* @param calendarVOList
*/
public static void insertCalendar(XWPFTable table, List<AdminSysCalendarOrganizationVO> calendarVOList, ...) {
//边框
table.setInsideHBorder(XWPFTable.XWPFBorderType.SINGLE, 4, 0, calendarTextBoder);
table.setInsideVBorder(XWPFTable.XWPFBorderType.SINGLE, 4, 0, calendarTextBoder);
table.setLeftBorder(XWPFTable.XWPFBorderType.SINGLE, 4, 0, calendarTextBoder);
table.setRightBorder(XWPFTable.XWPFBorderType.SINGLE, 4, 0, calendarTextBoder);
table.setTopBorder(XWPFTable.XWPFBorderType.SINGLE, 4, 0, calendarTextBoder);
table.setBottomBorder(XWPFTable.XWPFBorderType.SINGLE, 4, 0, calendarTextBoder);
XWPFTableRow row = table.getRow(0);
XWPFTableCell cell = null;
for (int col = 1; col < calendar.length; col++) {
// 第一行创建了多少列,后续增加的行自动增加列
CTTcPr cPr = row.createCell().getCTTc().addNewTcPr();
CTTblWidth width = cPr.addNewTcW();
width.setW(BigInteger.valueOf(1100));
}
//遍历title
for (int t = 0; t < calendar.length; t++) {
cell = row.getCell(t);
row.setHeight(500);
setCellText(cell, calendar[t], calendarTitleColor, 1100);
}
int beginWeek = calendarVOList.get(0).getWeekday();
int rows = 0;
if ((calendarVOList.size() + beginWeek) % 7 == 0) {
rows = (calendarVOList.size() + beginWeek) / 7;
} else {
rows = ((calendarVOList.size() + beginWeek) / 7) + 1;
}
for (int t = 0; t < rows * 2 - 1; t++) {
row = table.createRow();
row.setHeight(400);
for (int r = 0; r < 7; r++) {
int index = (t / 2) * 7 + r;
int indexAdjust = index;
if (indexAdjust >= beginWeek) {
indexAdjust -= beginWeek;
}
String text = "";
String textColor = calendarTextColor_black;
String bgColor = calendarTextColor_blue;//单元格背景色
if (t % 2 == 0 && index >= beginWeek && indexAdjust < calendarVOList.size()) {
text = calendarVOList.get(indexAdjust).getDay().toString();
if (calendarVOList.get(indexAdjust).getHoliday().equals(String.valueOf(Constant.CALENDAR_HOLIDAY_LEGAL_HOLIDAY_DATE))) {
text = text.concat("s(").concat(calendarVOList.get(indexAdjust).getDescription()).concat(")");
bgColor = calendarTextColor_blue;
textColor = calendarTextColor_red;
}
... ...
//此处需要设置不包含的天数单元格背景色置灰&字体为白
//数据格式:2021-01-15,2021-02-01,2021-03-01,截至时间需要取续课截止日期
int currentDay = calendarVOList.get(indexAdjust).getDay();
if (size > 1){
if ((item < size-1 && currentDay < currentDate.getDayOfMonth()) || (item == size-1 && currentDay > endDate.getDayOfMonth())){
bgColor = calendarTextColor_grey;
textColor = calendarTextColor_white2;
}
}else{
if (currentDay < beginDate.getDayOfMonth() || currentDay > endDate.getDayOfMonth()){
bgColor = calendarTextColor_grey;
textColor = calendarTextColor_white2;
}
}
}
if (t % 2 == 1) {
bgColor = calendarTextColor_white;
}
cell = row.getCell(r);
setCellText2(cell, text, bgColor, textColor, 1100);
}
}
}
//cell填值
public static void setCellText2(XWPFTableCell cell, String text, String bgcolor, String textColor, int width) {
CTTc cttc = cell.getCTTc();
CTTcPr cellPr = cttc.addNewTcPr();
cellPr.addNewTcW().setW(BigInteger.valueOf(width));
CTTcPr ctPr = cttc.addNewTcPr();
CTShd ctshd = ctPr.addNewShd();
ctshd.setFill(bgcolor);
ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
XWPFParagraph para = cell.getParagraphs().get(0);
XWPFRun run = para.createRun();
run.setColor(textColor);
String[] textArr = text.split("s");
for (int i = 0; i < textArr.length; i++) {
if (i > 0) {
run.setColor(textColor);//CB4335
run.setFontSize(10);
run.addBreak();
}
run.setText(textArr[i]);
}
}