JAVA编码(61)—— 解析Excel文件 xls、xlsx

java

package com.sinosoft.smallmicro.service.impl.smallmicro;

import java.io.IOException;

import java.io.InputStream;

import java.text.ParseException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.HashSet;

import java.util.List;

import java.util.Map;

import java.util.Set;

import java.util.regex.Pattern;

import org.apache.commons.fileupload.FileItem;

import org.apache.commons.lang.StringUtils;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

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.xssf.usermodel.XSSFRow;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.sinosoft.smallmicro.common.CommonUtil;

import com.sinosoft.smallmicro.common.PubFun;

import com.sinosoft.smallmicro.entity.smallmicro.SmallMicroFileUpload;

import com.sinosoft.smallmicro.service.smallmicro.SmallMicroFileUploadService;

import com.sinosoft.smallmicro.util.DateUtil;

import com.sinosoft.smallmicro.util.ExcelUtil;

public class SmallMicroFileUploadServiceImpl implements SmallMicroFileUploadService{

private Log logger = LogFactory.getLog("SmallMicroFileUploadServiceImpl");

/**

* 被保险人清单内容处理

* @param fileUpload

*/

@Override

public void fileUpload(SmallMicroFileUpload fileUpload) {

// TODO Auto-generated method stub

Map<String, Object> retMap = fileUpload.getRetMap();

Map<String, Object> resMap = fileUpload.getResMap();

try {

HashMap<String, Object> map = fileUpload.getMap();

String efile = fileUpload.getEfile();

String pathname = fileUpload.getPathname();

FileItem i = fileUpload.getIs();

String bendifilename = fileUpload.getBendifilename();

String preamall = String.valueOf(map.get("pream"));//保费集合

String product_type = String.valueOf(map.get("product_type"));//产品类型(团意险or交通险)

String jobcode = String.valueOf(map.get("jobcode"));//职业编码(试算时职业编码)

if(CommonUtil.isEmpty(product_type)){

logger.info("产品类型不能为空");

resMap.put("Flag", "N#系统繁忙,请稍后再试");

PubFun.FlagMap.put(pathname, resMap);

return;

}

if (CommonUtil.isEmpty(preamall)) {

logger.info("保费集合不能为空");

resMap.put("Flag", "N#系统繁忙,请稍后再试");

PubFun.FlagMap.put(pathname, resMap);

return;

}

if(CommonUtil.isEmpty(jobcode)){

logger.info("职业编码不能为空");

resMap.put("Flag", "N#系统繁忙,请稍后再试");

PubFun.FlagMap.put(pathname, resMap);

return;

}

String[] pArray = preamall.split(",");

InputStream is = i.getInputStream();

int pronum = 0 ; //被保险人清单中的申请人数

//校验姓名正则

String nameRegex = "^(([\u4e00-\u9fa5\\sa-zA-Z]{1,50})|(([a-zA-Z]+\\s)+[a-zA-Z]+{1,50}))$";

// String dateRegex = "^((\\d{4}-\\d{2}-\\d{2})|(\\d{4}/\\d{2}/\\d{2}))";//r日期格式正则

String dateRegex = "^((\\d{4}-\\d{1,2}-\\d{1,2})|(\\d{4}/\\d{1,2}/\\d{1,2}))";//r日期格式正则

List<String> idCardList = new ArrayList<String>();

//文件为.xls

if(".xls".equals(efile)){

HSSFWorkbook wb = new HSSFWorkbook(is);

HSSFSheet sheet = wb.getSheetAt(0);

int num = sheet.getLastRowNum();//获取excel最大行

if(num > 5){//数据从第五行开始(excel模板)

//快钱支付接口被保险人参数

List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();

//判断每行数据是否完整

for(int j=5 ; j< num ; j++){

String InsuranceJobCode ="";

//被保险人数据

HashMap<String, Object> dateMap = new HashMap<String, Object>();

HSSFRow hrow = sheet.getRow(j);//获取第j行数据

boolean falg1 = false;

boolean falg2 = false;

boolean falg3 = false;

boolean falg4 = false;

boolean falg5 = false;

boolean falg6 = false;

if(StringUtils.isBlank(ExcelUtil.getCellValue(hrow.getCell(1)))){

falg1 = true;

}

if(StringUtils.isBlank(ExcelUtil.getCellValue(hrow.getCell(2)))){

falg2 = true;

}

if(StringUtils.isBlank(ExcelUtil.getCellValue(hrow.getCell(3)))){

falg3 = true;

}

if(StringUtils.isBlank(ExcelUtil.getCellValue(hrow.getCell(4)))){

falg4 = true;

}

if(StringUtils.isBlank(ExcelUtil.getCellValue(hrow.getCell(5)))){

falg5 = true;

}

if("0".equals(product_type)){//团意险有第六列(职业代码)

if(StringUtils.isBlank(ExcelUtil.getCellValue(hrow.getCell(6)))){

falg6 = true;

if (falg1 && falg2 && falg3 && falg4 && falg5 && falg6) {

continue;

}

}else{

InsuranceJobCode = ExcelUtil.getCellValue(hrow.getCell(6));

}

}else {

if (falg1 && falg2 && falg3 && falg4 && falg5) {

continue;

}

}

if(falg1 || falg2 || falg3 || falg4 || falg5 || falg6){

logger.info("上传文件被保险人清单中数据不完整,产品类型:"+product_type+"(0:团意险,1:交通险)");

resMap.put("Flag", "N#文件:被保险人清单,第"+(j+1)+"行被保险人数据不完整!");

PubFun.FlagMap.put(pathname, resMap);

return;

}

//被保险人数据,姓名,证件类型,证件号码,出生日期,性别

String InsureName = ExcelUtil.getCellValue(hrow.getCell(1));

String CertificateType = ExcelUtil.getCellValue(hrow.getCell(2));

String CertificateCode = ExcelUtil.getCellValue(hrow.getCell(3));

String Birthday = ExcelUtil.getCellValue(hrow.getCell(4));

String Sex = ExcelUtil.getCellValue(hrow.getCell(5));

if("0".equals(product_type)){//团意险

if(!jobcode.equals(InsuranceJobCode)){

logger.info("上传文件被保险人清单中职业编码不正确");

resMap.put("Flag", "N#文件:被保险人清单,"+InsureName+"的职业编码不正确!请核查后重新上传!");

PubFun.FlagMap.put(pathname, resMap);

return;

}

}

if(!Pattern.matches(nameRegex, InsureName)){//校验姓名

logger.info("上传文件被保险人清单中被保险人姓名格式不正确");

resMap.put("Flag", "N#表格第"+(j+1)+"行的被保险人姓名:"+InsureName+"不规范!被保险人姓名只能由中文或英文字母(半角格式)组成,最多为50个字符!");

PubFun.FlagMap.put(pathname, resMap);

return;

}

if(StringUtils.isBlank(CertificateType)){//校验证件类型

logger.info("上传文件被保险人清单中被保险人证件类型为空");

resMap.put("Flag", "N#表格第"+(j+1)+"行的被保险人证件类型不能为空!");

PubFun.FlagMap.put(pathname, resMap);

return;

}else{

if("身份证".equals(CertificateType)){

String sexString = "";

if(!Pattern.matches(dateRegex, Birthday)){

logger.info("被保险人日期格式不正确");

resMap.put("Flag", "N#表格第"+(j+1)+"行的被保险人日期格式不正确(yyyy-MM-dd或yyyy/MM/dd)!");

PubFun.FlagMap.put(pathname, resMap);

return;

}

if("男".equals(Sex)){

sexString = "1";

}else if("女".equals(Sex)){

sexString = "2";

}

String message = CommonUtil.IDCardValidate(CertificateCode, DateUtil.convertStringToDate(Birthday.replace("/", "-"), DateUtil.YYYY_MM_DD), sexString);

if(!StringUtils.isBlank(message)){

logger.info("上传文件被保险人清单中被保险人证件号码不正确");

resMap.put("Flag", "N#表格第"+(j+1)+"行的被保险人"+message);

PubFun.FlagMap.put(pathname, resMap);

return;

}

} else {//护照或其他

if(CertificateCode.length() < 3){

logger.info("上传文件被保险人清单中被保险人证件号码不正确");

resMap.put("Flag", "N#表格第"+(j+1)+"行的被保险人证件号码不规范!证件号码不能为空,不能重复,护照或者其他证件至少为三个字符!");

PubFun.FlagMap.put(pathname, resMap);

return;

}

}

}

dateMap.put("InsureName", InsureName);//被保险人姓名

dateMap.put("CertificateType", CertificateType);//证件类型

dateMap.put("CertificateCode", CertificateCode);//证件号码

dateMap.put("Birthday", Birthday);//出生日期

dateMap.put("Sex", Sex);//性别

dateMap.put("InsuranceJobCode", InsuranceJobCode);//职业编码

list.add(dateMap);

pronum++;//参保人数

idCardList.add(CertificateCode);//判断证件号码是否有重复

}

if(!checkRepeat(idCardList)){//判断证件号码是否有重复

logger.info("被保险人清单:证件号码有重复");

resMap.put("Flag", "N#表格被保险人清单,证件号码不能重复!");

PubFun.FlagMap.put(pathname, resMap);

return;

}

if(pronum<5 || pronum >= 1000){

logger.info("被保险人数:"+pronum + "(5<人数范围<1000)");

resMap.put("Flag", "N#文件:被保险人清单,请填写大于5人小于1000人的被保险人");

PubFun.FlagMap.put(pathname, resMap);

return;

}

if("0".equals(product_type)){//团意险

//规模调整系数

double coefficient = 1.0;

if(pronum > 100){

coefficient = 0.8;

}else if(31 < pronum && pronum <=100){

coefficient = 0.9;

}

Double ylPream = 0.00;//原保费

int yfPream = 0;//应付保费

for(int x=0;x<pArray.length;x++){

Double yl = Double.parseDouble(pArray[x]);

ylPream += yl;

yfPream +=(new java.math.BigDecimal(yl*coefficient).setScale(0,java.math.BigDecimal.ROUND_HALF_UP).intValue());

}

Double yl_pream = (ylPream*pronum); // 原保费

int yf_pream = (yfPream*pronum); //应付保费

int pjPream = (yf_pream/pronum); //人均保费

retMap.put("PJ_PREAM", pjPream);//人均保费

retMap.put("YL_PREAM", yl_pream);//原保费

retMap.put("YF_PREAM", yf_pream);//应付保费

retMap.put("PNUM", pronum);//投保人数

retMap.put("BeneficiaryBase", list);//被保险人集合

}else if("1".equals(product_type)){//交通险

String jtPream = pArray[0];

Double jtp = Double.parseDouble(jtPream);

Double jtpicture = jtp*pronum;

retMap.put("PJ_PREAM", jtp);//人均保费

retMap.put("YL_PREAM", jtpicture);//原保费

retMap.put("YF_PREAM", jtpicture);//应付保费

retMap.put("PNUM", pronum);//投保人数

retMap.put("BeneficiaryBase", list);//被保险人集合

}

}else{

logger.info(bendifilename+"未填写投保人信息");

resMap.put("Flag", "N#文件:被保险人清单,请填写被保险人信息");

PubFun.FlagMap.put(pathname, resMap);

return;

}

//文件为.xlsx

}else if(".xlsx".equals(efile)){

XSSFWorkbook wb = new XSSFWorkbook(is);

XSSFSheet sheet = wb.getSheetAt(0);

int num = sheet.getLastRowNum();//获取excel最大行

if(num > 5){//数据从第五行开始(excel模板)

//被保险人数据集合

List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();

//判断每行数据是否完整

for(int j=5 ; j< num ; j++){

String InsuranceJobCode = "";

//被保险人数据

HashMap<String, Object> dateMap = new HashMap<String, Object>();

XSSFRow hrow = sheet.getRow(j);//获取第j行数据

boolean falg1 = false;

boolean falg2 = false;

boolean falg3 = false;

boolean falg4 = false;

boolean falg5 = false;

boolean falg6 = false;

if(StringUtils.isBlank(ExcelUtil.getCellValue(hrow.getCell(1)))){

falg1 = true;

}

if(StringUtils.isBlank(ExcelUtil.getCellValue(hrow.getCell(2)))){

falg2 = true;

}

if(StringUtils.isBlank(ExcelUtil.getCellValue(hrow.getCell(3)))){

falg3 = true;

}

if(StringUtils.isBlank(ExcelUtil.getCellValue(hrow.getCell(4)))){

falg4 = true;

}

if(StringUtils.isBlank(ExcelUtil.getCellValue(hrow.getCell(5)))){

falg5 = true;

}

if("0".equals(product_type)){//团意险有第六列(职业代码)

if(StringUtils.isBlank(ExcelUtil.getCellValue(hrow.getCell(6)))){

falg6 = true;

if (falg1 && falg2 && falg3 && falg4 && falg5 && falg6) {

continue;

}

}else{

InsuranceJobCode = ExcelUtil.getCellValue(hrow.getCell(6));

}

}else {

if (falg1 && falg2 && falg3 && falg4 && falg5) {

continue;

}

}

if(falg1 || falg2 || falg3 || falg4 || falg5 || falg6){

logger.info("上传文件被保险人清单中数据不完整,产品类型:"+product_type+"(0:团意险,1:交通险)");

resMap.put("Flag", "N#文件:被保险人清单,第"+(j+1)+"行被保险人数据不完整!");

PubFun.FlagMap.put(pathname, resMap);

return;

}

//被保险人数据,姓名,证件类型,证件号码,出生日期,性别

String InsureName = ExcelUtil.getCellValue(hrow.getCell(1));

String CertificateType = ExcelUtil.getCellValue(hrow.getCell(2));

String CertificateCode = ExcelUtil.getCellValue(hrow.getCell(3));

String Birthday = ExcelUtil.getCellValue(hrow.getCell(4));

String Sex = ExcelUtil.getCellValue(hrow.getCell(5));

if("0".equals(product_type)){//团意险

if(!jobcode.equals(InsuranceJobCode)){

logger.info("上传文件被保险人清单中职业编码不正确");

resMap.put("Flag", "N#文件:被保险人清单,"+InsureName+"的职业编码不正确!请核查后重新上传!");

PubFun.FlagMap.put(pathname, resMap);

return;

}

}

if(!Pattern.matches(nameRegex, InsureName)){//校验姓名

logger.info("上传文件被保险人清单中被保险人姓名格式不正确");

resMap.put("Flag", "N#表格第"+(j+1)+"行的被保险人姓名:"+InsureName+"不规范!被保险人姓名只能由中文或英文字母(半角格式)组成,最多为50个字符!");

PubFun.FlagMap.put(pathname, resMap);

return;

}

if(StringUtils.isBlank(CertificateType)){//校验证件类型

logger.info("上传文件被保险人清单中被保险人证件类型为空");

resMap.put("Flag", "N#表格第"+(j+1)+"行的被保险人证件类型不能为空!");

PubFun.FlagMap.put(pathname, resMap);

return;

}else{

if("身份证".equals(CertificateType)){

if(!Pattern.matches(dateRegex, Birthday)){

logger.info("被保险人日期格式不正确");

resMap.put("Flag", "N#表格第"+(j+1)+"行的被保险人日期格式不正确(yyyy-MM-dd或yyyy/MM/dd)!");

PubFun.FlagMap.put(pathname, resMap);

return;

}

String sexString = "";

if("男".equals(Sex)){

sexString = "1";

}else if("女".equals(Sex)){

sexString = "2";

}

String message = CommonUtil.IDCardValidate(CertificateCode, DateUtil.convertStringToDate(Birthday.replace("/", "-"), DateUtil.YYYY_MM_DD), sexString);

if(!StringUtils.isBlank(message)){

logger.info("上传文件被保险人清单中被保险人证件号码不正确");

resMap.put("Flag", "N#表格第"+(j+1)+"行的被保险人"+message);

PubFun.FlagMap.put(pathname, resMap);

return;

}

} else {//护照或其他

if(CertificateCode.length() < 3){

logger.info("上传文件被保险人清单中被保险人证件号码不正确");

resMap.put("Flag", "N#表格第"+(j+1)+"行的被保险人证件号码不规范!证件号码不能为空,不能重复,护照或者其他证件至少为三个字符!");

PubFun.FlagMap.put(pathname, resMap);

return;

}

}

}

dateMap.put("InsureName", InsureName);//被保险人姓名

dateMap.put("CertificateType", CertificateType);//证件类型

dateMap.put("CertificateCode", CertificateCode);//证件号码

dateMap.put("Birthday", Birthday);//出生日期

dateMap.put("Sex", Sex);//性别

dateMap.put("InsuranceJobCode", InsuranceJobCode);//职业编码

list.add(dateMap);

pronum++;//参保人数

idCardList.add(CertificateCode);//判断证件号码是否有重复

}

if(!checkRepeat(idCardList)){//判断证件号码是否有重复

logger.info("被保险人清单:证件号码有重复");

resMap.put("Flag", "N#表格被保险人清单,证件号码不能重复!");

PubFun.FlagMap.put(pathname, resMap);

return;

}

if(pronum<5){

logger.info("被保险人最低为5人");

resMap.put("Flag", "N#被保险人最低为5人");

PubFun.FlagMap.put(pathname, resMap);

return;

}

if("0".equals(product_type)){//团意险

//规模调整系数

double coefficient = 1.0;

if(pronum > 100){

coefficient = 0.8;

}else if(31 < pronum && pronum <=100){

coefficient = 0.9;

}

Double ylPream = 0.00;//原保费

int yfPream = 0;//应付保费

for(int x=0;x<pArray.length;x++){

Double yl = Double.parseDouble(pArray[x]);

ylPream += yl;

yfPream +=(new java.math.BigDecimal(yl*coefficient).setScale(0,java.math.BigDecimal.ROUND_HALF_UP).intValue());

}

Double yl_pream = (ylPream*pronum); // 原保费

int yf_pream = (yfPream*pronum); //应付保费

int pjPream = (yf_pream/pronum); //人均保费

retMap.put("PJ_PREAM", pjPream);//人均保费

retMap.put("YL_PREAM", yl_pream);//原保费

retMap.put("YF_PREAM", yf_pream);//应付保费

retMap.put("PNUM", pronum);//投保人数

retMap.put("BeneficiaryBase", list);//被保险人集合

}else if("1".equals(product_type)){//交通险

String jtPream = pArray[0];

Double jtp = Double.parseDouble(jtPream);

Double jtpicture = jtp*pronum;

retMap.put("PJ_PREAM", jtp);//人均保费

retMap.put("YL_PREAM", jtpicture);//原保费

retMap.put("YF_PREAM", jtpicture);//应付保费

retMap.put("PNUM", pronum);//投保人数

retMap.put("BeneficiaryBase", list);//被保险人集合

}

logger.info(bendifilename+"被保险人清单内容正确");

}else{

logger.info(bendifilename+"未填写投保人信息");

resMap.put("Flag", "N#文件:被保险人清单,请填写被保险人信息");

PubFun.FlagMap.put(pathname, resMap);

return;

}

}else {

logger.info("文件类型不正确!");

resMap.put("Flag", "N#文件:被保险人清单,请上传被保险人清单,格式为.xls或.xlsx");

PubFun.FlagMap.put(pathname, resMap);

return;

}

} catch (IOException e) {

// TODO Auto-generated catch block

logger.info(e.getMessage());

resMap.put("Flag", "N#系统繁忙,请稍后再试!");

return;

} catch (ParseException e) {

// TODO Auto-generated catch block

logger.info(e.getMessage());

resMap.put("Flag", "N#系统繁忙,请稍后再试!");

return;

}

}

// 判断数组中是否有重复值

public static boolean checkRepeat(List<String> idCardList) {

Set<String> set = new HashSet<String>();

for (String str : idCardList) {

set.add(str);

}

if (set.size() != idCardList.size()) {

return false;// 有重复

} else {

return true;// 不重复

}

}

}

