!318 hutool-poi修改

Merge pull request !318 from 有望得道/v5-dev
This commit is contained in:
Looly 2021-05-09 00:52:38 +08:00 committed by Gitee
commit 235d32043e
3 changed files with 109 additions and 0 deletions

View File

@ -38,6 +38,7 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -731,6 +732,31 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
return this; return this;
} }
/**
* 合并单元格并写入对象到单元格,使用指定的样式<br>
* 指定样式传入null则不使用任何样式
*
* @param firstRow 起始行0开始
* @param lastRow 结束行0开始
* @param firstColumn 起始列0开始
* @param lastColumn 结束列0开始
* @param content 合并单元格后的内容
* @param cellStyle 合并后单元格使用的样式可以为null
* @return this
*/
public ExcelWriter merge(int firstRow, int lastRow, int firstColumn, int lastColumn, Object content, CellStyle cellStyle) {
Assert.isFalse(this.isClosed, "ExcelWriter has been closed!");
CellUtil.mergingCells(this.getSheet(), firstRow, lastRow, firstColumn, lastColumn, cellStyle);
// 设置内容
if (null != content) {
final Cell cell = getOrCreateCell(firstColumn, firstRow);
CellUtil.setCellValue(cell, content, cellStyle);
}
return this;
}
/** /**
* 写出数据本方法只是将数据写入Workbook中的Sheet并不写出到文件<br> * 写出数据本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
* 写出的起始行为当前行号可使用{@link #getCurrentRow()}方法调用根据写出的的行数当前行号自动增加<br> * 写出的起始行为当前行号可使用{@link #getCurrentRow()}方法调用根据写出的的行数当前行号自动增加<br>
@ -845,6 +871,38 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
return this; return this;
} }
/**
* 写出复杂标题的第二行标题数据<br>
* 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
* 写出的起始行为当前行号可使用{@link #getCurrentRow()}方法调用根据写出的的行数当前行号自动+1<br>
* 样式为默认标题样式可使用{@link #getHeadCellStyle()}方法调用后自定义默认样式
*
* @param rowData 一行的数据
* @return this
*/
public ExcelWriter writeSecHeadRow(Iterable<?> rowData){
final Row row = RowUtil.getOrCreateRow(this.sheet,this.currentRow.getAndIncrement());
Iterator<?> iterator = rowData.iterator();
//如果获取的row存在单元格则执行复杂表头逻辑否则直接调用writeHeadRow(Iterable<?> rowData)
if (row.getLastCellNum() != 0) {
for (int i = 0; i < this.workbook.getSpreadsheetVersion().getMaxColumns(); i++) {
Cell cell = row.getCell(i);
if (cell != null) {
continue;
}
if (iterator.hasNext()) {
cell = row.createCell(i);
CellUtil.setCellValue(cell, iterator.next(), this.styleSet, true);
} else {
break;
}
}
} else {
writeHeadRow(rowData);
}
return this;
}
/** /**
* 写出一行根据rowBean数据类型不同写出情况如下 * 写出一行根据rowBean数据类型不同写出情况如下
* *

View File

@ -361,6 +361,10 @@ public class CellUtil {
RegionUtil.setBorderRight(cellStyle.getBorderRight(), cellRangeAddress, sheet); RegionUtil.setBorderRight(cellStyle.getBorderRight(), cellRangeAddress, sheet);
RegionUtil.setBorderBottom(cellStyle.getBorderBottom(), cellRangeAddress, sheet); RegionUtil.setBorderBottom(cellStyle.getBorderBottom(), cellRangeAddress, sheet);
RegionUtil.setBorderLeft(cellStyle.getBorderLeft(), cellRangeAddress, sheet); RegionUtil.setBorderLeft(cellStyle.getBorderLeft(), cellRangeAddress, sheet);
RegionUtil.setTopBorderColor(cellStyle.getTopBorderColor(),cellRangeAddress,sheet);
RegionUtil.setRightBorderColor(cellStyle.getRightBorderColor(),cellRangeAddress,sheet);
RegionUtil.setLeftBorderColor(cellStyle.getLeftBorderColor(),cellRangeAddress,sheet);
RegionUtil.setBottomBorderColor(cellStyle.getBottomBorderColor(),cellRangeAddress,sheet);
} }
return sheet.addMergedRegion(cellRangeAddress); return sheet.addMergedRegion(cellRangeAddress);
} }

View File

@ -13,6 +13,9 @@ import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.util.CellRangeAddressList; import org.apache.poi.ss.util.CellRangeAddressList;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
@ -520,4 +523,48 @@ public class ExcelWriteTest {
columnStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("0.00")); columnStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("0.00"));
writer.close(); writer.close();
} }
@Test
@Ignore
public void writeSecHeadRowTest() {
List<?> row1 = CollUtil.newArrayList(1,"aa", "bb", "cc", "dd", "ee");
List<?> row2 = CollUtil.newArrayList(2,"aa1", "bb1", "cc1", "dd1", "ee1");
List<?> row3 = CollUtil.newArrayList(3,"aa2", "bb2", "cc2", "dd2", "ee2");
List<?> row4 = CollUtil.newArrayList(4,"aa3", "bb3", "cc3", "dd3", "ee3");
List<?> row5 = CollUtil.newArrayList(5,"aa4", "bb4", "cc4", "dd4", "ee4");
List<List<?>> rows = CollUtil.newArrayList(row1, row2, row3, row4, row5);
// 通过工具类创建writer
ExcelWriter writer = ExcelUtil.getWriter("d:/test/writeSecHeadRowTest.xlsx");
CellStyle cellStyle = writer.getWorkbook().createCellStyle();
cellStyle.setWrapText(false);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//设置标题内容字体
Font font = writer.createFont();
font.setBold(true);
font.setFontHeightInPoints((short) 15);
font.setFontName("Arial");
//设置边框样式
StyleUtil.setBorder(cellStyle,BorderStyle.THICK,IndexedColors.RED);
cellStyle.setFont(font);
// 合并单元格后的标题行使用设置好的样式
writer.merge(0,1,0,row1.size() - 1, "标题XXXXXXXX",cellStyle);
System.out.println(writer.getCurrentRow());
//设置复杂表头
writer.merge(2,3,0,0,"序号",true);
writer.merge(2,2,1,2,"AABB",true);
writer.merge(2,3,3,3,"CCCC",true);
writer.merge(2,2,4,5,"DDEE",true);
writer.setCurrentRow(3);
List<String> sechead = CollUtil.newArrayList("AA","BB","DD","EE");
writer.writeSecHeadRow(sechead);
// 一次性写出内容使用默认样式
writer.write(rows);
// 关闭writer释放内存
writer.close();
}
} }