poi快速导入导出excel表格

Posted catsblog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poi快速导入导出excel表格相关的知识,希望对你有一定的参考价值。

@RequestMapping("reportToExcel")
@ResponseBody
public ResultJsonInfo reportExcel(WxSchoolWisdomPayInfoQueryParam param, HttpServletRequest request, HttpServletResponse response) throws IOException {
//String accountName= request.getParameter("accountName");
String filePath="C:\Users\Administrator\Desktop\test.xlsx";
//导出模板内容
String path="/excelTemplates/reportXls/paymentDetail.xls";
InputStream inputStream= getClass().getResourceAsStream(path);
//2003版excel表格要用HSSFWorkbook工作簿创建
HSSFWorkbook wb = new HSSFWorkbook(inputStream);
//获取第一个Sheet
Sheet sheet = wb.getSheetAt(0);
int num= sheet.getRow(0).getLastCellNum();
//排序
OrderBean orderBean = new OrderBean();
orderBean.setOrder(OrderBean.DESC);
orderBean.setOrderBy("payEndDate");
param.setOrderBean(orderBean);

XSSFWorkbook workbook1 = new XSSFWorkbook(new FileInputStream(new File(filePath)));
SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(workbook1, 100);
Sheet first = sxssfWorkbook.getSheetAt(0);

//总记录
List<WxSchoolWisdomPayInfoVO> datas = new ArrayList<WxSchoolWisdomPayInfoVO>();
String ids = param.getWxSchoolWisdomPayInfo().getIds();
if (ids == null) {
datas = wxSchoolWisdomPayInfoService.findPage(param).getDataList();
} else {
String id[] = ids.split(",");
for (int i = 0; i < id.length; i++) {
datas.add(wxSchoolWisdomPayInfoService.findById(id[i]));
}
}
if (datas.size() > 10000) {
long t1=System.currentTimeMillis();
List<WxSchoolWisdomPayInfoDetailVO> dtlDatas = new ArrayList<WxSchoolWisdomPayInfoDetailVO>();
int i = 1;
String batchid = "";
for(int k=0;k<datas.size()%10;k++){
for(int j = 1;j<100;j++){
batchid+=datas.get(j).getdId()+",";
}
}
batchid = batchid.substring(0,batchid.length()-1);
List<String> list = new ArrayList<String>();
String[] str = batchid.split(",");
for(int j = 0;j<str.length;j++){
list.add(str[j]);
}
WxSchoolWisdomPayInfoDetailQueryParam q = new WxSchoolWisdomPayInfoDetailQueryParam();
WxSchoolWisdomPayInfoDetailVO v = new WxSchoolWisdomPayInfoDetailVO();
v.setList(list);
List<WxSchoolWisdomPayInfoDetailVO> payInfoList = wxSchoolWisdomPayInfoDetailService.findPage(q).getDataList();
for (WxSchoolWisdomPayInfoDetailVO dtl : payInfoList) {
dtl.setId(i);
if (dtl.getPayEndDate() != null) {
String payDate = DateTools.getFormattingDateTime(dtl.getPayEndDate());
dtl.setPayDate(payDate);
}
String state = dtl.getPayState();
if ("1".equals(state)) {
dtl.setPayState("待支付");
} else if ("0".equals(state)) {
dtl.setPayState("支付成功");
}
DecimalFormat decimalFormat = new DecimalFormat("###0.00");
String amt = decimalFormat.format(dtl.getPayAmt());
dtl.setAmt(amt);

dtlDatas.add(dtl);
i++;
}
try {
for (int f = 0; f < dtlDatas.size(); f++) {
Row row = first.createRow(f);
for (int j =0; j <num; j++) {
if(f == 0) {
Cell cell = row.createCell(j);
cell.setCellValue(sheet.getRow(0).getCell(j).toString());//给单元格赋值
//字体设置
Font font = workbook1.createFont();
font.setFontName("微软雅黑");//字体
font.setFontHeightInPoints((short)11);//设置字体
CellStyle stlye = workbook1.createCellStyle();
stlye.setFont(font);
row.getCell(j).setCellStyle(stlye);

} else {
if (j == 0) {
CellUtil.createCell(row, j, String.valueOf(i));
} else
CellUtil.createCell(row, 0, String.valueOf(f));
CellUtil.createCell(row, 1, String.valueOf(" "));
CellUtil.createCell(row, 2, String.valueOf(" "));
CellUtil.createCell(row, 3, String.valueOf(dtlDatas.get(f).getStudentNo()));
CellUtil.createCell(row, 4, String.valueOf(dtlDatas.get(f).getStudentName()));
CellUtil.createCell(row, 5, String.valueOf(dtlDatas.get(f).getProjectName()));
CellUtil.createCell(row, 6, String.valueOf(dtlDatas.get(f).getAmt()));
CellUtil.createCell(row, 7, String.valueOf(dtlDatas.get(f).getPayState()));
CellUtil.createCell(row, 8, String.valueOf(dtlDatas.get(f).getTransactionId()));
CellUtil.createCell(row, 9, String.valueOf(dtlDatas.get(f).getPayDate()));
CellUtil.createCell(row, 10, String.valueOf(" "));

}
}
}
OutputStream outputStream = null;
response.setHeader("Content-Disposition", "attachment;filename=" + DateTools.getFormattingToday(6)+".xlsx");
response.setContentType("application/octet-stream");
outputStream = response.getOutputStream();
sxssfWorkbook.write(outputStream);
outputStream.flush();
System.out.println("结束,耗时:" + (System.currentTimeMillis() - t1) + "(ms)");
} catch (Exception e) {
response.getWriter().write(JSONUtils.objToJson(new ResultJsonInfo(false, e.getMessage())));
return new ResultJsonInfo(false, e.getMessage());
}
}
return new ResultJsonInfo(true, "导出成功");
}





























































































































以上是关于poi快速导入导出excel表格的主要内容,如果未能解决你的问题,请参考以下文章

excel表格数据导入导出

Easypoi excel导入时日期校验

怎么使用java Poi解决导入excel表格大数据量时的内存溢出问

急~~~在线等 excel导入导出问题(导出有乱码)

POI导出excel

POI采坑系列:POI 导入导出Excel时,需要注意的时间格式####