最近在做一个文史类统计项目,许多地方需要用到导出excel报表。之前用的是公司的poi导出,最后听群友说csv的导出会比poi快很多。但是csv文件是以“,”分割文件的。如果数据中逗号比较多就会导致数据混乱。
①POI导出
1.导入poi依赖或者导入jar包E:\百度\poi-3.9.jar
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
<scope>compile</scope>
</dependency>
2.excel帮助类
package com.yszh.pdf.listdemo;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
public class ExcelUtil {
/**
* 导出excel,带合并单元格表头
* @param title sheet名称及excel标题名称
* @param rowsName 每列列头名称数组
* @param dataList 数据内容
* @param fileName 文件名
* @param response 输出流
* @throws Exception
*/
public void exportExcel(String title,String[] rowsName,List<Object[]> dataList,String fileName,HttpServletResponse response) throws Exception{
OutputStream output = response.getOutputStream();
response.reset();
response.setHeader("Content-disposition",
"attachment; filename=" + fileName + ".xls");
response.setContentType("application/msexcel");
this.export(title,rowsName,dataList,true,output);
this.close(output);
}
/**
* 导出excel,不带合并单元格表头
* @param title sheet名称及excel标题名称
* @param rowsName 每列列头名称数组
* @param dataList 数据内容
* @param fileName 文件名
* @param response 输出流
* @throws Exception
*/
public void exportExcelNoTitle(String title,String[] rowsName,List<Object[]> dataList,String fileName,HttpServletResponse response) throws Exception{
OutputStream output = response.getOutputStream();
response.reset();
response.setHeader("Content-disposition",
"attachment; filename=" + fileName + ".xls");
response.setContentType("application/msexcel");
this.export(title,rowsName,dataList,false,output);
this.close(output);
}
/**
* 导出excel
* @param title sheet名称及excel标题名称
* @param rowName 每列列头名称数组
* @param dataList 数据内容
* @param tag 是否带表头
* @param out 输出流
* @throws Exception
*/
private void export(String title,String[] rowName,List<Object[]> dataList,boolean tag,OutputStream out) throws Exception {
try {
// 创建工作簿对象
HSSFWorkbook workbook = new HSSFWorkbook();
// 创建工作表
HSSFSheet sheet = workbook.createSheet(title);
// 产生表格标题行
HSSFRow rowm = sheet.createRow(0);
//创建表格标题列
HSSFCell cellTiltle = rowm.createCell(0);
// sheet样式定义; getColumnTopStyle(); getStyle()均为自定义方法 --在下面,可扩展
// 获取列头样式对象
HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);
// 获取单元格样式对象
HSSFCellStyle style = this.getStyle(workbook);
//设置居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
int titleNum = 0;
if(tag){
//合并表格标题行,合并列数为列名的长度,第一个0为起始行号,第二个1为终止行号,第三个0为起始列好,第四个参数为终止列号
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length - 1)));
//设置标题行样式
cellTiltle.setCellStyle(columnTopStyle);
//设置标题行值
cellTiltle.setCellValue(title);
titleNum=2;
}
// 定义所需列数
int columnNum = rowName.length;
// 在索引2的位置创建行(最顶端的行开始的第二行)
HSSFRow rowRowName = sheet.createRow(titleNum);
// 将列头设置到sheet的单元格中
for (int n = 0; n < columnNum; n++) {
// 创建列头对应个数的单元格
HSSFCell cellRowName = rowRowName.createCell(n);
// 设置列头单元格的数据类型
cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING);
HSSFRichTextString text = new HSSFRichTextString(rowName[n]);
// 设置列头单元格的值
cellRowName.setCellValue(text);
// 设置列头单元格样式
cellRowName.setCellStyle(columnTopStyle);
}
// 将查询出的数据设置到sheet对应的单元格中
for (int i = 0; i < dataList.size(); i++) {
// 遍历每个对象
Object[] obj = dataList.get(i);
// 创建所需的行数
HSSFRow row = sheet.createRow(i + titleNum+1);
for (int j = 0; j < obj.length; j++) {
// 设置单元格的数据类型
HSSFCell cell = null;
if (j == 0) {
cell = row.createCell(j, HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(i + 1);
} else {
cell = row.createCell(j, HSSFCell.CELL_TYPE_STRING);
if (!"".equals(obj[j]) && obj[j] != null) {
// 设置单元格的值
cell.setCellValue(obj[j].toString());
}
}
// 设置单元格样式
cell.setCellStyle(style);
}
}
// 让列宽随着导出的列长自动适应
for (int colNum = 0; colNum < columnNum; colNum++) {
int columnWidth = sheet.getColumnWidth(colNum) / 256;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
HSSFRow currentRow;
// 当前行未被使用过
if (sheet.getRow(rowNum) == null) {
currentRow = sheet.createRow(rowNum);
} else {
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(colNum) != null) {
HSSFCell currentCell = currentRow.getCell(colNum);
if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
int length = currentCell.getStringCellValue()
.getBytes().length;
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
if (colNum == 0) {
sheet.setColumnWidth(colNum, (columnWidth - 2) * 256);
} else {
sheet.setColumnWidth(colNum, (columnWidth + 4) * 256);
}
}
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 列头单元格样式
* @param workbook
* @return
*/
private HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {
// 设置字体
HSSFFont font = workbook.createFont();
// 设置字体大小
font.setFontHeightInPoints((short) 11);
// 字体加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 设置字体名字
font.setFontName("宋体");
// 设置样式;
HSSFCellStyle style = workbook.createCellStyle();
// 设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
// 设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
// 设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
// 设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
// 设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
// 设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
// 设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
// 设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
// 在样式用应用设置的字体;
style.setFont(font);
// 设置自动换行;
style.setWrapText(false);
// 设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}
/**
* 列数据信息单元格样式
* @param workbook
* @return
*/
private HSSFCellStyle getStyle(HSSFWorkbook workbook) {
// 设置字体
HSSFFont font = workbook.createFont();
// 设置字体大小
// font.setFontHeightInPoints((short)10);
// 字体加粗
// font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 设置字体名字
font.setFontName("黑体");
// 设置样式;
HSSFCellStyle style = workbook.createCellStyle();
// 设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
// 设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
// 设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
// 设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
// 设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
// 设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
// 设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
// 设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
// 在样式用应用设置的字体;
style.setFont(font);
// 设置自动换行;
style.setWrapText(false);
// 设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}
/**
* 关闭输出流
* @param os
*/
private void close(OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.使用poi导出的时候只需要调用exportExce(String title,String[] rowsName,List<Object[]> dataList,String fileName,HttpServletResponse response)方法就可以了。title定义表名字,rowsName:定义excel表格的表头
@GetMapping("exportExcelFontDisplayAboutReports")
@ApiOperation(value = "前台展示报告列表导出",notes="isAll:0单选导出,isAll:1表示待筛选条件的导出")
public void exportExcelFontDisplayAboutReports(HttpServletResponse response,String[] entityId,Integer isAll,Integer[] ids, String[] level, String[] structId, String[] year, String[] province, String keyWord){
String title = "村庄调查报告列表";
String[] rowsName = new String[]{"序号","名称","字数","页数","类型","年份","评级"};
//定义一个Object[]类型的数组,数组中每个位置对应的元素依次是表头对应的数据
List<Object[]> dataLists = new ArrayList<>();
//从数据库中查出数据封装到Map中
List<Map<String,Object>> export = null;
Integer num = 0;
if(isAll == 0){
export = iOralHisFilesService.getVillageFileByConditionToExcel(entityId,null,null,null,null,null,null);
}else{
export = iOralHisFilesService.getVillageFileByConditionToExcel(null,ids,level,structId,year,province,keyWord);
}
//遍历Map集合并赋值给dataLists中的数组元素
for (Map<String,Object> map: export) {
Object[] objects = new Object[rowsName.length];
objects[0] = num++;
objects[1] = map.get("FileName") != null ? map.get("FileName") : " ";
objects[2] = map.get("WordCount") != null ? map.get("WordCount") : " ";
objects[3] = map.get("FileSize") != null ? map.get("FileSize") : " ";
objects[4] = map.get("structName") != null ? map.get("structName") : " ";
objects[5] = map.get("year") != null ? map.get("year") : " ";
objects[6] = map.get("level") != null ? map.get("level") : " ";
dataLists.add(objects);
}
ExcelUtil excelUtil = new ExcelUtil();
try {
//生成Excel文件的文件名
String fileName = new String("村庄调查报告列表".getBytes("UTF-8"), "iso-8859-1");
excelUtil.exportExcel(title,rowsName,dataLists,fileName,response);
} catch (Exception e) {
e.printStackTrace();
}
}
注意:在给表头赋值的时候一定要做判断,判断查出来的数据是否为空,(为空的话就需要将他设置为“ ”)不然会报错
②csv的导出
在做csv的导出的时候,由于从网上看的发给发不具有通用性,就将她封装了一下。其中利用到了反射的相关知识。只需要传几个简单的参就可以导出报表了
1.导入csv依赖:
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>
2.封装的csv导出方法
/**
*
* @param dataList 封装成Pojo类的List
* @param tableName 表头名字
* @param request request
* @param response response
* @param fileName 文件名
* @param methodNames 需要导入字段的get方法名字(如果需要添加序号,请在数组前添加一个"num",没有序号就传get方法名字)
* @param <T>
* @throws Exception
*/
public static<T> void exportByCVS(List<T> dataList, String[] tableName, HttpServletRequest request, HttpServletResponse response, String fileName, String[] methodNames) throws Exception {
List<T> t = dataList;
// 文件名
request.getHeader("User-Agent");
response.setCharacterEncoding(request.getCharacterEncoding());
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
ServletOutputStream out = response.getOutputStream();
CsvWriter write = new CsvWriter(out, ',', Charset.forName("gb2312"));
write.setForceQualifier(true);
Integer num = 0;
//定义表头
write.writeRecord(tableName);
//比如说查出来10条数据,首先遍历这10条数据,利用反射根据表头需要的信息循环每条数据中的get方法进行赋值
//每条信息对应的数据内容中get方法中的返回值将会存入数组之中。每次循环完之后调用 write.writeRecord(insert)方法姐可以将
//数据插入到文件中
for (int i = 0; i < t.size(); i++) {
//定义表头数据的长度
String[] insert = new String[tableName.length];
//从数据库中查出来的字段根据需要获取字段对应的get方法
for (int k = 0; k < methodNames.length; k++) {
//获取数据源中泛型的类型
T t1 = t.get(i);
//如果在methodNames中定义了num就表示需要序号这一列
if( k == 0 && "num".equals(methodNames[0])){
insert[0] = String.valueOf(++num);
}else{
//获取数据源List中泛型的类名
String name = t1.getClass().getName();
//根据类名加载字节码文件
Class<?> aClass = Class.forName(name);
//根据传入的参数获取数据源List中泛型中的get方法
Method paramGetMethod = aClass.getMethod(methodNames[k]);
//获取数据源List中泛型中的get方法的返回值并判断类型
Type returnType = paramGetMethod.getAnnotatedReturnType().getType();
String temp = null;
//判断get方法返回值的类型,防止类型转换错误。此处调用数据源中泛型中的对象引用区执行方法就可以获得数据
if("java.lang.Integer".equals(returnType.getTypeName())){
temp = String.valueOf((Integer) paramGetMethod.invoke(t1));
}else{
temp = (String)paramGetMethod.invoke(t1);
}
//写入数据中
insert[k] = temp != null ? temp : "";
}
}
try {
//调用依赖中csv的方法去将数据中的数据写入文件中
write.writeRecord(insert);
} catch (IOException e) {
e.printStackTrace();
}
}
write.flush();
write.close();
response.flushBuffer();
}
3.调用导出方法。传入对应的参数即可
@Resource
OralHisFilesMapper oralHisFilesMapper;
@GetMapping("exportExcelFontDisplayAboutReportsToCSV")
@ApiOperation(value = "前台展示报告列表导出",notes="isAll:0单选导出,isAll:1表示待筛选条件的导出")
public void exportExcelFontDisplayAboutReportsToCSV(HttpServletRequest request,HttpServletResponse response,String[] entityId,Integer isAll,Integer[] ids, String[] level, String[] structId, String[] year, String[] province, String keyWord) throws Exception {
String[] tableName = { "名称","字数","页数","类型","年份","评级"};
String fileName = new String("民国调查报告列表.csv".getBytes("UTF-8"), "ISO-8859-1");
String[] methodNames = {"getFileName","getWordCount","getFileSize","getTypeName","getYear","getLevel"};
List<OralHisFiles> dataLists = null;
Integer num = 0;
if(isAll == 0){
dataLists = oralHisFilesMapper.getVillageFileByCondition(null,ids,level,structId,year,province,keyWord);
}else{
dataLists = oralHisFilesMapper.getVillageFileByConditionTocsv(ids,level,structId,year,province,keyWord);
}
CsvUtils.exportByCVS(dataLists,tableName,request,response,fileName,methodNames);
}
备注:csv文件是以“,”分割文件的。如果你的导入数据中又大量的逗号,建议使用poi的形式导出。但相比较而言,他的导出速度会非常快。
Java POI与CSV导出Excel对比分析

在文史类统计项目中,导出Excel报表的需求促使作者对比了使用Apache POI和CSV两种方式。POI导出时需要注意表头数据为空的情况,而CSV导出虽然速度较快,但因数据中逗号可能导致数据混乱。作者还提供了POI和CSV的导出实现方法。
3108

被折叠的 条评论
为什么被折叠?



