動態匯出excel

2021-10-05 06:16:27 字數 3739 閱讀 7792

現在有個業務場景,需要動態匯出單據費用。由於不同的業務單位配置的費用名稱不同,所有表頭需要動態拼接。

表頭還會一級表頭合併,如下圖所示:

表頭動態載入,同時對應的行資料也要根據表頭載入。

使用阿里的easyexcel。

headtitle9.add("運輸/配送成本");

headtitle9.add("承運商");

headlist.add(headtitle9);

feeitemvolist.stream().foreach(feeitemvo -> );

headtitle10.add("毛利");

headlist.add(headtitle10);

headtitle11.add("毛利率");

headlist.add(headtitle11);

table.sethead(headlist);

/** 所有資料行集合 **/

list> list = lists.newarraylist();

baseresultvo resultvo = list(dto, null);

for (int i = 0; i < maplist.size(); i++)

list.add(row);

}} /**彙總合計行*/

listrow = lists.newarraylist();

dto.settitletype(billfeeenums.titletypeenum.detail.getid());

baseresultvo totalfeeresultvo = listtotalfee(dto);

maptotalfeemap = (map)totalfeeresultvo.getdata();

row.add("合計");

row.add("");

/**單據營收: 提貨費:80.00,幹線費:50.00,配送費:100.00**/

for (feeitemvo feeitemvo : feeitemvolist)

//todo 該行的其他資料

list.add(row);

string filename = excelutils.generatorfilename("fee_detail");

excelwriter excelwriter =new excelwriter(getoutputstream(filename, response), exceltypeenum.xlsx);

excelwriter.write1(list,sheet,table);

/**表頭合併、毛利、毛利率合併**/

excelwriter.merge(0,1,0,0);

excelwriter.merge(0,1,1,1);

int length = 7 + 3 * feeitemvolist.size() + 2 + 1;

excelwriter.merge(0,1,length,length);

/**毛利報表 彙總行合併**/

int lastrow = 1 + list.size();

excelwriter.merge(lastrow,lastrow,0,7);

/** 釋放資源**/

excelwriter.finish();

system.out.println("ok");

return null;

}生成excel檔名:

public static string generatorfilename(string title) throws unsupportedencodingexception
httpservletresponse設定:

}controller層:

注意:1.controller層,get請求。

2.請求引數,如果不是必填,需要加入@requestparam(name = "no", required = false) string no,required = false限制。

3.狀態列表,不能 @requestparam(name = "statuslist", required = false) liststatuslist,前端轉義錯誤。

要使用:@requestparam(name = "statuslist", required = false) string statuslist;可以與前端約定值使用"-"分隔。

4.用postman測試匯出excel,選擇「send and download」。

5.合併單元格,需要數清楚第幾列,然後使用

merge(int firstrow, int lastrow, int firstcol, int lastcol) 。
excelwriter.merge(0,1,col,col),把第col列的,第一行和第二行合併。

6.二級表頭:

list> headlist = lists.newarraylist();
listheadtitle8 = lists.newarraylist();
headtitle8.add("提貨成本");

headtitle8.add("承運商");

POI動態匯出Excel(動態匯出前台選中的字段)

業務場景 使用者勾選想要匯出的字段 於是就自己寫了個通用的動態匯出封裝類,前台選中什麼欄位就匯出什麼字段 這是我資料庫表的字段 假如使用者現在只想匯出name和phone這兩個字段 直接上匯出成功效果圖 excel匯入匯出 org.apache.poi groupid poi scratchpad ...

C NPIO匯出 動態匯出為Excel

using npoi.hssf.usermodel using npoi.ss.usermodel using system using system.collections.generic using system.data using system.io using system.linq us...

C NPIO匯出 動態匯出為Excel公升級版

之前的動態匯出,當資料大於xls的最大行數時回出現錯誤 我在之前的基礎上進行了改進 using npoi.hssf.usermodel using npoi.ss.usermodel using npoi.ss.util using system using system.collections.g...