计一次excel 导出
最终结果
? 将统计字段和查询的信息放在同一个对象之后,创建一个相对的一对多关系表
? 如下
@Data
@ExcelTarget("storeRank")
public class storeRank {
@Excel(name = "门店名字", orderNum = "1", width = 25, needMerge = true)
private String storeName;
@ExcelCollection(name = "信息", orderNum = "2")
private List<StoreRankVo> list;
}
其相对应的StoreRankVo 的结构为对应的单个查询明细
@Data
public class StoreRankVo {
@Excel(name = "承运商名字", width = 20)
private String carriersName;
@Excel(name = "票数", width = 20)
private String num;
@Excel(name = "运费", width = 20)
private String money;
}
由于表头相对复杂,所以这里使用的是easyPoi 其 needMerge 可以很好的自适应对应的字段的高度
处理的数据结构如下
[
{
"storeName": "浙江省杭州市龙湖金沙天街店-XX",
"list": [
{
"num": "21",
"money": "116.0",
"carriersName": "京东快递"
},
{
"num": "6",
"money": "252.0",
"carriersName": "京东物流"
},
{
"num": "27",
"money": "368.0",
"carriersName": "总计"
}
]
},
{
"storeName": "浙江省杭州市西湖区西溪天街店-XXX",
"list": [
{
"num": "44",
"money": "251.5",
"carriersName": "京东快递"
},
{
"num": "1",
"money": "16.2",
"carriersName": "顺丰速运(陆运产品)"
},
{
"num": "45",
"money": "267.7",
"carriersName": "总计"
}
]
}
]
部分代码如下
HashMap<String, Object> map = new HashMap<>();
List<String> list = storeBrandsDao.getSoreID("");
map.put("ids", list);
LinkedList<storeRank> objects = new LinkedList<>();
for (String s : list
) {
LinkedList<StoreRankVo> rankVoLinkedList = new LinkedList<>();
String senderStoreName = orderDao.selectList(new QueryWrapper<ConfigOrder>().lambda().eq(ConfigOrder::getSenderStoreID, s)).get(0).getSenderStoreName();
map.put("storeId", s);
List<Map<String, Object>> maps = orderDao.testDeliveryRank(map);
double countMoney = 0;
Integer countNum = 0;
for (int i = 0; i < maps.size(); i++) {
Object money = maps.get(i).get("money");
Object num = maps.get(i).get("num");
Object carriersName = maps.get(i).get("carriersName");
countMoney = Double.parseDouble(money.toString()) + countMoney;
countNum = Integer.valueOf(num.toString()) + countNum;
StoreRankVo rankVo = new StoreRankVo();
rankVo.setMoney(money.toString());
rankVo.setNum(num.toString());
rankVo.setCarriersName((String) carriersName);
rankVoLinkedList.add(rankVo);
}
StoreRankVo rankVo = new StoreRankVo();
rankVo.setCarriersName((String) "总计");
rankVo.setMoney(String.valueOf(countMoney));
rankVo.setNum(String.valueOf(countNum));
rankVoLinkedList.add(rankVo);
storeRank rank = new storeRank();
rank.setStoreName(senderStoreName);
rank.setList(rankVoLinkedList);
objects.add(rank);
}
String title = "门店信息";
String sheetName = "门店信息";
ExcelUtils.exportExcel(objects, title, sheetName, storeRank.class, "门店信息" + System.currentTimeMillis(), response);
此处附上工具类
package com.dp_admin_server.utils;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
public class ExcelUtils {
public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException {
defaultExport(list, fileName, response);
}
private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException {
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
downLoadExcel(fileName, response, workbook);
}
private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) throws IOException {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
downLoadExcel(fileName, response, workbook);
}
public static void exportExcel(List<?> list, Class<?> pojoClass, String fileName, ExportParams exportParams, HttpServletResponse response) throws IOException {
defaultExport(list, pojoClass, fileName, response, exportParams);
}
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response) throws IOException {
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName, ExcelType.XSSF));
}
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) throws IOException {
ExportParams exportParams = new ExportParams(title, sheetName, ExcelType.XSSF);
exportParams.setCreateHeadRows(isCreateHeader);
defaultExport(list, pojoClass, fileName, response, exportParams);
}
private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
workbook.write(response.getOutputStream());
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
public static <T> List<T> importExcel(MultipartFile file, Class<T> pojoClass) throws IOException {
return importExcel(file, 1, 1, pojoClass);
}
public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException {
if (StringUtils.isBlank(filePath)) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
params.setNeedSave(true);
params.setSaveUrl("/excel/");
try {
return ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
} catch (NoSuchElementException e) {
throw new IOException("模板不能为空");
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException {
if (file == null) {
return null;
}
try {
return importExcel(file.getInputStream(), titleRows, headerRows, pojoClass);
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
public static <T> List<T> importExcel(InputStream inputStream, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException {
if (inputStream == null) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
params.setSaveUrl("/excel/");
params.setNeedSave(true);
try {
return ExcelImportUtil.importExcel(inputStream, pojoClass, params);
} catch (NoSuchElementException e) {
throw new IOException("excel文件不能为空");
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
public static HSSFWorkbook jsonToExcel(JSONArray jsonArray) throws IOException {
Set<String> keys = null;
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet0");
int roleNo = 0;
int rowNo = 0;
HSSFRow row = sheet.createRow(roleNo++);
keys = jsonArray.getJSONObject(0).keySet();
for (String s : keys) {
HSSFCell cell = row.createCell(rowNo++);
cell.setCellValue(s);
}
rowNo = 0;
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
row = sheet.createRow(roleNo++);
for (String s : keys) {
HSSFCell cell = row.createCell(rowNo++);
cell.setCellValue(jsonObject.getString(s));
}
rowNo = 0;
}
return wb;
}
}
|