package com.sinosoft.util;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.math.BigDecimal;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.sinosoft.entity.icar.Icaractivitymember;

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.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.hssf.util.HSSFColor;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.CellStyle;

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.ss.util.CellRangeAddress;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.type.TypeReference;

import com.fasterxml.jackson.databind.ObjectMapper;

/**

* Excel文件操作工具类,包括读、写、合并等功能

*

* @author : zh

* @Version : 1.00

*/

@Component

public class ExcelUtil {

//%%%%%%%%-------常量部分 开始----------%%%%%%%%%

/**

* 默认的开始读取的行位置为第一行(索引值为0)

*/

private final static int READ_START_POS = 1;

/**

* 默认结束读取的行位置为最后一行(索引值=0,用负数来表示倒数第n行)

*/

private final static int READ_END_POS = 0;

/**

* 默认Excel内容的开始比较列位置为第一列(索引值为0)

*/

private final static int COMPARE_POS = 0;

/**

* 默认多文件合并的时需要做内容比较(相同的内容不重复出现)

*/

private final static boolean NEED_COMPARE = true;

/**

* 默认多文件合并的新文件遇到名称重复时,进行覆盖

*/

private final static boolean NEED_OVERWRITE = true;

/**

* 默认只操作一个sheet

*/

private final static boolean ONLY_ONE_SHEET = false;

/**

* 默认读取第一个sheet中(只有当ONLY_ONE_SHEET = true时有效)

*/

private final static int SELECTED_SHEET = 0;

/**

* 默认从第一个sheet开始读取(索引值为0)

*/

private final static int READ_START_SHEET= 0;

/**

* 默认在最后一个sheet结束读取(索引值=0,用负数来表示倒数第n行)

*/

private final static int READ_END_SHEET = 0;

/**

* 默认打印各种信息

*/

private final static boolean PRINT_MSG = true;

//%%%%%%%%-------常量部分 结束----------%%%%%%%%%

//%%%%%%%%-------字段部分 开始----------%%%%%%%%%

/**

* Excel文件路径

*/

private String excelPath = "data.xlsx";

/**

* 设定开始读取的位置,默认为0

*/

private int startReadPos = READ_START_POS;

/**

* 设定结束读取的位置,默认为0,用负数来表示倒数第n行

*/

private int endReadPos = READ_END_POS;

/**

* 设定开始比较的列位置,默认为0

*/

private int comparePos = COMPARE_POS;

/**

* 设定汇总的文件是否需要替换,默认为true

*/

private boolean isOverWrite = NEED_OVERWRITE;

/**

* 设定是否需要比较,默认为true(仅当不覆写目标内容是有效,即isOverWrite=false时有效)

*/

private boolean isNeedCompare = NEED_COMPARE;

/**

* 设定是否只操作第一个sheet

*/

private boolean onlyReadOneSheet = ONLY_ONE_SHEET;

/**

* 设定操作的sheet在索引值

*/

private int selectedSheetIdx =SELECTED_SHEET;

/**

* 设定操作的sheet的名称

*/

private String selectedSheetName = "";

/**

* 设定开始读取的sheet,默认为0

*/

private int startSheetIdx = READ_START_SHEET;

/**

* 设定结束读取的sheet,默认为0,用负数来表示倒数第n行

*/

private int endSheetIdx = READ_END_SHEET;

/**

* 设定是否打印消息

*/

private boolean printMsg = PRINT_MSG;

//%%%%%%%%-------字段部分 结束----------%%%%%%%%%

@SuppressWarnings("static-access")

public static void main(String[] args) {

try {

ExcelUtil eu = new ExcelUtil();

/*

eu.setExcelPath("d:\\关于印发偿二代季度报告有关编报基础文件的通知 附件1(1).保险公司偿付能力季度报告EXCEL样表(2015-12 (版本 1).xlsm");

*/

eu.setExcelPath("F:\\QIDANDAN\\soft\\imovemanageicaricar\\src\\main\\webapp\\upload\\LI-SPTO-齐丹丹-个人周报20170421.xls");

/* System.out.println("=======测试Excel 默认 读取========");

eu.readExcel(); */

/*System.out.println("\n=======测试Excel 从第四行读取,倒数第二行结束========");

eu = eu.RestoreSettings();//还原设定

eu.setStartReadPos(3);

eu.setEndReadPos(0); */

List<Row> list=eu.readExcel();

System.out.println(list.get(0).getCell(0));

System.out.println(list.get(0).getCell(6));

System.out.println(list.get(0).getCell(7));

System.out.println(eu.getCellValue(list.get(1).getCell(1)));

/* System.out.println("\n=======测试Excel 读取第二个sheet========");

eu = eu.RestoreSettings();//还原设定

eu.setSelectedSheetIdx(1);

eu.readExcel();

System.out.println("\n=======测试Excel 读取所有的sheet========");

eu = eu.RestoreSettings();//还原设定

eu.setOnlyReadOneSheet(false);

eu.readExcel();

List<Row> list1=new ArrayList<Row>();

Row r=null;*/

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public ExcelUtil(){

}

public ExcelUtil(String excelPath){

this.excelPath = excelPath;

}

/**

* 还原设定(其实是重新new一个新的对象并返回)

* @return

*/

public ExcelUtil RestoreSettings(){

ExcelUtil instance = new ExcelUtil(this.excelPath);

return instance;

}

/**

* 自动根据文件扩展名,调用对应的读取方法

*

* @Title: writeExcel

* @Date : 2014-9-11 下午01:50:38

* @param xlsPath

* @throws IOException

*/

public List<Row> readExcel() throws IOException{

return readExcel(this.excelPath);

}

/**

* 自动根据文件扩展名,调用对应的读取方法

*

* @Title: writeExcel

* @param xlsPath

* @throws IOException

*/

public List<Row> readExcel(String xlsPath) throws IOException{

//扩展名为空时,

if (xlsPath.equals("")){

throw new IOException("文件路径不能为空!");

}else{

File file = new File(xlsPath);

if(!file.exists()){

throw new IOException("文件不存在!");

}

}

//获取扩展名

String ext = xlsPath.substring(xlsPath.lastIndexOf(".")+1);

try {

if("xls".equals(ext)){ //使用xls方式读取

return readExcel_xls(xlsPath);

}else if("xlsx".equals(ext)){ //使用xlsx方式读取

return readExcel_xlsx(xlsPath);

}else if("xlsm".equals(ext)){ //使用xlsx方式读取

return readExcel_xlsx(xlsPath);

}else{ //依次尝试xls、xlsx方式读取

out("您要操作的文件没有扩展名,正在尝试以xls方式读取...");

try{

return readExcel_xls(xlsPath);

} catch (IOException e1) {

out("尝试以xls方式读取,结果失败!,正在尝试以xlsx方式读取...");

try{

return readExcel_xlsx(xlsPath);

} catch (IOException e2) {

out("尝试以xls方式读取,结果失败!\n请您确保您的文件是Excel文件,并且无损,然后再试。");

throw e2;

}

}

}

} catch (IOException e) {

throw e;

}

}

/**

* 自动根据文件扩展名,调用对应的写入方法

*

* @Title: writeExcel

* @param rowList

* @throws IOException

*/

public void writeExcel(List<Row> rowList) throws IOException{

writeExcel(rowList,excelPath);

}

/**

* 自动根据文件扩展名,调用对应的写入方法

*

* @Title: writeExcel

* @param rowList

* @param xlsPath

* @throws IOException

*/

public void writeExcel(List<Row> rowList, String xlsPath) throws IOException {

//扩展名为空时,

if (xlsPath.equals("")){

throw new IOException("文件路径不能为空!");

}

//获取扩展名

String ext = xlsPath.substring(xlsPath.lastIndexOf(".")+1);

try {

if("xls".equals(ext)){ //使用xls方式写入

writeExcel_xls(rowList,xlsPath);

}else if("xlsx".equals(ext)){ //使用xlsx方式写入

writeExcel_xlsx(rowList,xlsPath);

}else{ //依次尝试xls、xlsx方式写入

out("您要操作的文件没有扩展名,正在尝试以xls方式写入...");

try{

writeExcel_xls(rowList,xlsPath);

} catch (IOException e1) {

out("尝试以xls方式写入,结果失败!,正在尝试以xlsx方式读取...");

try{

writeExcel_xlsx(rowList,xlsPath);

} catch (IOException e2) {

out("尝试以xls方式写入,结果失败!\n请您确保您的文件是Excel文件,并且无损,然后再试。");

throw e2;

}

}

}

} catch (IOException e) {

throw e;

}

}

/**

* 修改Excel(97-03版,xls格式)

*

* @Title: writeExcel_xls

* @param rowList

* @param dist_xlsPath

* @throws IOException

*/

public void writeExcel_xls(List<Row> rowList, String dist_xlsPath) throws IOException {

writeExcel_xls(rowList, excelPath,dist_xlsPath);

}

/**

* 修改Excel(97-03版,xls格式)

*

* @Title: writeExcel_xls

* @param rowList

* @param src_xlsPath

* @param dist_xlsPath

* @throws IOException

*/

public void writeExcel_xls(List<Row> rowList, String src_xlsPath, String dist_xlsPath) throws IOException {

// 判断文件路径是否为空

if (dist_xlsPath == null || dist_xlsPath.equals("")) {

out("文件路径不能为空");

throw new IOException("文件路径不能为空");

}

// 判断文件路径是否为空

if (src_xlsPath == null || src_xlsPath.equals("")) {

out("文件路径不能为空");

throw new IOException("文件路径不能为空");

}

// 判断列表是否有数据,如果没有数据,则返回

if (rowList == null || rowList.size() == 0) {

out("文档为空");

return;

}

try {

HSSFWorkbook wb = null;

// 判断文件是否存在

File file = new File(dist_xlsPath);

if (file.exists()) {

// 如果复写,则删除后

if (isOverWrite) {

file.delete();

// 如果文件不存在,则创建一个新的Excel

// wb = new HSSFWorkbook();

// wb.createSheet("Sheet1");

wb = new HSSFWorkbook(new FileInputStream(src_xlsPath));

} else {

// 如果文件存在,则读取Excel

wb = new HSSFWorkbook(new FileInputStream(file));

}

} else {

// 如果文件不存在,则创建一个新的Excel

// wb = new HSSFWorkbook();

// wb.createSheet("Sheet1");

wb = new HSSFWorkbook(new FileInputStream(src_xlsPath));

}

// 将rowlist的内容写到Excel中

writeExcel(wb, rowList, dist_xlsPath);

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 修改Excel(97-03版,xls格式)

*

* @Title: writeExcel_xls

* @param rowList

* @param dist_xlsPath

* @throws IOException

*/

public void writeExcel_xlsx(List<Row> rowList, String dist_xlsPath) throws IOException {

writeExcel_xls(rowList, excelPath , dist_xlsPath);

}

/**

* 修改Excel(2007版,xlsx格式)

*

* @Title: writeExcel_xlsx

* @param rowList

* @param xlsPath

* @throws IOException

*/

public void writeExcel_xlsx(List<Row> rowList, String src_xlsPath, String dist_xlsPath) throws IOException {

// 判断文件路径是否为空

if (dist_xlsPath == null || dist_xlsPath.equals("")) {

out("文件路径不能为空");

throw new IOException("文件路径不能为空");

}

// 判断文件路径是否为空

if (src_xlsPath == null || src_xlsPath.equals("")) {

out("文件路径不能为空");

throw new IOException("文件路径不能为空");

}

// 判断列表是否有数据,如果没有数据,则返回

if (rowList == null || rowList.size() == 0) {

out("文档为空");

return;

}

try {

// 读取文档

XSSFWorkbook wb = null;

// 判断文件是否存在

File file = new File(dist_xlsPath);

if (file.exists()) {

// 如果复写,则删除后

if (isOverWrite) {

file.delete();

// 如果文件不存在,则创建一个新的Excel

// wb = new XSSFWorkbook();

// wb.createSheet("Sheet1");

wb = new XSSFWorkbook(new FileInputStream(src_xlsPath));

} else {

// 如果文件存在,则读取Excel

wb = new XSSFWorkbook(new FileInputStream(file));

}

} else {

// 如果文件不存在,则创建一个新的Excel

// wb = new XSSFWorkbook();

// wb.createSheet("Sheet1");

wb = new XSSFWorkbook(new FileInputStream(src_xlsPath));

}

// 将rowlist的内容添加到Excel中

writeExcel(wb, rowList, dist_xlsPath);

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* //读取Excel 2007版,xlsx格式

*

* @Title: readExcel_xlsx

* @return

* @throws IOException

*/

public List<Row> readExcel_xlsx() throws IOException {

return readExcel_xlsx(excelPath);

}

/**

* //读取Excel 2007版,xlsx格式

*

* @Title: readExcel_xlsx

* @return

* @throws Exception

*/

public List<Row> readExcel_xlsx(String xlsPath) throws IOException {

// 判断文件是否存在

File file = new File(xlsPath);

if (!file.exists()) {

throw new IOException("文件名为" + file.getName() + "Excel文件不存在!");

}

XSSFWorkbook wb = null;

List<Row> rowList = new ArrayList<Row>();

try {

FileInputStream fis = new FileInputStream(file);

// 去读Excel

wb = new XSSFWorkbook(fis);

// 读取Excel 2007版,xlsx格式

rowList = readExcel(wb);

} catch (IOException e) {

e.printStackTrace();

}

return rowList;

}

/***

* 读取Excel(97-03版,xls格式)

*

* @throws IOException

*

* @Title: readExcel

*/

public List<Row> readExcel_xls() throws IOException {

return readExcel_xls(excelPath);

}

/***

* 读取Excel(97-03版,xls格式)

*

* @throws Exception

*

* @Title: readExcel

*/

public List<Row> readExcel_xls(String xlsPath) throws IOException {

// 判断文件是否存在

File file = new File(xlsPath);

if (!file.exists()) {

throw new IOException("文件名为" + file.getName() + "Excel文件不存在!");

}

HSSFWorkbook wb = null;// 用于Workbook级的操作,创建、删除Excel

List<Row> rowList = new ArrayList<Row>();

try {

// 读取Excel

wb = new HSSFWorkbook(new FileInputStream(file));

// 读取Excel 97-03版,xls格式

rowList = readExcel(wb);

} catch (IOException e) {

e.printStackTrace();

}

return rowList;

}

/***

* 读取单元格的值

*

* @Title: getCellValue

* @param cell

* @return

*/

public static String getCellValue(Cell cell) {

Object result = "";

if (cell != null) {

switch (cell.getCellType()) {

case Cell.CELL_TYPE_STRING:

result = cell.getStringCellValue();

break;

case Cell.CELL_TYPE_NUMERIC:

result = double2String(cell.getNumericCellValue());

break;

case Cell.CELL_TYPE_BOOLEAN:

result = cell.getBooleanCellValue();

break;

case Cell.CELL_TYPE_FORMULA:

//result = cell.getCellFormula();

int type=cell.getCachedFormulaResultType();

if(type==0){

result = double2String(cell.getNumericCellValue());

}else if(type==1){

result = cell.getStringCellValue();

}else if(type==5){

result = cell.getErrorCellValue();

}

//result = String.valueOf(cell.getNumericCellValue());

break;

case Cell.CELL_TYPE_ERROR:

result = cell.getErrorCellValue();

break;

case Cell.CELL_TYPE_BLANK:

break;

default:

break;

}

}

return result.toString();

}

/**

* 科学计数法问题解决

* @param test

* @return

*/

public static String double2String(double test){

BigDecimal bdTest = null;

if (test % 1 ==0 ){

long i = new Double(test).longValue();

bdTest = new BigDecimal(String.valueOf(i));

}else{

bdTest = new BigDecimal(String.valueOf(test));

}

///System.out.println(String.valueOf(test) + "=" + bdTest.toString());

return bdTest.toString();

}

/**

* 通用读取Excel

*

* @Title: readExcel

* @param wb

* @return

*/

private List<Row> readExcel(Workbook wb) {

List<Row> rowList = new ArrayList<Row>();

int sheetCount = 3;//需要操作的sheet数量

Sheet sheet = null;

if(onlyReadOneSheet){ //只操作一个sheet

// 获取设定操作的sheet(如果设定了名称,按名称查,否则按索引值查)

sheet =selectedSheetName.equals("")? wb.getSheetAt(selectedSheetIdx):wb.getSheet(selectedSheetName);

}else{ //操作多个sheet

sheetCount = wb.getNumberOfSheets();//获取可以操作的总数量

}

// 获取sheet数目

for(int t=startSheetIdx; t<sheetCount+endSheetIdx;t++){

// 获取设定操作的sheet

if(!onlyReadOneSheet) {

sheet =wb.getSheetAt(t);

}

//获取最后行号

int lastRowNum = sheet.getLastRowNum();

if(lastRowNum>0){ //如果>0,表示有数据

out("\n开始读取名为【"+sheet.getSheetName()+"】的内容:");

}

Row row = null;

// 循环读取

for (int i = startReadPos; i <= lastRowNum + endReadPos; i++) {

row = sheet.getRow(i);

if (row != null) {

rowList.add(row);

/*out("第"+(i+1)+"行:",false);

// 获取每一单元格的值

for (int j = 0; j < row.getLastCellNum(); j++) {

String value = getCellValue(row.getCell(j));

//row.getCell(j).setCellValue(value);

if (!value.equals("")) {

out(value + " | ",false);

}

}

out(""); */

}

}

}

return rowList;

}

/**

* 修改Excel,并另存为

*

* @Title: WriteExcel

* @param wb

* @param rowList

* @param xlsPath

*/

private void writeExcel(Workbook wb, List<Row> rowList, String xlsPath) {

if (wb == null) {

out("操作文档不能为空!");

return;

}

Sheet sheet = wb.getSheetAt(0);// 修改第一个sheet中的值

// 如果每次重写,那么则从开始读取的位置写,否则果获取源文件最新的行。

int lastRowNum = isOverWrite ? startReadPos : sheet.getLastRowNum() + 1;

int t = 0;//记录最新添加的行数

out("要添加的数据总条数为:"+rowList.size());

for (Row row : rowList) {

if (row == null) continue;

// 判断是否已经存在该数据

int pos = findInExcel(sheet, row);

Row r = null;// 如果数据行已经存在,则获取后重写,否则自动创建新行。

if (pos >= 0) {

sheet.removeRow(sheet.getRow(pos));

r = sheet.createRow(pos);

} else {

r = sheet.createRow(lastRowNum + t++);

}

//用于设定单元格样式

CellStyle newstyle = wb.createCellStyle();

//循环为新行创建单元格

for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {

Cell cell = r.createCell(i);// 获取数据类型

cell.setCellValue(getCellValue(row.getCell(i)));// 复制单元格的值到新的单元格

// cell.setCellStyle(row.getCell(i).getCellStyle());//出错

if (row.getCell(i) == null) continue;

copyCellStyle(row.getCell(i).getCellStyle(), newstyle); // 获取原来的单元格样式

cell.setCellStyle(newstyle);// 设置样式

// sheet.autoSizeColumn(i);//自动跳转列宽度

}

}

out("其中检测到重复条数为:" + (rowList.size() - t) + " ,追加条数为:"+t);

// 统一设定合并单元格

setMergedRegion(sheet);

try {

// 重新将数据写入Excel中

FileOutputStream outputStream = new FileOutputStream(xlsPath);

wb.write(outputStream);

outputStream.flush();

outputStream.close();

} catch (Exception e) {

out("写入Excel时发生错误! ");

e.printStackTrace();

}

}

/**

* 查找某行数据是否在Excel表中存在,返回行数。

*

* @Title: findInExcel

* @param sheet

* @param row

* @return

*/

private int findInExcel(Sheet sheet, Row row) {

int pos = -1;

try {

// 如果覆写目标文件,或者不需要比较,则直接返回

if (isOverWrite || !isNeedCompare) {

return pos;

}

for (int i = startReadPos; i <= sheet.getLastRowNum() + endReadPos; i++) {

Row r = sheet.getRow(i);

if (r != null && row != null) {

String v1 = getCellValue(r.getCell(comparePos));

String v2 = getCellValue(row.getCell(comparePos));

if (v1.equals(v2)) {

pos = i;

break;

}

}

}

} catch (Exception e) {

e.printStackTrace();

}

return pos;

}

/**

* 复制一个单元格样式到目的单元格样式

*

* @param fromStyle

* @param toStyle

*/

public static void copyCellStyle(CellStyle fromStyle, CellStyle toStyle) {

toStyle.setAlignment(fromStyle.getAlignment());

// 边框和边框颜色

toStyle.setBorderBottom(fromStyle.getBorderBottom());

toStyle.setBorderLeft(fromStyle.getBorderLeft());

toStyle.setBorderRight(fromStyle.getBorderRight());

toStyle.setBorderTop(fromStyle.getBorderTop());

toStyle.setTopBorderColor(fromStyle.getTopBorderColor());

toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor());

toStyle.setRightBorderColor(fromStyle.getRightBorderColor());

toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor());

// 背景和前景

toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor());

toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor());

// 数据格式

toStyle.setDataFormat(fromStyle.getDataFormat());

toStyle.setFillPattern(fromStyle.getFillPattern());

// toStyle.setFont(fromStyle.getFont(null));

toStyle.setHidden(fromStyle.getHidden());

toStyle.setIndention(fromStyle.getIndention());// 首行缩进

toStyle.setLocked(fromStyle.getLocked());

toStyle.setRotation(fromStyle.getRotation());// 旋转

toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment());

toStyle.setWrapText(fromStyle.getWrapText());

}

/**

* 获取合并单元格的值

*

* @param sheet

* @param row

* @param column

* @return

*/

public void setMergedRegion(Sheet sheet) {

int sheetMergeCount = sheet.getNumMergedRegions();

for (int i = 0; i < sheetMergeCount; i++) {

// 获取合并单元格位置

CellRangeAddress ca = sheet.getMergedRegion(i);

int firstRow = ca.getFirstRow();

if (startReadPos - 1 > firstRow) {// 如果第一个合并单元格格式在正式数据的上面,则跳过。

continue;

}

int lastRow = ca.getLastRow();

int mergeRows = lastRow - firstRow;// 合并的行数

int firstColumn = ca.getFirstColumn();

int lastColumn = ca.getLastColumn();

// 根据合并的单元格位置和大小,调整所有的数据行格式,

for (int j = lastRow + 1; j <= sheet.getLastRowNum(); j++) {

// 设定合并单元格

sheet.addMergedRegion(new CellRangeAddress(j, j + mergeRows, firstColumn, lastColumn));

j = j + mergeRows;// 跳过已合并的行

}

}

}

/**

* 打印消息,

* @param msg 消息内容

* @param tr 换行

*/

private void out(String msg){

if(printMsg){

out(msg,true);

}

}

/**

* 导出excel工具类

* @param request

* @param response

* @param name

* @param cols

* @param datas

*/

public void export(HttpServletRequest request, HttpServletResponse response, String name, String cols, String datas) {

if(name == null || cols == null || datas == null){

return;

}

try {

List<Map<String, Object>> colList = new ObjectMapper().readValue(cols.replaceAll("&quot;", "\""), new TypeReference<List<Map<String, Object>>>() {});

List<Map<String, Object>> dataList = new ObjectMapper().readValue(datas.replaceAll("&quot;", "\""), new TypeReference<List<Map<String, Object>>>() {});

@SuppressWarnings("resource")

HSSFWorkbook wb = new HSSFWorkbook();

HSSFSheet sheet = wb.createSheet(name);

HSSFCellStyle titleStyle = wb.createCellStyle();

titleStyle.setFillForegroundColor(HSSFColor.DARK_TEAL.index);//颜色

titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);

titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);

titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);

titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);

titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中

HSSFFont font = wb.createFont();

font.setColor(HSSFColor.WHITE.index);

font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

titleStyle.setFont(font);

HSSFCellStyle headStyle = wb.createCellStyle();

headStyle.setFillForegroundColor(HSSFColor.WHITE.index);

headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

headStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);

headStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);

headStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);

headStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);

headStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中

HSSFFont fontHead = wb.createFont();

fontHead.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//字体样式

fontHead.setFontHeightInPoints((short) 12);//设置字体大小

headStyle.setFont(fontHead);

HSSFCellStyle oddStyle = wb.createCellStyle();

oddStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);

oddStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);

oddStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);

oddStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);

oddStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

HSSFCellStyle evenStyle = wb.createCellStyle();

evenStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);

evenStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

evenStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);

evenStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);

evenStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);

evenStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);

evenStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

//合并第一行单元格

HSSFRow rowOne = sheet.createRow(0);

rowOne.setHeight((short) 470);

sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, colList.size()-1));

HSSFCell cell;

cell = rowOne.createCell(0);

cell.setCellStyle(headStyle);

cell.setCellValue(name);

//增加最后第一行合并前最后一个cell的边框

cell = rowOne.createCell(colList.size()-1);

cell.setCellStyle(headStyle);

