javaweb的list集合转Excel导出
汇总今日所学,关于java的Excel导出过程
环境是spring boot的框架。
第一步:
首先在pom.xml里面增加依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.5-FINAL</version>
</dependency>
第二步:
创建一个utils包,并在里面增加一个ExcelUtil.class
class内容:
public class ExcelUtil {
public static HSSFWorkbook getHSSFWorkbook(String sheetName, String []title, String [][]values, HSSFWorkbook wb){
// 第一步,创建一个webbook,对应一个Excel文件
if(wb == null){
wb = new HSSFWorkbook();
}
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCell cell = null;
//创建标题
for(int i=0;i<title.length;i++){
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
//创建内容
for(int i=0;i<values.length;i++){
row = sheet.createRow(i + 1);
for(int j=0;j<values[i].length;j++){
row.createCell(j).setCellValue(values[i][j]);
}
}
return wb;
}
}
以上的代码复制就好不要问为什么,有时候能用就是王道。主要是知道它的参数有哪些。
第三步:
controller包里写一个control层的控制器
@RequestMapping("/test")
public void findUsers(HttpServletResponse resp,HttpServletRequest rest) throws Exception{
String fileName = "反馈明细"+System.currentTimeMillis()+".xls"; //文件名
String sheetName = "反馈明细";//sheet名
String []title = new String[]{"1","2","3","4","5","6","7","8","9","10"};//标题
List<baseModel> list = gm.getM("");//内容list
String [][]values = new String[list.size()][];
for(int i=0;i<list.size();i++){
values[i] = new String[title.length];
//将对象内容转换成string
baseModel obj = list.get(i);
values[i][0] = obj.getLoc_wms()+"";
values[i][1] = obj.getSku();
values[i][2] = obj.getQty();
values[i][3] = obj.getLotatt03();
values[i][4] = obj.getLotatt04();
values[i][5] = obj.getLotatt05();
values[i][6] = obj.getLotatt06();
values[i][7] = obj.getLotatt07();
values[i][8] = obj.getLotatt08();
values[i][9] = obj.getLotatt09();
}
HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, values, null);
//将文件存到指定位置
try {
this.setResponseHeader(resp, fileName);
OutputStream os = resp.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
再加一个返回response的方法:
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(),"ISO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
第四步:
model层写一个POJO,其实应该是第三步
public class baseModel {
private String loc_wms;
private String sku;
private String qty;
private String lotatt03;
private String lotatt04;
private String lotatt05;
private String lotatt06;
private String lotatt07;
private String lotatt08;
private String lotatt09;
public String getLoc_wms() {
return loc_wms;
}
public void setLoc_wms(String loc_wms) {
this.loc_wms = loc_wms;
}
public String getSku() {
return sku;
}
public void setSku(String sku) {
this.sku = sku;
}
public String getQty() {
return qty;
}
public void setQty(String qty) {
this.qty = qty;
}
public String getLotatt03() {
return lotatt03;
}
public void setLotatt03(String lotatt03) {
this.lotatt03 = lotatt03;
}
public String getLotatt04() {
return lotatt04;
}
public void setLotatt04(String lotatt04) {
this.lotatt04 = lotatt04;
}
public String getLotatt05() {
return lotatt05;
}
public void setLotatt05(String lotatt05) {
this.lotatt05 = lotatt05;
}
public String getLotatt06() {
return lotatt06;
}
public void setLotatt06(String lotatt06) {
this.lotatt06 = lotatt06;
}
public String getLotatt07() {
return lotatt07;
}
public void setLotatt07(String lotatt07) {
this.lotatt07 = lotatt07;
}
public String getLotatt08() {
return lotatt08;
}
public void setLotatt08(String lotatt08) {
this.lotatt08 = lotatt08;
}
public String getLotatt09() {
return lotatt09;
}
public void setLotatt09(String lotatt09) {
this.lotatt09 = lotatt09;
}
}
第五步:
数据库查数据放到list中,怎么放,方法多样,能得到结果就行。
总得来讲并不复杂,主要是弄懂第一步的参数是什么意思就行。里面需要自己写的也就是只有pojo,数据库查询,最后就是将对应的数据放入excel中。
本文主要的代码也是窃取过来,还请原文谅解。