!1003 添加系列方法writeCol,以支持按列输出。

Merge pull request !1003 from hower/v5-dev
This commit is contained in:
Looly 2023-05-29 04:01:39 +00:00 committed by Gitee
commit 8c91e25047
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 112 additions and 0 deletions

View File

@ -1096,6 +1096,87 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
return this;
}
/**
* 从第1列开始按列写入数据(index 从0开始)<br>
* 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
* 写出的起始行为当前行号可使用{@link #getCurrentRow()}方法调用根据写出的的行数当前行号自动+1<br>
* 样式为默认样式可使用{@link #getCellStyle()}方法调用后自定义默认样式
*
* @param colMap 一列的数据
* @param isWriteKeyAsHead 是否将Map的Key作为表头输出如果为True第一行为表头紧接着为values
* @return this
*/
public ExcelWriter writeCol(Map<?,? extends Iterable<?>> colMap, boolean isWriteKeyAsHead){
return writeCol(colMap, 0, isWriteKeyAsHead);
}
/**
* 从指定列开始按列写入数据(index 从0开始)<br>
* 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
* 写出的起始行为当前行号可使用{@link #getCurrentRow()}方法调用根据写出的的行数当前行号自动+1<br>
* 样式为默认样式可使用{@link #getCellStyle()}方法调用后自定义默认样式
*
* @param colMap 一列的数据
* @param startColIndex 起始的列号从0开始
* @param isWriteKeyAsHead 是否将Map的Key作为表头输出如果为True第一行为表头紧接着为values
* @return this
*/
public ExcelWriter writeCol(Map<?,? extends Iterable<?>> colMap, int startColIndex, boolean isWriteKeyAsHead){
for (Object k : colMap.keySet()) {
Iterable<?> v = colMap.get(k);
if(v != null){
writeCol(isWriteKeyAsHead?k:null,startColIndex, v, startColIndex != colMap.size() - 1);
startColIndex ++;
}
}
return this;
}
/**
* 为第一列写入数据<br>
* 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
* 写出的起始行为当前行号可使用{@link #getCurrentRow()}方法调用根据写出的的行数当前行号自动+1<br>
* 样式为默认样式可使用{@link #getCellStyle()}方法调用后自定义默认样式
*
* @param headerVal 表头名称,如果为null则不写入
* @param colData 需要写入的列数据
* @param isResetRowIndex 如果为true写入完毕后Row index 将会重置为写入之前的未知如果为false写入完毕后Row index将会在写完的数据下方
* @return this
*/
public ExcelWriter writeCol(Object headerVal, Iterable<?> colData, boolean isResetRowIndex){
return writeCol(headerVal,0,colData,isResetRowIndex);
}
/**
* 为第指定列写入数据<br>
* 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
* 写出的起始行为当前行号可使用{@link #getCurrentRow()}方法调用根据写出的的行数当前行号自动+1<br>
* 样式为默认样式可使用{@link #getCellStyle()}方法调用后自定义默认样式
*
* @param headerVal 表头名称,如果为null则不写入
* @param colIndex 列index
* @param colData 需要写入的列数据
* @param isResetRowIndex 如果为true写入完毕后Row index 将会重置为写入之前的未知如果为false写入完毕后Row index将会在写完的数据下方
* @return this
*/
public ExcelWriter writeCol(Object headerVal, int colIndex, Iterable<?> colData, boolean isResetRowIndex){
Assert.isFalse(this.isClosed, "ExcelWriter has been closed!");
int currentRowIndex = currentRow.get();
if(null != headerVal){
writeCellValue(colIndex, currentRowIndex, headerVal,true);
currentRowIndex++;
}
for (Object colDatum : colData) {
writeCellValue(colIndex, currentRowIndex, colDatum);
currentRowIndex++;
}
if(!isResetRowIndex){
currentRow.set(currentRowIndex);
}
return this;
}
/**
* 给指定单元格赋值使用默认单元格样式默认不是Header
*

View File

@ -885,4 +885,35 @@ public class ExcelWriteTest {
final String disposition = writer.getDisposition("测试A12.xlsx", CharsetUtil.CHARSET_UTF_8);
Assert.assertEquals("attachment; filename=\"%E6%B5%8B%E8%AF%95A12.xlsx\"", disposition);
}
@Test
@Ignore
public void writeColTest(){
ExcelWriter writer = ExcelUtil.getWriter("G:\\test.xlsx");
List<Integer> list= new ArrayList<>();
for (int i = 0; i < 20; i++) {
list.add(i);
}
writer.writeCol("header1",0,list,true);
writer.writeCol("header2",1,list,false);
writer.writeCol("header3",0,list,true);
writer.writeCol("header4",1,list,false);
Map<String,List<Integer>> map1 = new LinkedHashMap<>();
map1.put("map1_header1",list);
map1.put("map1_header2",list);
map1.put("map1_header3",list);
map1.put("map1_header4",list);
Map<String,List<Integer>> map2 = new LinkedHashMap<>();
map2.put("map2_header1",list);
map2.put("map2_header2",list);
map2.put("map2_header3",list);
map2.put("map2_header4",list);
writer.writeCol(map1,true);
writer.writeCol(map2,false);
writer.close();
}
}