HSSFRow row = sheet.createRow(1);

row.setHeight((short) 320);

Map<String, Object> col, data;

for (int i = 0; i < colList.size(); i++) {

col = colList.get(i);

cell = row.createCell(i);

cell.setCellStyle(titleStyle);

cell.setCellValue(col.get("title").toString());

// int a=(int) (Integer.parseInt(col.get("width").toString()) * 40);

byte[] b=col.get("title").toString().getBytes("gbk");

//设置execl列宽为标题的2倍

int d=b.length * 256 * 2;

if(b.length>=255){

d=255*256;

}

sheet.setColumnWidth(i, d);

}

Object value;

for (int i = 0; i < dataList.size(); i++) {

data = dataList.get(i);

row = sheet.createRow(i + 2);

row.setHeight((short) 320);

for (int j = 0; j < colList.size(); j++) {

col = colList.get(j);

cell = row.createCell(j);

cell.setCellStyle(i % 2 == 0 ? oddStyle : evenStyle);

value = data.get(col.get("field"));

if (value != null) {

if (value instanceof Double) {

cell.setCellValue(((Double) value).doubleValue());

} else if (value instanceof Integer) {

cell.setCellValue(((Integer) value).intValue());

} else {

cell.setCellValue(value.toString());

}

}

}

}

