java mvc poi导出excel_springMVC框架+POI组件导出Excel

public class PoiExcelExport{//excle导出名称

privateString fileName;//excel 表头

privateString[] heads;//excel 列

privateString[] cols;//设置数值型的列 从0开始计数

private int[] numerics;//list集合

private Listlist;//输出流

privateOutputStream out;//构造函数

public PoiExcelExport(String fileName, String[] heads, String[] cols, Listlist, OutputStream out) {this.fileName =fileName;this.heads =heads;this.cols =cols;this.list =list;this.out =out;

}//构造函数 带数字类型

public PoiExcelExport(String fileName, String[] heads, String[] cols, List list, int[] numerics, OutputStream out) {this.fileName =fileName;this.heads =heads;this.cols =cols;this.list =list;this.numerics =numerics;this.out =out;

}public voidexportExcel() {

HSSFWorkbook hssfworkbook= new HSSFWorkbook(); //创建一个excel对象

for (int i = 0; i <= (list.size() / 65535); i++) {

HSSFSheet hssfsheet= hssfworkbook.createSheet(); //工作表//工作表名称

hssfworkbook.setSheetName(i, fileName.replace(".xls", "") + "(第" + (i + 1) + "页)");int beginRows = 65535 *i;int endRows = (list.size() > 65535 * (i + 1)) ? 65535 * (i + 1) - 1 : list.size() - 1;

HSSFRow hssfrowHead= hssfsheet.createRow(0);//输出excel 表头

if (heads != null && heads.length > 0) {for (int h = 0; h < heads.length; h++) {

HSSFCell hssfcell=hssfrowHead.createCell(h,Cell.CELL_TYPE_STRING);

hssfcell.setCellValue(heads[h]);

}

}//要设置数值型 列表

HSSFCellStyle cellstyle =hssfworkbook.createCellStyle();

cellstyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("##0"));//是否是数值型

boolean isnum = false;//输出excel 数据

for (int curRow = beginRows; curRow <= endRows; curRow++) {//获取数据

BeanToMap btm = new BeanToMap();

Map hm =btm.getMap(list.get(curRow));//创建excel行 表头1行 导致数据行数 延后一行

HSSFRow hssfrow = hssfsheet.createRow(curRow % 65535 + 1);//读取数据值

for (int k = 0; k < cols.length; k++) {

HSSFCell hssfcell=hssfrow.createCell(k);//hssfcell.setCellValue(hm.get(cols[k])==null?"":hm.get(cols[k]).toString());

isnum = false;for (int z = 0; z < numerics.length; z++) {if (numerics[z] ==k) {

isnum= true;break;

}

}if(isnum) {if (hm.get(cols[k]) == null || hm.get(cols[k]).equals("")) {

}else{

hssfcell.setCellStyle(cellstyle);

hssfcell.setCellValue(Double.parseDouble(

hm.get(cols[k])== null ? "" : hm.get(cols[k]).toString().replace(",", "")));

}

}else{

hssfcell.setCellValue(hm.get(cols[k])== null ? "": hm.get(cols[k]).toString());

}

}

}

}//excel生成完毕,写到输出流

try{

hssfworkbook.write(out);

out.flush();

out.close();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值