标签:style blog java color os 文件
Apache POI是一个开源的Java读写Excel、WORD等微软OLE2组件文档的项目
结构:
package com.ocj.util; import java.io.FileOutputStream; import java.io.IOException; import java.util.LinkedHashMap; import org.apache.log4j.Logger; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; /** * Copyright (C), 2014-03, ocj * * excel导出工具类 * * @author mzj * @version 1.0 * @date 2014-03 */ public class ExcelExpUtil { private static Logger logger = Logger.getLogger(ExcelExpUtil.class); /** * excel导出 * * @param 导出的fileName文件path * +名 * @param columnNameJson * @param dataJson * @return */ public static int createExcel(String fileName, String columnNameJson, String dataJson) { if (CommonUtils.isBlank(columnNameJson) || CommonUtils.isBlank(dataJson)) { logger.error("===传入的数据为空!"); return 0; } fileName = CommonUtils.isBlank(fileName) ? ConsField.OCJASSISTANT_USERCONF_DIR + "/导出数据.xls" : fileName; @SuppressWarnings("unchecked") LinkedHashMap<String, String> columnNameMap = JSON.parseObject(ConsField.EXCEL_COLUMN_NAME_JSON, LinkedHashMap.class); JSONObject jsonObj = null; JSONArray jsonArr = null; if (dataJson.startsWith("[")) { jsonArr = JSON.parseArray(dataJson); } else if (dataJson.startsWith("{")) { jsonObj = JSON.parseObject(dataJson); } else { logger.error("===传入的数据不是json格式!"); } FileOutputStream fOut = null; try { HSSFWorkbook book = new HSSFWorkbook(); HSSFSheet sheet = null; HSSFRow row = null; short jsonIndex = 0; int index = 0; sheet = book.createSheet(); if (jsonArr != null) { while (jsonArr.size() > 0 && jsonIndex < jsonArr.size()) { row = sheet.createRow(index); // 英文列名: if (index == 0) { Short i = 0; for (String key : columnNameMap.keySet()) { HSSFCell cell = row.createCell(i); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue(key); i++; } index++; continue; } // 中文列名: if (index == 1) { Short i = 0; for (String key : columnNameMap.keySet()) { HSSFCell cell = row.createCell(i); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue(columnNameMap.get(key)); i++; } index++; continue; } // 每列的数据: { short i = 0; JSONObject obj = jsonArr.getJSONObject(jsonIndex); for (String key : columnNameMap.keySet()) { HSSFCell cell = row.createCell(i); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue(obj.getString(key)); i++; } index++; jsonIndex++; } } }else if(jsonObj!=null){ row = sheet.createRow(index); // 英文列名: if (index == 0) { Short i = 0; for (String key : columnNameMap.keySet()) { HSSFCell cell = row.createCell(i); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue(key); i++; } index++; } // 中文列名: if (index == 1) { Short i = 0; for (String key : columnNameMap.keySet()) { HSSFCell cell = row.createCell(i); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue(columnNameMap.get(key)); i++; } index++; } row = sheet.createRow(index); // 数据: { short i = 0; for (String key : columnNameMap.keySet()) { HSSFCell cell = row.createCell(i); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue(jsonObj.getString(key)); i++; } } } fOut = new FileOutputStream(fileName); book.write(fOut); fOut.flush(); logger.info("文件生成..."); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fOut != null) { fOut.close(); } } catch (IOException e) { e.printStackTrace(); } } return 0; } }
标签:style blog java color os 文件
原文地址:http://www.cnblogs.com/muzhongjiang/p/3509849.html