String fileName = name + ".xls";

fileName = new String(fileName.getBytes("GBK"), "iso8859-1");

response.reset();

response.setHeader("Content-Disposition","attachment;filename="+fileName);//指定下载的文件名

response.setContentType("application/vnd.ms-excel");

response.setHeader("Pragma", "no-cache");

response.setHeader("Cache-Control", "no-cache");

response.setDateHeader("Expires", 0);

BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());

wb.write(out);

out.flush();

out.close();

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 打印消息,

* @param msg 消息内容

* @param tr 换行

*/

private void out(String msg,boolean tr){

if(printMsg){

System.out.print(msg+(tr?"\n":""));

}

}

public String getExcelPath() {

return this.excelPath;

}

public void setExcelPath(String excelPath) {

this.excelPath = excelPath;

}

public boolean isNeedCompare() {

return isNeedCompare;

}

public void setNeedCompare(boolean isNeedCompare) {

this.isNeedCompare = isNeedCompare;

}

public int getComparePos() {

return comparePos;

}

public void setComparePos(int comparePos) {

this.comparePos = comparePos;

}

public int getStartReadPos() {

return startReadPos;

}

public void setStartReadPos(int startReadPos) {

this.startReadPos = startReadPos;

}

public int getEndReadPos() {

return endReadPos;

}

