前言
网上找了好久资源都没找到把自己之前开发使用的Excel工具类贴出来,
Mavn 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>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
工具类
/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS." Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY. Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.example.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
/**
* @ClassName: ExcelUtil
* @Description:导出excel表格
* @author zzw
* @date 2017年5月31日 下午2:20:20
*/
public class ExcelUtil {
public static final int PERPAGENUM = 50000;
/**
* @param excelName 表格名称
* @param colwidth 列宽
* @param titleNames 标题名称
* @param listExcelValue 每一行的值
* @throws Exception
* @Title: createExcel
* @Description: 创建excel表格
* @return File
* @author zzw
* @date 2017年5月31日 下午2:20:54
*/
public static byte[] createExcelByte(String excelName, int[] colwidth, String[] titleNames,
List<String[]> listExcelValue) throws Exception {
String excelFullName = excelName + ".xls";// 生成excel表格文件名称
int sheetNum = 1;// 工作薄sheet编号
int bodyRowCount = 1;// 正文内容行号
int currentRowCount = 1;// 当前的行号
HSSFWorkbook workbook = new HSSFWorkbook();// 创建excel
HSSFSheet sheet = workbook.createSheet(excelName + "(" + sheetNum + ")");// 创建一个工作薄
HSSFRow row = null;// 创建一行
HSSFCell cell = null;// 每个单元格
HSSFCellStyle titleCellStyle = createTitleCellStyle(workbook);// 设置标题样式
writeTitleContent(sheet, titleCellStyle, colwidth, titleNames);// 写入标题
HSSFCellStyle bodyCellStyle = createBodyCellStyle(workbook);// 设置正文样式
// 中文乱码处理
// request.setCharacterEncoding(Constant.ENCODE_UTF);
// response.setCharacterEncoding(Constant.ENCODE_UTF);
// 第二行开始写入数据 正文内容
for (String[] lineValue : listExcelValue) {
row = sheet.createRow(bodyRowCount);// 创建一行
// 每行excel单元格
for (int i = 0; i < lineValue.length; i++) {
cell = row.createCell(i);// 创建一个单元格
cell.setCellStyle(bodyCellStyle);// 给单元格设置样式
cell.setCellValue(lineValue[i]);// 给单元格设置值
}
// 每个sheet显示50000条数据,超过50000创建一个新的sheet页
if (listExcelValue.size() > currentRowCount && currentRowCount % PERPAGENUM == 0) {
sheet = null;
sheetNum++;// 工作薄编号递增1
sheet = workbook.createSheet(excelName + "(" + sheetNum + ")");// 创建一个新的工作薄
bodyRowCount = 0;// 正文内容行号置位为0
writeTitleContent(sheet, titleCellStyle, colwidth, titleNames);// 写入标题
}
bodyRowCount++;// 正文内容行号递增1
currentRowCount++;// 当前行号递增1
}
listExcelValue = null;
ByteArrayOutputStream os = new ByteArrayOutputStream();
workbook.write(os);
return os.toByteArray();
}
/**
* @param excelName 表格名称
* @param colwidth 列宽
* @param titleNames 标题名称
* @param listExcelValue 每一行的值
* @throws Exception
* @Title: createExcel
* @Description: 创建excel表格
* @return ResponseEntity<byte[]>
* @author zzw
* @date 2017年5月31日 下午2:20:54
*/
public static ResponseEntity<byte[]> createExcel(String excelName, int[] colwidth, String[] titleNames,
List<String[]> listExcelValue) throws Exception {
String excelFullName = excelName + ".xls";// 生成excel表格文件名称
int sheetNum = 1;// 工作薄sheet编号
int bodyRowCount = 1;// 正文内容行号
int currentRowCount = 1;// 当前的行号
HSSFWorkbook workbook = new HSSFWorkbook();// 创建excel
HSSFSheet sheet = workbook.createSheet(excelName + "(" + sheetNum + ")");// 创建一个工作薄
HSSFRow row = null;// 创建一行
HSSFCell cell = null;// 每个单元格
HSSFCellStyle titleCellStyle = createTitleCellStyle(workbook);// 设置标题样式
writeTitleContent(sheet, titleCellStyle, colwidth, titleNames);// 写入标题
HSSFCellStyle bodyCellStyle = createBodyCellStyle(workbook);// 设置正文样式
// 中文乱码处理
// request.setCharacterEncoding(Constant.ENCODE_UTF);
// response.setCharacterEncoding(Constant.ENCODE_UTF);
// 第二行开始写入数据 正文内容
for (String[] lineValue : listExcelValue) {
row = sheet.createRow(bodyRowCount);// 创建一行
// 每行excel单元格
for (int i = 0; i < lineValue.length; i++) {
cell = row.createCell(i);// 创建一个单元格
cell.setCellStyle(bodyCellStyle);// 给单元格设置样式
cell.setCellValue(lineValue[i]);// 给单元格设置值
}
// 每个sheet显示50000条数据,超过50000创建一个新的sheet页
if (listExcelValue.size() > currentRowCount && currentRowCount % PERPAGENUM == 0) {
sheet = null;
sheetNum++;// 工作薄编号递增1
sheet = workbook.createSheet(excelName + "(" + sheetNum + ")");// 创建一个新的工作薄
bodyRowCount = 0;// 正文内容行号置位为0
writeTitleContent(sheet, titleCellStyle, colwidth, titleNames);// 写入标题
}
bodyRowCount++;// 正文内容行号递增1
currentRowCount++;// 当前行号递增1
}
listExcelValue = null;
ByteArrayOutputStream os = new ByteArrayOutputStream();
workbook.write(os);
// String filename = new
// String(excelFullName.getBytes(Constant.ENCODE_UTF),
// Constant.ENCODE_IOS8);// 为了解决中文名称乱码问题
// String filename = excelFullName;
// String filename = URLEncoder.encode(excelFullName,
// EncodeCharSet.UTF8.getId());
HttpHeaders headers = new HttpHeaders();
headers.setContentDispositionFormData("attachment", excelFullName=java.net.URLEncoder.encode(excelFullName, "UTF-8"));
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
List<String> accessHeaders = new ArrayList<>();
accessHeaders.add("Content-Disposition");
headers.setAccessControlExposeHeaders(accessHeaders);
return new ResponseEntity<byte[]>(os.toByteArray(), headers, HttpStatus.OK);
}
/**
* @param workbook
* @Title: createBodyCellStyle
* @Description: 设置正文单元样式
* @return HSSFCellStyle
* @author zzw
* @date 2017年5月31日 上午9:57:20
*/
public static HSSFCellStyle createBodyCellStyle(HSSFWorkbook workbook) {
HSSFCellStyle cellStyle = workbook.createCellStyle();
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 8);
font.setFontName(HSSFFont.FONT_ARIAL);// 设置标题字体
cellStyle.setFont(font);
cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
return cellStyle;
}
/**
* @param workbook
* @Title: createDateBodyCellStyle
* @Description: 设置正文单元时间样式
* @return HSSFCellStyle
* @author zzw
* @date 2017年5月31日 上午9:58:08
*/
public static HSSFCellStyle createDateBodyCellStyle(HSSFWorkbook workbook) {
HSSFCellStyle cellStyle = workbook.createCellStyle();
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 8);
font.setFontName(HSSFFont.FONT_ARIAL);// 设置标题字体
cellStyle.setFont(font);
cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
HSSFDataFormat format = workbook.createDataFormat();
cellStyle.setDataFormat(format.getFormat("yyyy-mm-dd"));
return cellStyle;
}
/**
* @param workbook
* @Title: createTitleCellStyle
* @Description: 设置标题单元样式
* @return HSSFCellStyle
* @author zzw
* @date 2017年5月31日 上午9:58:24
*/
public static HSSFCellStyle createTitleCellStyle(HSSFWorkbook workbook) {
HSSFCellStyle cellStyle = workbook.createCellStyle();
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeightInPoints((short) 8);
font.setFontName(HSSFFont.FONT_ARIAL);// 设置标题字体
cellStyle.setFont(font);
cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);// 设置列标题样式
cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);// 设置背景色
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
return cellStyle;
}
/**
* @param sheet
* @param cellStyle
* @Title: writeTitleContent
* @Description: 写入标题行
* @return void
* @author zzw
* @date 2017年5月31日 上午9:58:51
*/
public static void writeTitleContent(HSSFSheet sheet, HSSFCellStyle cellStyle, int[] colwidth, String[] titleNames) {
HSSFRow row = sheet.createRow(0);// 创建一行
HSSFCell cell = null;
for (int i = 0; i < titleNames.length; i++) {
sheet.setColumnWidth(i, colwidth[i]);// 设置列宽
cell = row.createCell(i);// 创建单元格
cell.setCellStyle(cellStyle);// 创建单元样式
cell.setCellValue(titleNames[i]);// 创建单元格内容
}
}
}
示例
@GetMapping(value = "/exports")
public ResponseEntity<byte[]> exports() throws Exception {
String excelName = "测试导出";
// 设置工作薄列宽
int[] colwidth = { 6000, 6000};
// 设置工作薄标题行
String[] titleNames = { "code","中文"};
// excel 表格内容数组集合
List<String[]> listExcelValue = new ArrayList<>();
// 每一行的内容 转换为字符数组 放入list中
String[] tableValue = {"001","点点"};
listExcelValue.add(tableValue);
return ExcelUtil.createExcel(excelName, colwidth, titleNames, listExcelValue);
}