在实际中导出excel非常常见,于是自己封装了一个导出数据到excel的工具类,先附上代码,最后会写出实例和解释。支持03和07两个版本的 excel。
HSSF导出的是xls的excel,XSSF导出的是xlsx的excel,SXSSF导出的也是xlsx的excel,只不过这个用于处理数据量大的情况,生成文件之后数据不会留在内存中。
代码中依赖了slf4j日志包,commons-io包的IOUtils关闭流,commons-lang和commons-collections包等包。
package cn.xm.exam.utils; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.collections.MapUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.ArrayUtils; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ExcelExporter { private static final Logger LOGGER = LoggerFactory.getLogger(ExcelExporter.class); private String[] headerNames; private Workbook workBook; private Sheet sheet; /** * * @param headerNames * 表头 * @param sheetName * sheet的名称 * @param excelVerson * excel的版本 */ public ExcelExporter(String[] headerNames, String sheetName, String excelVerson) { this.headerNames = headerNames; // 创建一个工作簿 if ("07".equals(excelVerson)) { // workBook = new XSSFWorkbook();//处理07版本excel workBook = new SXSSFWorkbook();// 处理07版本,但是适用于大数据量,导出之后数据不会占用内存 } else { workBook = new HSSFWorkbook(); } // 创建一个工作表sheet sheet = workBook.createSheet(sheetName); initHeader(); } /** * 初始化表头信息 */ private void initHeader() { // 创建第一行 Row row = sheet.createRow(0); Cell cell = null; // 创建表头 for (int i = 0; i < headerNames.length; i++) { cell = row.createCell(i); cell.setCellValue(headerNames[i]); setCellStyle(cell); } } /** * 设置单元格样式 * * @param cell * 单元格 */ public void setCellStyle(Cell cell) { // 设置样式 CellStyle cellStyle = workBook.createCellStyle(); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 设置字体居中 // 设置字体 Font font = workBook.createFont(); font.setFontName("宋体"); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字体加粗 font.setFontHeightInPoints((short) 13); cellStyle.setFont(font); cell.setCellStyle(cellStyle); } /** * 创建行内容(每一行的数据装在list中) * * @param datas * 每一行的数据 * @param rowIndex * 行号(从1开始) */ public void createTableRow(List<String> datas, int rowIndex) { // 创建第i行 Row row = sheet.createRow(rowIndex); Cell cell = null; // 写入数据 for (int index = 0, length = datas.size(); index < length; index++) { // 参数代表第几列 cell = row.createCell(index); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(datas.get(index)); } } /** * * @param datas * 数据,每一个map都是一行 * @param keys * key[i]代表从map中获取keys[i]的值作为第i列的值,如果传的是null默认取表头 */ public void createTableRows(List<Map<String, Object>> datas, String[] keys) { for (int i = 0, length_1 = datas.size(); i < length_1; i++) { if (ArrayUtils.isEmpty(keys)) { keys = headerNames; } // 创建行(从第二行开始) Map<String, Object> data = datas.get(i); Row row = sheet.createRow(i + 1); Cell cell = null; for (int j = 0, length_2 = keys.length; j < length_2; j++) { // 单元格获取map中的key String key = keys[j]; String value = MapUtils.getString(data, key, ""); cell = row.createCell(j); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(value); } } } /** * 根据表头自动调整列宽度 */ public void autoAllSizeColumn() { if (sheet instanceof SXSSFSheet) {// 如果是SXSSFSheet,需要调用trackAllColumnsForAutoSizing方法一次 SXSSFSheet tmpSheet = (SXSSFSheet) sheet; tmpSheet.trackAllColumnsForAutoSizing(); } for (int i = 0, length = headerNames.length; i < length; i++) { sheet.autoSizeColumn(i); } } /** * 将数据写出到excel中 * * @param outputStream */ public void exportExcel(OutputStream outputStream) { // 导出之前先自动设置列宽 this.autoAllSizeColumn(); try { workBook.write(outputStream); } catch (IOException e) { LOGGER.error(" exportExcel error", e); } finally { IOUtils.closeQuietly(outputStream); } } }
上面的代码逻辑非常简单,创建实例的时候就初始化表头信息和创建sheet。
向excel中填充数据的方式有两种,就是上面的createTableRow方法和createTableRows方法。
createTableRow(List,int)方法就是多次调用此方法,list中数据,int是行号。list中数据依次作为第i行的列数据。
createTableRows(List<Map>,String[])这个方法应该是非常常用的一种方式。我们从数据库查询到的数据大多数映射为Map放入list中,因此上面的方法就比较常用。List<Map>参数就是所有的数据,一个Map代表一行,String[]是Map中的key,也就是数组的第一个元素对应的key作为第一列,第二个元素是作为第二列。如果map数据的key正好与表头一致我们可以传一个null。(因为Map是基于数组+链表,且存入的是无序的,所以无法直接通过map中的key确定列。除非传的数据是LinkedHashMap)。这个方法封装的比较好,如果一条数据中没有对应的值会将此单元格设为空。
下面是自己测试代码:
(1)测试List<String>写入数据,导出03版本的excel
public static void test1() { ExcelExporter hssfWorkExcel = new ExcelExporter(new String[] { "姓名", "年龄" }, "人员基本信息", "03"); for (int i = 0; i < 10; i++) { List<String> data = new ArrayList<>(); data.add("namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss" + i); data.add("" + (i + 20)); hssfWorkExcel.createTableRow(data, i + 1); } try { hssfWorkExcel.exportExcel(new FileOutputStream(new File("e:/test.xls"))); } catch (FileNotFoundException e) { e.printStackTrace(); } }
结果:
(2)测试List<Map>写入数据,导出07版本的excel
public static void test2() { HSSFWorkExcel hssfWorkExcel = new HSSFWorkExcel(new String[] { "姓名", "年龄" }, "人员基本信息","07"); List<Map<String, Object>> datas = new ArrayList<>(); for (int i = 0; i < 10; i++) { Map data = new HashMap<>(); data.put("name", "tttttttttttttt" + i); data.put("age", "age" + i); datas.add(data); } hssfWorkExcel.createTableRows(datas, new String[] { "name", "age" }); try { hssfWorkExcel.exportExcel(new FileOutputStream(new File("e:/test1.xls"))); } catch (FileNotFoundException e) { e.printStackTrace(); } }
结果:
下面附上一段自己实际中用到的代码:
果然在导出excel的时候方便多了,只需要短短的几行代码就可以生成一个excel到本地。
package cn.xm.exam.action.safeHat; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.text.ParseException; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.commons.lang3.time.DateUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport; import cn.xm.exam.service.safehat.SafehatService; import cn.xm.exam.utils.ExamSystemUtils; import cn.xm.exam.utils.HSSFWorkExcel; import cn.xm.exam.utils.ValidateCheck; /** * 导出安全帽台账 * * @author Administrator * */ @Controller public class ExtSafeHatTaizhang extends ActionSupport { private static final Logger log = LoggerFactory.getLogger(ExtSafeHatTaizhang.class); private String userName; private String idCard; private String safeHatNum; private String fileName; @Autowired private SafehatService safehatService; @Override public String execute() throws Exception { return SUCCESS; } public InputStream getInputStream() { // 获取一个临时文件 File file = ExamSystemUtils.getTmpFile(); // 只用返回一个输入流 try { // 查数据 List<Map<String, Object>> safehatTaizhang = getSafehatTaizhang(); // 写入文件中 String[] headerNames = new String[] { "序号", "年龄", "性别", "学历", "身体状况", "职务(工种)", "成绩", "身份证号", "安全帽编号", "备注" }; HSSFWorkExcel hssfWorkExcel = new HSSFWorkExcel(headerNames, "安全帽台账信息","03"); String[] keys = new String[] { "index", "age", "sex", "empeducate", "empphysicalstatus", "empType", "thirdScore", "idCode", "safeHatNum", "desc" }; hssfWorkExcel.createTableRows(safehatTaizhang, keys); hssfWorkExcel.exportExcel(new FileOutputStream(file)); // 返回流 return FileUtils.openInputStream(file); } catch (IOException e) { log.error("getInputStream error", e); } return null; } public List<Map<String, Object>> getSafehatTaizhang() { Map condition = new HashMap<>(); if (ValidateCheck.isNotNull(userName)) { condition.put("userName", userName); } if (ValidateCheck.isNotNull(idCard)) { condition.put("idCard", idCard); } if (ValidateCheck.isNotNull(safeHatNum)) { condition.put("safeHatNum", safeHatNum); } // 开始分页 List<Map<String, Object>> results = null; try { results = safehatService.getSafehatTaizhang(condition); } catch (Exception e) { log.error("getSafehatTaizhang error", e); results = new ArrayList<>(); return results; } if (CollectionUtils.isNotEmpty(results)) { disPoseResults(results); } return results; } private void disPoseResults(List<Map<String, Object>> results) { int index = 1; for (Map map : results) { // 装一个序号进去 map.put("index", index++); // 处理性别 if (StringUtils.isNotBlank(MapUtils.getString(map, "sex"))) { if ("1".equals(map.get("sex").toString())) { map.put("sex", "男"); } if ("2".equals(map.get("sex").toString())) { map.put("sex", "女"); } } // 处理年龄 String birthday = MapUtils.getString(map, "birthday"); if (StringUtils.isNotBlank(birthday)) { Date date = null; try { date = DateUtils.parseDateStrictly(birthday, "yyyy-MM-dd"); } catch (ParseException e) { date = new Date(); } int birhYear = date.getYear(); int nowYear = (new Date()).getYear(); map.put("age", (nowYear - birhYear) + 1); } } } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getIdCard() { return idCard; } public void setIdCard(String idCard) { this.idCard = idCard; } public String getSafeHatNum() { return safeHatNum; } public void setSafeHatNum(String safeHatNum) { this.safeHatNum = safeHatNum; } public SafehatService getSafehatService() { return safehatService; } public void setSafehatService(SafehatService safehatService) { this.safehatService = safehatService; } public String getFileName() { String name = "安全帽台账" + DateFormatUtils.format(new Date(), "yyyy-MM-dd") + ".xls"; try { name = new String(name.getBytes("UTF-8"), "ISO8859-1"); } catch (UnsupportedEncodingException e) { log.error("getFileName error", e); return DateFormatUtils.format(new Date(), "yyyy-MM-dd") + ".xls"; } return name; } }