Java中实现简单的Excel导出
今天小编就为大家分享一篇关于Java中实现简单的Excel导出,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧简单介绍一下java中的excel文件导出功能(基于httpservletresponse实现下载)
首先,引入需要依赖的jar包:
<dependency>
<groupid>org.apache.poi</groupid>
<artifactid>poi</artifactid>
<version>3.14</version>
</dependency>
<dependency>
<groupid>org.apache.poi</groupid>
<artifactid>poi-ooxml</artifactid>
<version>3.14</version>
</dependency>
编写一个工具类:
package exceloutput;
import org.apache.commons.lang3.stringutils;
import org.apache.poi.ss.usermodel.cell;
import org.apache.poi.ss.usermodel.row;
import org.apache.poi.ss.usermodel.workbook;
import org.apache.poi.xssf.streaming.sxssfsheet;
import org.apache.poi.xssf.streaming.sxssfworkbook;
import javax.servlet.http.httpservletresponse;
import java.io.ioexception;
import java.io.unsupportedencodingexception;
import java.util.list;
import java.util.uuid;
/**
* @author haozz
* @date 2018/6/6 9:57
* @description excel导出抽象工具类
**/
public abstract class exportabstractutil {
public void write(httpservletresponse response, workbook workbook){
string filename = uuid.randomuuid().tostring()+".xls";
pwrite(response,workbook,filename);
}
public void write(httpservletresponse response,workbook workbook,string filename){
if(stringutils.isempty(filename)){
filename = uuid.randomuuid().tostring()+".xls";
}
pwrite(response,workbook,filename);
}
public void write(httpservletresponse response, list<list<string>> lists,string filename){
if(stringutils.isempty(filename)){
filename = uuid.randomuuid().tostring()+".xls";
}
sxssfworkbook workbook = new sxssfworkbook(lists.size());
sxssfsheet sheet = workbook.createsheet(filename.substring(0,filename.indexof(".xls")));
integer rowindex = 0;
row row = null;
cell cell = null;
for(list<string> rowdata: lists ){
integer columnindex = 0;
row = sheet.createrow(rowindex++);
for(string columnval:rowdata){
cell = row.createcell(columnindex++);
cell.setcellvalue(columnval);
}
}
pwrite(response,workbook,filename);
}
private void pwrite(httpservletresponse response,workbook workbook,string filename){
response.setcharacterencoding("utf-8");
response.setcontenttype("application/vnd.ms-excel;charset=utf-8");
try {
response.addheader("content-disposition", "attachment; filename="+new string(filename.getbytes("utf-8"),"iso8859-1"));
} catch (unsupportedencodingexception e) {
e.printstacktrace();
filename= uuid.randomuuid().tostring()+".xls";
response.addheader("content-disposition", "attachment; filename="+filename);
}
try {
workbook.write(response.getoutputstream());
} catch (ioexception e) {
e.printstacktrace();
}
}
}
有了这个工具类就可以实现excel导出了,代码不难,这里就不多解释了。
在springboot项目中编写一个导出excel的controller,并继承上面的exportabstractutil,给出一个接口用作测试:
package com.csdn.myboot.controller;
import com.csdn.myboot.utils.exportabstractutil;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.responsebody;
import javax.servlet.http.httpservletresponse;
import java.util.arraylist;
import java.util.arrays;
import java.util.list;
/**
* @author haozz
* @date 2018/6/6 10:14
* @description
**/
@controller
@requestmapping(value = "/index")
public class helloctrl extends exportabstractutil{
@requestmapping(value = "/testexceloutput")
@responsebody
public void testexceloutput(httpservletresponse response){
//拼接数据start
list<list<string>> lists = new arraylist<list<string>>();
string rows[] = {"year","month","day"};
list<string> rowstitle = arrays.aslist(rows);
lists.add(rowstitle);
for(int i = 0; i<=9;i++){
string [] rowss = {"1","2","3"};
list<string> rowsslist = arrays.aslist(rowss);
lists.add(rowsslist);
}
//拼接数据end
write(response,lists,"导出excel.xls");
}
}
浏览器输入链接:
http://localhost:8099/index/testexceloutput
即可自动下载测试数据组成的excel:
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对CodeAE代码之家的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/hz_940611/article/details/80590488
http://www.zzvips.com/article/175128.html
页:
[1]