public void setEndReadPos(int endReadPos) {

this.endReadPos = endReadPos;

}

public boolean isOverWrite() {

return isOverWrite;

}

public void setOverWrite(boolean isOverWrite) {

this.isOverWrite = isOverWrite;

}

public boolean isOnlyReadOneSheet() {

return onlyReadOneSheet;

}

public void setOnlyReadOneSheet(boolean onlyReadOneSheet) {

this.onlyReadOneSheet = onlyReadOneSheet;

}

public int getSelectedSheetIdx() {

return selectedSheetIdx;

}

public void setSelectedSheetIdx(int selectedSheetIdx) {

this.selectedSheetIdx = selectedSheetIdx;

}

public String getSelectedSheetName() {

return selectedSheetName;

}

public void setSelectedSheetName(String selectedSheetName) {

this.selectedSheetName = selectedSheetName;

}

public int getStartSheetIdx() {

return startSheetIdx;

}

public void setStartSheetIdx(int startSheetIdx) {

this.startSheetIdx = startSheetIdx;

}

public int getEndSheetIdx() {

return endSheetIdx;

}

public void setEndSheetIdx(int endSheetIdx) {

this.endSheetIdx = endSheetIdx;

}

public boolean isPrintMsg() {

return printMsg;

}

public void setPrintMsg(boolean printMsg) {

this.printMsg = printMsg;

}

}

以上是 JAVA编码(61)—— 解析Excel文件 xls、xlsx 的全部内容, 来源链接: utcz.com/z/390734.html

回到顶部