需要导包:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
第一种方法:
public void exportJbxx600kgsbzdbExcel(HttpServletResponse response,JbxxDto jbxxDto, String tbName) {
//从dto中取出前端传的需要导出的集合
List<InfoJbxx600kgsbzdb> list = jbxxDto.getJbxx600kgsbzdbs();
if (list ==null||list.size()==0 ) {
//如果没传,则根据dto中的条件,去数据库中查询出所有符合条件的数据,导出
list = infoJBXX600KgsbzdbService.getAllParam(jbxxDto);
}
try {
//excel表头数据
String[] header = {"jbxx_ZDBMC", "jbxx_ZDBBH", "jbxx_YZDW", "jbxx_MBLX", "jbxx_ZC", "jbxx_JBQK", "jbxx_ZDBZJ",
"jbxx_ZDBCD", "jbxx_FXKKX", "jbxx_FSKKX", "jbxx_WXX", "jbxx_BZX", "jbxx_AQX", "jbxx_XNBCQ", "jbxx_FAJD",
"jbxx_CYJD", "jbxx_SYJD", "jbxx_ZYZB", "jbxx_TP", "jbxx_CSX"};
//声明一个工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//生成一个表格,设置表格名称为"Jbxx600kgsbzdb"
HSSFSheet sheet = workbook.createSheet("Jbxx600kgsbzdb");
//设置表格列宽度为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);
}
for (int i = 0; i < list.size(); i++) {
InfoJbxx600kgsbzdb infoJbxx600kgsbzdb = list.get(i);
//创建一行
HSSFRow row1 = sheet.createRow(i + 1);
//根据上面的表头,进行一一对应赋值
//第一列创建并赋值
row1.createCell(0).setCellValue(new HSSFRichTextString(infoJbxx600kgsbzdb.getJbxx_ZDBMC()));
//第二列创建并赋值
row1.createCell(1).setCellValue(new HSSFRichTextString(infoJbxx600kgsbzdb.getJbxx_ZDBBH()));
//第三列创建并赋值
row1.createCell(2).setCellValue(new HSSFRichTextString(infoJbxx600kgsbzdb.getJbxx_YZDW()));
//第4列创建并赋值
row1.createCell(3).setCellValue(new HSSFRichTextString(infoJbxx600kgsbzdb.getJbxx_MBLX()));
//第5列创建并赋值
row1.createCell(4).setCellValue(new HSSFRichTextString(infoJbxx600kgsbzdb.getJbxx_ZC()));
//第6列创建并赋值
row1.createCell(5).setCellValue(new HSSFRichTextString(infoJbxx600kgsbzdb.getJbxx_JBQK()));
//第7列创建并赋值
row1.createCell(6).setCellValue(new HSSFRichTextString(infoJbxx600kgsbzdb.getJbxx_ZDBZJ()));
//第8列创建并赋值
row1.createCell(7).setCellValue(new HSSFRichTextString(infoJbxx600kgsbzdb.getJbxx_ZDBCD()));
//第9列创建并赋值
row1.createCell(8).setCellValue(new HSSFRichTextString(infoJbxx600kgsbzdb.getJbxx_FXKKX()));
//第10列创建并赋值
row1.createCell(9).setCellValue(new HSSFRichTextString(infoJbxx600kgsbzdb.getJbxx_FSKKX()));
//第11列创建并赋值
row1.createCell(10).setCellValue(new HSSFRichTextString(infoJbxx600kgsbzdb.getJbxx_WXX()));
//第12列创建并赋值
row1.createCell(11).setCellValue(new HSSFRichTextString(infoJbxx600kgsbzdb.getJbxx_BZX()));
//第13列创建并赋值
row1.createCell(12).setCellValue(new HSSFRichTextString(infoJbxx600kgsbzdb.getJbxx_AQX()));
//第14列创建并赋值
row1.createCell(13).setCellValue(new HSSFRichTextString(infoJbxx600kgsbzdb.getJbxx_XNBCQ()));
//第15列创建并赋值
row1.createCell(14).setCellValue(new HSSFRichTextString(infoJbxx600kgsbzdb.getJbxx_FAJD()));
row1.createCell(15).setCellValue(new HSSFRichTextString(infoJbxx600kgsbzdb.getJbxx_CYJD()));
row1.createCell(16).setCellValue(new HSSFRichTextString(infoJbxx600kgsbzdb.getJbxx_SYJD()));
row1.createCell(17).setCellValue(new HSSFRichTextString(infoJbxx600kgsbzdb.getJbxx_ZYZB()));
row1.createCell(18).setCellValue(new HSSFRichTextString(infoJbxx600kgsbzdb.getJbxx_TP()));
row1.createCell(19).setCellValue(new HSSFRichTextString(infoJbxx600kgsbzdb.getJbxx_CSX()));
}
//准备将Excel的输出流通过response输出到页面下载
//八进制输出流
response.setContentType("application/octet-stream");
//这后面可以设置导出Excel的名称
response.setHeader("Content-disposition", "attachment;filename=" + tbName);
//刷新缓冲
response.flushBuffer();
//workbook将Excel写入到response的输出流中,供页面下载
workbook.write(response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
第二种方法: 不用固定表头列,导出的列是随机的,如:第一次导出的id列在第一列,第二次可能在第二列.
public void exportParamZtclzdbExcel(HttpServletRequest request, HttpServletResponse response, ParamDto paramDto, String tbName) {
List<JSONArray> objectsList = new ArrayList<>();
//获取需要导出的数据信息
List<InfoParamZtclzdb> allParam = infoParamZtclzdbService.getAllParam(paramDto);
//中转一下
List<InfoParamZtclzdb> allParam1 = new ArrayList<>();
for (InfoParamZtclzdb infoParamZtclzdb : allParam) {
allParam1.add(infoParamZtclzdb);
//将集合转成json
String s = JSONArray.toJSONString(allParam1);
//再转成jsonarray
JSONArray jsonArray = JSONArray.parseArray(s);
//将jsonarray add到objectsList集合中,传给excelExportUtil
objectsList.add(jsonArray);
allParam1.clear();
}
ExcelExportUtil.exportExcel(request, response, tbName, objectsList, tbName);
}
package com.dfdt.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;
/**
* excel导出
*/
public class ExcelExportUtil {
/**
* 导出excel表格
*
* @param name sheet名
* @param ja JSONArray 报名数据json集合
* @param tbName 文件名
*/
public static void exportExcel(HttpServletRequest request, HttpServletResponse response, String name, List<JSONArray> ja, String tbName) {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet(name);
XSSFRow row = sheet.createRow(0);
int index = 0;
if (ja != null && ja.size() > 0) {
JSONArray objects = ja.get(0);
for (int i = 0; i < objects.size(); i++) {
JSONObject jaa = objects.getJSONObject(i);
Iterator<String> iterator = jaa.keySet().iterator(); //得到第一项的key集合
while (iterator.hasNext()) { // 遍历key集合
String key = iterator.next();// 得到key
XSSFCell cell = row.createCell(index);
cell.setCellValue(key);//设置表头
index++;
}
}
for (int j = 0; j < ja.size(); j++) {
row = sheet.createRow(j + 1);
int index1 = 0;
for (int i = 0; i < ja.get(j).size(); i++) {
JSONObject jaa = ja.get(j).getJSONObject(i);
Iterator<String> iterator1 = jaa.keySet().iterator();
while (iterator1.hasNext()) { // 遍历key集合
String key1 = iterator1.next(); // 得到key
String value = jaa.getString(key1);
XSSFCell cell = row.createCell(index1);
cell.setCellValue(value);
index1++;
}
}
}
try {
OutputStream outputStream = response.getOutputStream();
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(tbName, "UTF8") + ".xls");
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
workbook.write(outputStream);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
OutputStream outputStream = response.getOutputStream();
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(tbName, "UTF8") + ".xls");
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
workbook.write(outputStream);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}