工具类:
package cn.oa.utils;
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.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class CommonExcelUtils {
/**
* 显示的导出表的标题
* */
private String title;
/**
* 导出表的列名
* */
private String[] colName;
/**
* 导出的文件名
* */
private String fileName;
private List<Object[]> dataList = new ArrayList<Object[]>();
private HttpServletResponse response;
/**
* @param 构造方法,传入要导出的数据
* @param colName
* @param dataList
* @param response
* @param fileName
*/
public CommonExcelUtils(String title, String[] colName, List<Object[]> dataList, HttpServletResponse response, String fileName) {
this.dataList = dataList;
this.colName = colName;
this.title = title;
this.response = response;
this.fileName = fileName;
}
/**
* @Description 导出列表
* @date 2018年8月22日下午4:37:05
*/
public void downloadExcel() 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);