mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
!1003 添加系列方法writeCol,以支持按列输出。
Merge pull request !1003 from hower/v5-dev
This commit is contained in:
commit
8c91e25047
@ -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
|
||||
*
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user