我的数据来源:json字符串
步骤:
1.寻找导出excel demo
参考:http://www.cnblogs.com/dragonetyu/p/7591914.html
2.解析json字符串取值,并封装entity,将entity加入jsonArrary,传入excel导出的方法中遍历
3.合并表头
4.返回前端下载
详细步骤:
springboot项目:
controller:
(包含 Java POI Excel导出文件名中文乱码)
/**
* 下载量统计 导出 excel
* @return
*/
@CrossOrigin
@RequestMapping(value = "/dowloadResumeDownloadExcel", method={RequestMethod.GET})
public void dowloadResumeDownloadExcel(HttpServletRequest request, HttpServletResponse response) {
String res =null;
try {
String startTime = request.getParameter("startTime");
String endTime = request.getParameter("endTime");
res = iCalResumesService.queryResumeDownloadInfo(startTime,endTime);
net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(res);
String data = jsonObject.getString("data");
net.sf.json.JSONObject jsonData = net.sf.json.JSONObject.fromObject(data);
String allTotal = jsonData.getString("allTotal");
net.sf.json.JSONObject jo = net.sf.json.JSONObject.fromObject(data);
String list = jo.getString("list");
//将jsonArray字符串转化为JSONArray
JSONArray jAlist = JSONArray.fromObject(list);
com.alibaba.fastjson.JSONArray jaa = new com.alibaba.fastjson.JSONArray();
String brand ="";
String brandTotal ="";
for(int k=0;k<jAlist.size();k++){
//取出数组元素
net.sf.json.JSONObject jso = jAlist.getJSONObject(k);
//取出第一个元素的信息,并且转化为JSONObject
brand = jso.getString("brand");
brandTotal = jso.getString("brandTotal");
String officeList = jso.getString("officeList");
//将jsonArray字符串转化为JSONArray
JSONArray jAofficeList = JSONArray.fromObject(officeList);
String officeName ="";
String officeTotal ="";
for(int m=0;m<jAofficeList.size();m++){
//取出数组元素
net.sf.json.JSONObject jsp = jAofficeList.getJSONObject(m);
officeName = jsp.getString("officeName");
officeTotal = jsp.getString("officeTotal");
String proTeamList = jsp.getString("proTeamList");
//将jsonArray字符串转化为JSONArray
JSONArray jAproTeamList = JSONArray.fromObject(proTeamList);
String proTeamName ="";
String count ="";
String lb ="";
String lbzl ="";
String fxs ="";
String fxszl ="";
String fxsqcwl ="";
String xf ="";
String xfzl ="";
String xfqcwl ="";
String jlpc ="";
String hyzljlpc ="";
String qd ="";
String qdhj ="";
String yxj ="";
String yxjzl ="";
String zlm ="";
String zlmzl ="";
String hy ="";
String hykj ="";
for(int n=0;n<jAproTeamList.size();n++){
//取出数组元素
net.sf.json.JSONObject jsq = jAproTeamList.getJSONObject(n);
proTeamName = jsq.getString("proTeamName");
count = jsq.getString("count");
lb = jsq.getString("lb");
net.sf.json.JSONObject jslb = net.sf.json.JSONObject.fromObject(lb);
lbzl = jslb.getString("zl");
fxs = jsq.getString("fxs");
net.sf.json.JSONObject jsfxs = net.sf.json.JSONObject.fromObject(fxs);
fxszl = jsfxs.getString("zl");
fxsqcwl = jsfxs.getString("qcwl");
xf = jsq.getString("xf");
net.sf.json.JSONObject jsxf = net.sf.json.JSONObject.fromObject(xf);
xfzl = jsxf.getString("zl");
xfqcwl = jsxf.getString("wyjj");
jlpc = jsq.getString("jlpc");
net.sf.json.JSONObject jsjlpc = net.sf.json.JSONObject.fromObject(jlpc);
hyzljlpc = jsjlpc.getString("hyzljlpc");
qd = jsq.getString("qd");
net.sf.json.JSONObject jsqd = net.sf.json.JSONObject.fromObject(qd);
qdhj = jsqd.getString("qdhj");
yxj = jsq.getString("yxj");
net.sf.json.JSONObject jsyxj = net.sf.json.JSONObject.fromObject(yxj);
yxjzl = jsyxj.getString("zl");
zlm = jsq.getString("zlm");
net.sf.json.JSONObject jszlm = net.sf.json.JSONObject.fromObject(zlm);
zlmzl = jszlm.getString("zl");
hy = jsq.getString("hy");
net.sf.json.JSONObject jshy = net.sf.json.JSONObject.fromObject(hy);
hykj = jshy.getString("hykj");
SupplierDownLoadEntity sde = new SupplierDownLoadEntity();
sde.setBrand(brand);
sde.setOfficeName(officeName);
sde.setProTeamName(proTeamName);
sde.setProTeamName(proTeamName);
sde.setProTeamName(proTeamName);
sde.setProTeamName(proTeamName);
sde.setProTeamName(proTeamName);
sde.setProTeamName(proTeamName);
sde.setLb(lbzl);
sde.setFxs(fxszl);
sde.setKb1(fxsqcwl);
sde.setXf(xfzl);
sde.setKb2(xfqcwl);
sde.setJlpc(hyzljlpc);
sde.setQd(qdhj);
sde.setYxj(yxjzl);
sde.setZlm(zlmzl);
sde.setHy(hykj);
sde.setCount(count);
sde.setOfficeTotal(officeTotal);
sde.setBrandTotal(brandTotal);
sde.setAllTotal(allTotal);
jaa.add(sde);
}
}
}
//列头1
Map<String,String> headMap = new LinkedHashMap<String,String>();
//键名与实体类相对应
headMap.put("brand","品牌");
headMap.put("officeName","办公室");
headMap.put("proTeamName","项目组");
headMap.put("lb","李");
headMap.put("fxs","樊");
headMap.put("kb1","");
headMap.put("xf","谢");
headMap.put("kb2","");
headMap.put("jlpc","爬虫");
headMap.put("qd","董");
headMap.put("yxj","杨");
headMap.put("zlm","朱");
headMap.put("hy","cc科技");
headMap.put("count","项目组合计");
headMap.put("officeTotal","办公室合计");
headMap.put("brandTotal","品牌合计");
headMap.put("allTotal","总计");
SXSSFWorkbook wo =null;
try {
wo = ExcelUtilForDownLoad.exportExcelX("办公室下载量统计",headMap,jaa,null,0); //xslx
} catch (Exception e) {
e.printStackTrace();
}
String filename="办公室下载量统计("+DateUtil.fmtDateToStr(new Date(),"yyyy-MM-dd HH:mm:ss")+")";
this.compatibleFileName(request,response,filename);
OutputStream outputStream = response.getOutputStream();
wo.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
//e.printStackTrace();
LogUtil.error(DateUtil.fmtDateToStr(new Date(),"yyyy-MM-dd HH:mm:ss")+"http接口异常",e.getMessage(),e);
}
}
// 判断浏览器类型,firefox浏览器做特殊处理,否则下载文件名乱码
public static void compatibleFileName(HttpServletRequest request, HttpServletResponse response, String excelname) throws UnsupportedEncodingException {
String agent = request.getHeader("USER-AGENT").toLowerCase();
response.setContentType("application/vnd.ms-excel");
String fileName = excelname;
String codedFileName = java.net.URLEncoder.encode(fileName, "UTF-8");
if (agent.contains("firefox")) {
response.setCharacterEncoding("utf-8");
response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.replace("+"," ").getBytes(), "ISO8859-1") + ".xlsx");
} else {
response.setHeader("content-disposition", "attachment;filename=" + codedFileName.replace("+"," ") + ".xlsx");
}
}
注意解析json代码不能放到service中,会报如下错误;
ExcelUtilForDownLoad:
package com.huayong.bi.web.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huayong.bi.dao.pfbi.entity.SupplierUploadEntity;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class ExcelUtilForDownLoad {
public static String NO_DEFINE = "no_define";//未定义的字段
public static String DEFAULT_DATE_PATTERN="yyyy年MM月dd日";//默认日期格式
public static int DEFAULT_COLOUMN_WIDTH = 17;
/**
* 导出Excel 97(.xls)格式 ,少量数据
* @param title 标题行
* @param headMap 属性-列名
* @param jsonArray 数据集
* @param datePattern 日期格式,null则用默认日期格式
* @param colWidth 列宽 默认 至少17个字节
* @param out 输出流
*/
public static HSSFWorkbook exportExcel(String title,Map<String, String> headMap,JSONArray jsonArray,String datePattern,int colWidth, OutputStream out) {
if(datePattern==null) datePattern = DEFAULT_DATE_PATTERN;
// 声明一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
workbook.createInformationProperties();
workbook.getDocumentSummaryInformation().setCompany("*****公司");
SummaryInformation si = workbook.getSummaryInformation();
si.setAuthor("JACK"); //填加xls文件作者信息
si.setApplicationName("导出程序"); //填加xls文件创建程序信息
si.setLastAuthor("最后保存者信息"); //填加xls文件最后保存者信息
si.setComments("JACK is a programmer!"); //填加xls文件作者信息
si.setTitle("POI导出Excel"); //填加xls文件标题信息
si.setSubject("POI导出Excel");//填加文件主题信息
si.setCreateDateTime(new Date());
//表头样式
HSSFCellStyle titleStyle = workbook.createCellStyle();
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont titleFont = workbook.createFont();
titleFont.setFontHeightInPoints((short) 20);
titleFont.setBoldweight((short) 700);
titleStyle.setFont(titleFont);
// 列头样式
HSSFCellStyle headerStyle = workbook.createCellStyle();
headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont headerFont = workbook.createFont();
headerFont.setFontHeightInPoints((short) 12);
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headerStyle.setFont(headerFont);
// 单元格样式
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFFont cellFont = workbook.createFont();
cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
cellStyle.setFont(cellFont);
// 生成一个(带标题)表格
HSSFSheet sheet = workbook.createSheet();
// 声明一个画图的顶级管理器
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
// 定义注释的大小和位置,详见文档
HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0,
0, 0, 0, (short) 4, 2, (short) 6, 5));
// 设置注释内容
comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));
// 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容.
comment.setAuthor("JACK");
//设置列宽
int minBytes = colWidth<DEFAULT_COLOUMN_WIDTH?DEFAULT_COLOUMN_WIDTH:colWidth;//至少字节数
int[] arrColWidth = new int[headMap.size()];
// 产生表格标题行,以及设置列宽
String[] properties = new String[headMap.size()];
String[] headers = new String[headMap.size()];
int ii = 0;
for (Iterator<String> iter = headMap.keySet().iterator(); iter
.hasNext();) {
String fieldName = iter.next();
properties[ii] = fieldName;
headers[ii] = fieldName;
int bytes = fieldName.getBytes().length;
arrColWidth[ii] = bytes < minBytes ? minBytes : bytes;
sheet.setColumnWidth(ii,arrColWidth[ii]*256);
ii++;
}
// 遍历集合数据,产生数据行
int rowIndex = 0;
for (Object obj : jsonArray) {
if(rowIndex == 65535 || rowIndex == 0){
if ( rowIndex != 0 ) sheet = workbook.createSheet();//如果数据超过了,则在第二页显示
HSSFRow titleRow = sheet.createRow(0);//表头 rowIndex=0
titleRow.createCell(0).setCellValue(title);
titleRow.getCell(0).setCellStyle(titleStyle);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headMap.size() - 1));
HSSFRow headerRow = sheet.createRow(1); //列头 rowIndex =1
for(int i=0;i<headers.length;i++)
{
headerRow.createCell(i).setCellValue(headers[i]);
headerRow.getCell(i).setCellStyle(headerStyle);
}
rowIndex = 2;//数据内容从 rowIndex=2开始
}
JSONObject jo = (JSONObject) JSONObject.toJSON(obj);
HSSFRow dataRow = sheet.createRow(rowIndex);
for (int i = 0; i < properties.length; i++)
{
HSSFCell newCell = dataRow.createCell(i);
Object o = jo.get(properties[i]);
String cellValue = "";
if(o==null) cellValue = "";
else if(o instanceof Date) cellValue = new SimpleDateFormat(datePattern).format(o);
else cellValue = o.toString();
newCell.setCellValue(cellValue);
newCell.setCellStyle(cellStyle);
}
rowIndex++;
}
// 自动调整宽度
/*for (int i = 0; i < headers.length; i++) {
sheet.autoSizeColumn(i);
}*/
try {
workbook.write(out);
// workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
return workbook;
}
/**
* 导出Excel 2007 OOXML (.xlsx)格式
* @param title 标题行
* @param headMap 属性-列头1
* @param jsonArray 数据集
* @param datePattern 日期格式,传null值则默认 年月日
* @param colWidth 列宽 默认 至少17个字节
*/
public static SXSSFWorkbook exportExcelX(String title,Map<String, String> headMap,JSONArray jsonArray,String datePattern,int colWidth) {
if(datePattern==null) datePattern = DEFAULT_DATE_PATTERN;
// 声明一个工作薄
SXSSFWorkbook workbook = new SXSSFWorkbook(1000);//缓存
workbook.setCompressTempFiles(true);
//表头样式
CellStyle titleStyle = workbook.createCellStyle();
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
Font titleFont = workbook.createFont();
titleFont.setFontHeightInPoints((short) 20);
titleFont.setBoldweight((short) 700);
titleStyle.setFont(titleFont);
// 列头样式
CellStyle headerStyle = workbook.createCellStyle();
headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
Font headerFont = workbook.createFont();
headerFont.setFontHeightInPoints((short) 12);
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headerStyle.setFont(headerFont);
// 单元格样式
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
Font cellFont = workbook.createFont();
cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
cellStyle.setFont(cellFont);
// 生成一个(带标题)表格
SXSSFSheet sheet = workbook.createSheet();
//设置列宽
int minBytes = colWidth<DEFAULT_COLOUMN_WIDTH?DEFAULT_COLOUMN_WIDTH:colWidth;//至少字节数
int[] arrColWidth = new int[headMap.size()];
int[] arrColWidth2 = new int[headMap.size()];
// 产生表格标题行,以及设置列宽
//列头1
String[] properties = new String[headMap.size()];
String[] headers = new String[headMap.size()];
int ii = 0;
for (Iterator<String> iter = headMap.keySet().iterator();
iter.hasNext();) {
String fieldName = iter.next();//map的键
properties[ii] = fieldName;
headers[ii] = headMap.get(fieldName);//map的值
int bytes = fieldName.getBytes().length;
arrColWidth[ii] = bytes < minBytes ? minBytes : bytes;
sheet.setColumnWidth(ii,arrColWidth[ii]*256);
ii++;
}
//列头2
Map<String,String> headMap2 = new LinkedHashMap<String,String>();
headMap2.put("brand","");
headMap2.put("officeName","");
headMap2.put("proTeamName","");
headMap2.put("lb","智联招聘");
headMap2.put("fxs","智联招聘");
headMap2.put("kb1","前程无忧");
headMap2.put("xf","智联招聘");
headMap2.put("kb2","无忧精英");
headMap2.put("jlpc","");
headMap2.put("qd","智联招聘");
headMap2.put("yxj","智联招聘");
headMap2.put("zlm","智联招聘");
headMap2.put("hy","");
headMap2.put("count","");
headMap2.put("officeTotal","");
headMap2.put("brandTotal","");
headMap2.put("allTotal","");
String[] properties2 = new String[headMap2.size()];
String[] headers2 = new String[headMap2.size()];
int ii2 = 0;
for (Iterator<String> iter = headMap2.keySet().iterator();
iter.hasNext();) {
String fieldName = iter.next();//map的键
properties2[ii2] = fieldName;
headers2[ii2] = headMap2.get(fieldName);//map的值
int bytes = fieldName.getBytes().length;
arrColWidth2[ii2] = bytes < minBytes ? minBytes : bytes;
sheet.setColumnWidth(ii2,arrColWidth2[ii2]*256);
ii2++;
}
// 遍历集合数据,产生数据行
int rowIndex = 0;
//总计
// sheet.addMergedRegion(new CellRangeAddress(2, jsonArray.size()+1, 5, 5));
for (Object obj : jsonArray) {
if(rowIndex == 65535 || rowIndex == 0){
if ( rowIndex != 0 ) sheet = workbook.createSheet();//如果数据超过了,则在第二页显示
SXSSFRow titleRow = sheet.createRow(0);//表头 rowIndex=0
titleRow.createCell(0).setCellValue(title);
titleRow.getCell(0).setCellStyle(titleStyle);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headMap.size() - 1));
SXSSFRow headerRow = sheet.createRow(1); //列头1 rowIndex =1
for(int i=0;i<headers.length;i++)
{
headerRow.createCell(i).setCellValue(headers[i]);
headerRow.getCell(i).setCellStyle(headerStyle);
}
sheet.addMergedRegion(new CellRangeAddress(1, 1, 4, 5));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 6, 7));
SXSSFRow headerRow2 = sheet.createRow(2); //列头2 rowIndex =2
for(int i=0;i<headers2.length;i++)
{
headerRow2.createCell(i).setCellValue(headers2[i]);
headerRow2.getCell(i).setCellStyle(headerStyle);
}
sheet.addMergedRegion(new CellRangeAddress(1, 2, 0, 0));
sheet.addMergedRegion(new CellRangeAddress(1, 2, 1, 1));
sheet.addMergedRegion(new CellRangeAddress(1, 2, 2, 2));
sheet.addMergedRegion(new CellRangeAddress(1, 2, 8, 8));
sheet.addMergedRegion(new CellRangeAddress(1, 2, 12, 12));
sheet.addMergedRegion(new CellRangeAddress(1, 2, 13, 13));
sheet.addMergedRegion(new CellRangeAddress(1, 2, 14, 14));
sheet.addMergedRegion(new CellRangeAddress(1, 2, 15, 15));
sheet.addMergedRegion(new CellRangeAddress(1, 2, 16, 16));
rowIndex = 3;//数据内容从 rowIndex=3开始
}
JSONObject jo = (JSONObject) JSONObject.toJSON(obj);
SXSSFRow dataRow = sheet.createRow(rowIndex);
for (int i = 0; i < properties.length; i++)//填充一行数据
{
SXSSFCell newCell = dataRow.createCell(i);
Object o = jo.get(properties[i]);//cell值
String cellValue = "";
if(o==null) cellValue = "";
else if(o instanceof Date) cellValue = new SimpleDateFormat(datePattern).format(o);
else if(o instanceof Float || o instanceof Double) {
cellValue= new BigDecimal(o.toString()).setScale(2,BigDecimal.ROUND_HALF_UP).toString();
}
else cellValue = o.toString();
newCell.setCellValue(cellValue);
newCell.setCellStyle(cellStyle);
}
rowIndex++;
}
// 自动调整宽度
/*for (int i = 0; i < headers.length; i++) {
sheet.autoSizeColumn(i);
}*/
// try {
// workbook.write(out);
// workbook.close();
// workbook.dispose();
// } catch (IOException e) {
// e.printStackTrace();
// }
return workbook;
}
//Web 导出excel
public static void downloadExcelFile(String title, Map<String,String> headMap, JSONArray ja, HttpServletResponse response){
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ExcelUtilForDownLoad.exportExcelX(title,headMap,ja,null,0);
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
// 设置response参数,可以打开下载页面
response.reset();
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+ new String((title + ".xlsx").getBytes(), "iso-8859-1"));
response.setContentLength(content.length);
ServletOutputStream outputStream = response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
byte[] buff = new byte[8192];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();
outputStream.flush();
outputStream.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
int count = 100;
JSONArray ja = new JSONArray();
for(int i=0;i<100;i++){
// Student s = new Student();
// s.setName("POI"+i);
// s.setAge(i);
// s.setBirthday(new Date());
// s.setHeight(i);
// s.setWeight(i);
// s.setSex(i/2==0?false:true);
//
}
SupplierUploadEntity sue = new SupplierUploadEntity();
sue.setSupplier("谢飞");
sue.setResumeSource("智联招聘");
sue.setUpCount("1000");
sue.setTotal("20");
sue.setProportion("20%");
sue.setAllTotal("1020");
ja.add(sue);
Map<String,String> headMap = new LinkedHashMap<String,String>();
headMap.put("supplier","供应商");
headMap.put("resumeSource","年龄");
headMap.put("upCount","上传量");
headMap.put("total","合计");
headMap.put("proportion","占比");
headMap.put("allTotal","总计");
String title = "测试";
/*
OutputStream outXls = new FileOutputStream("E://a.xls");
System.out.println("正在导出xls....");
Date d = new Date();
ExcelUtil.exportExcel(title,headMap,ja,null,outXls);
System.out.println("共"+count+"条数据,执行"+(new Date().getTime()-d.getTime())+"ms");
outXls.close();*/
//
OutputStream osPath = new FileOutputStream("C:/Users/Administrator/Desktop/aaa.xlsx");
System.out.println("正在导出xlsx....");
Date d2 = new Date();
ExcelUtilForDownLoad.exportExcelX(title,headMap,ja,null,0);
System.out.println("共"+count+"条数据,执行"+(new Date().getTime()-d2.getTime())+"ms");
osPath.close();
}
}
后端一开始用的post,前端用ajax,一直是导出后乱码。
后来后端改为get方式。
导出excel 不支持中文,如上解决后post返回时示例,浏览器会解析此码:
前端代码:
设置单元格格式数值类型便于计算:
/**
* 利用正则表达式判断字符串是否是数字(整数)
* @param str
* @return
*/
public static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}
参考:
office 所有后缀对应的 content-type:
https://blog.csdn.net/xiaoranzhizhu/article/details/70473734
demon:
https://github.com/xiaopotian1990/SpringBootExcel
https://blog.csdn.net/long530439142/article/details/79002792?utm_source=blogxgwz5
https://blog.csdn.net/deke512/article/details/78973744