提交 235d3204 编写于 作者: L Looly 提交者: Gitee

!318 hutool-poi修改

Merge pull request !318 from 有望得道/v5-dev
......@@ -38,6 +38,7 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
......@@ -731,6 +732,31 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
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>
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动增加<br>
......@@ -845,6 +871,38 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
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数据类型不同,写出情况如下:
*
......
......@@ -361,6 +361,10 @@ public class CellUtil {
RegionUtil.setBorderRight(cellStyle.getBorderRight(), cellRangeAddress, sheet);
RegionUtil.setBorderBottom(cellStyle.getBorderBottom(), 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);
}
......
......@@ -13,6 +13,9 @@ import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
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.junit.Ignore;
import org.junit.Test;
......@@ -520,4 +523,48 @@ public class ExcelWriteTest {
columnStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("0.00"));
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();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册