java数据excel_java统计分析数据Excel导出

大家在做项目开发的时候,经常需要统计数据导出excel,一般统计数据的时候是用EChart来做统计显示用,导出表格统计数据一般用poi来导出,前台展示咱们就先不讲了,对于后台的excel导出,咱们今天来看一下,希望能够帮助大家,下面废话就不多说了,开始搞代码。

1.spring boot中Controller代码

/**

* 导出数据

*

* @param request

* @param response

* @throws IOException

*/

@PostMapping("/ExportUserInfo")

public void ExportUserInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {

//表头数据

String[] header = {"姓名", "编号", "年龄", "地址"};

//声明一个工作簿

HSSFWorkbook workbook = new HSSFWorkbook();

//生成一个表格,设置表格名称为"学生表"

HSSFSheet sheet = workbook.createSheet("用户信息");

//设置表格列宽度为10个字节

sheet.setDefaultColumnWidth(10);

//创建标题的显示样式

HSSFCellStyle headerStyle = workbook.createCellStyle();

headerStyle.setFillForegroundColor(IndexedColors.YELLOW.index);

headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

//创建第一行表头

HSSFRow headrow = sheet.createRow(0);

//遍历添加表头(下面模拟遍历学生,也是同样的操作过程)

for (int i = 0; i < header.length; i++) {

//创建一个单元格

HSSFCell cell = headrow.createCell(i);

//创建一个内容对象

HSSFRichTextString text = new HSSFRichTextString(header[i]);

//将内容对象的文字内容写入到单元格中

cell.setCellValue(text);

cell.setCellStyle(headerStyle);

}

//获取导出的数据

SysUserPageInvo invo = new SysUserPageInvo();

List list = this.sysUserService.getUserList(invo);

// 设置样式

HSSFCellStyle bodyStyle = workbook.createCellStyle();

bodyStyle.setBorderTop(BorderStyle.THIN);

bodyStyle.setBorderBottom(BorderStyle.THIN);

bodyStyle.setBorderLeft(BorderStyle.THIN);

bodyStyle.setBorderRight(BorderStyle.THIN);

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

//创建一行

HSSFRow row1 = sheet.createRow(i + 1);

//姓名

Cell cell0 = row1.createCell(0);

cell0.setCellStyle(bodyStyle);

cell0.setCellValue(new HSSFRichTextString(list.get(i).getUserName()));

//编号

Cell cell3 = row1.createCell(1);

cell3.setCellStyle(bodyStyle);

cell3.setCellValue(new HSSFRichTextString(list.get(i).getTeamType().toString()));

//年龄

Cell cell4 = row1.createCell(2);

cell4.setCellStyle(bodyStyle);

cell4.setCellValue(new HSSFRichTextString(list.get(i).getAccountNumbers().toString()));

//地址

Cell cell6 = row1.createCell(3);

cell6.setCellStyle(bodyStyle);

cell6.setCellValue(new HSSFRichTextString(list.get(i).getWelfarelables()));

}

//准备将Excel的输出流通过response输出到页面下载

//八进制输出流

response.setContentType("application/octet-stream;charset=UTF-8");

response.setCharacterEncoding("UTF-8");//设置返回数据编码

//这后面可以设置导出Excel的名称,此例中名为student.xls

response.setHeader("Content-disposition", "attachment;filename=用户信息.xls");

//刷新缓冲

response.flushBuffer();

//workbook将Excel写入到response的输出流中,供页面下载

workbook.write(response.getOutputStream());

}

2. element ui前台调用

ExportUserInfo() {

this.$axios({

method: 'POST',

url: this.url.ExportUserInfo,

responseType: 'blob'

}).then(response => {

if (!response) {

return

}

const blob = new Blob([response.data])

if (window.navigator && window.navigator.msSaveOrOpenBlob) {

navigator.msSaveBlob(blob, '用户信息.xls')

} else {

let u = window.URL.createObjectURL(response.data)

let aLink = document.createElement('a')

aLink.style.display = 'none'

aLink.href = u

aLink.setAttribute('download', '用户信息.xls')

document.body.appendChild(aLink)

aLink.click()

document.body.removeChild(aLink)

window.URL.revokeObjectURL(u)

}

}).catch(error => {

throw error

})

},

java使用poi导出excel只要把数据组织好,按照固定的格式输出就可以,需要注意的是导出的格式如果是不是中规中矩的格式(第一行标题,第二行以下是数据)的话,需要用模版或者根据单元格位置固定导出,大家可以把这个方法抽成一个共同的方法方便以后开发项目的时候继续使用。后续我会分享更多的技术相关的内容,请大家多多关注。

cdbbf245b28a30ee300d1bd6a86b78d7.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值