XSSF:xlsx
HSSF:xls
HSSF是POI工程对Excel 97(-2007)文件操作的纯Java实现
XSSF是POI工程对Excel 2007 OOXML (.xlsx)文件操作的纯Java实现
我之前是导出xls版本的,后来改成xlsx版本的,想切换版本就把全文的HSSF和XSSF替换了就可以了,可能会有极个别的方法不适用,自行百度一下就可以了

package com.wttech.tfjd.utils;import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;import javax.servlet.http.HttpServletResponse;import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
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.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFDataFormat;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import com.wttech.tfjd.model.assess.MonthBalance;
import com.wttech.tfjd.model.assess.PointHistory;
import com.wttech.tfjd.model.assess.SalaryHistory;
import com.wttech.tfjd.model.assess.StatisticalHistory;public class DownPOIUtils {/*** * @param response:响应对象,类型是HttpServletResponse* @param map:要封装的信息的map容器,其中key为Student,value为String类型的,在这里代表分数* @throws Exception:代表异常对象*/public static void downPoi(HttpServletResponse response,MonthBalance monthBalance) throws Exception {String fname = "行政权力清单.xlsx";// Excel文件名OutputStream os = response.getOutputStream();// 取得输出流response.reset();// 清空输出流response.setHeader("Content-Disposition", "attachment;filename=" +URLEncoder.encode(fname, "UTF-8")); // 设定输出文件头,该方法有两个参数,分别表示应答头的名字和值。XSSF:xlsx    HSSF:xls  中文要用encode处理一下response.setContentType("application/msexcel");response.setContentType("text/html; charset=UTF-8"); //设置编码字符try {new DownPOIUtils().new POIS().createFixationSheet1(os, monthBalance);} catch (Exception e) {e.printStackTrace();}}class POIS {public void createFixationSheet1(OutputStream os,MonthBalance monthBalance) throws Exception {List<PointHistory> pointHistoryList = monthBalance.getPointHistoryList();List<StatisticalHistory> statisticalHistoryList = monthBalance.getStatisticalHistoryList();List<SalaryHistory> salaryHistoryList = monthBalance.getSalaryHistoryList();// 创建工作薄XSSFWorkbook wb = new XSSFWorkbook();// sheet1XSSFSheet sheet1 = wb.createSheet();//固定绩效int gdjx = 0;//考核绩效BigDecimal khjx = new BigDecimal(0);//应发绩效BigDecimal yfjx = new BigDecimal(0);//设置列宽sheet1.setColumnWidth(2, 4000); sheet1.setColumnWidth(3, 6000); XSSFRow sheet1row1 = sheet1.createRow((short) 0);sheet1.createFreezePane(0, 1);cteateCell(wb, sheet1row1, (short) 0, "序号");cteateCell(wb, sheet1row1, (short) 1, "姓名");cteateCell(wb, sheet1row1, (short) 2, "工作岗位");cteateCell(wb, sheet1row1, (short) 3, "身份证号码");cteateCell(wb, sheet1row1, (short) 4, "固定绩效");cteateCell(wb, sheet1row1, (short) 5, "考核绩效");cteateCell(wb, sheet1row1, (short) 6, "应发绩效");cteateCell(wb, sheet1row1, (short) 7, "备注");for(int i =1;i <= salaryHistoryList.size();i++){XSSFRow sheet1rowi = sheet1.createRow((short) i);cteateCell(wb, sheet1rowi, (short) 0, String.valueOf(i));cteateCell(wb, sheet1rowi, (short) 1, salaryHistoryList.get(i-1).getUser().getUsername());cteateCell(wb, sheet1rowi, (short) 2, salaryHistoryList.get(i-1).getJob());cteateCell(wb, sheet1rowi, (short) 3, salaryHistoryList.get(i-1).getIdnumber());cteateNumberCell(wb, sheet1rowi, (short) 4, salaryHistoryList.get(i-1).getFixedperformance());cteateNumberCell(wb, sheet1rowi, (short) 5, salaryHistoryList.get(i-1).getKhjx().toString());cteateNumberCell(wb, sheet1rowi, (short) 6, salaryHistoryList.get(i-1).getYfjx().toString());cteateCell(wb, sheet1rowi, (short) 7, "");gdjx += Integer.valueOf(salaryHistoryList.get(i-1).getFixedperformance());khjx = khjx.add(salaryHistoryList.get(i-1).getKhjx());yfjx = yfjx.add(salaryHistoryList.get(i-1).getYfjx());}XSSFRow sheet1row2 = sheet1.createRow((short) (salaryHistoryList.size()+1));cteateNoborderCell(wb,sheet1row2,(short) 4,String.valueOf(gdjx));cteateNoborderCell(wb,sheet1row2,(short) 5,khjx.toString());cteateNoborderCell(wb,sheet1row2,(short) 6,yfjx.toString());XSSFRow sheet1row3 = sheet1.createRow((short) (salaryHistoryList.size()+2));cteateNoborderCell(wb,sheet1row3,(short) 0,"负责人:");cteateNoborderCell(wb,sheet1row3,(short) 1,"池福波");//sheet2XSSFSheet sheet2 = wb.createSheet();//维修总数合计int total = 0;BigDecimal gd = new BigDecimal(0);BigDecimal rc = new BigDecimal(0);sheet2.setColumnWidth(6, 4000); sheet2.setColumnWidth(7, 4000); sheet2.setColumnWidth(8, 4000); sheet2.setColumnWidth(9, 4000); sheet2.setColumnWidth(10, 4000); XSSFRow sheet2row1 = sheet2.createRow((short) 0);sheet2.createFreezePane(0, 1);cteateCell(wb, sheet2row1, (short) 0, "姓名");cteateCell(wb, sheet2row1, (short) 1, "简单");cteateCell(wb, sheet2row1, (short) 2, "一般");cteateCell(wb, sheet2row1, (short) 3, "较难");cteateCell(wb, sheet2row1, (short) 4, "困难");cteateCell(wb, sheet2row1, (short) 5, "重大");cteateCell(wb, sheet2row1, (short) 6, "维护总数合计");cteateCell(wb, sheet2row1, (short) 7, "维修量占比");cteateCell(wb, sheet2row1, (short) 8, "工单绩效工资");cteateCell(wb, sheet2row1, (short) 9, "日常绩效得分");cteateCell(wb, sheet2row1, (short) 10, "日常绩效工资");for(int i =1;i <= statisticalHistoryList.size();i++){XSSFRow sheet2rowi = sheet2.createRow((short) i);cteateCell(wb, sheet2rowi, (short) 0, statisticalHistoryList.get(i-1).getUser().getUsername());cteateCell(wb, sheet2rowi, (short) 1, statisticalHistoryList.get(i-1).getEasy()==null?"":statisticalHistoryList.get(i-1).getEasy().toString());cteateCell(wb, sheet2rowi, (short) 2, statisticalHistoryList.get(i-1).getNormal()==null?"":statisticalHistoryList.get(i-1).getNormal().toString());cteateCell(wb, sheet2rowi, (short) 3, statisticalHistoryList.get(i-1).getLessdifficult()==null?"":statisticalHistoryList.get(i-1).getLessdifficult().toString());cteateCell(wb, sheet2rowi, (short) 4, statisticalHistoryList.get(i-1).getDifficult()==null?"":statisticalHistoryList.get(i-1).getDifficult().toString());cteateCell(wb, sheet2rowi, (short) 5, statisticalHistoryList.get(i-1).getImportant()==null?"":statisticalHistoryList.get(i-1).getImportant().toString());cteateCell(wb, sheet2rowi, (short) 6, statisticalHistoryList.get(i-1).getTotal()==null?"":statisticalHistoryList.get(i-1).getTotal().toString());cteateCell(wb, sheet2rowi, (short) 7, statisticalHistoryList.get(i-1).getAccountedfor()==null?"":statisticalHistoryList.get(i-1).getAccountedfor());cteateNumberCell(wb, sheet2rowi, (short) 8, statisticalHistoryList.get(i-1).getGdwage().toString());cteateCell(wb, sheet2rowi, (short) 9, statisticalHistoryList.get(i-1).getScore().toString());cteateNumberCell(wb, sheet2rowi, (short) 10, statisticalHistoryList.get(i-1).getRcwage().toString());if(statisticalHistoryList.get(i-1).getTotal()!=null){total += statisticalHistoryList.get(i-1).getTotal();}gd = gd.add(statisticalHistoryList.get(i-1).getGdwage());rc = rc.add(statisticalHistoryList.get(i-1).getRcwage());}XSSFRow sheet2row2 = sheet2.createRow((short) statisticalHistoryList.size()+1);cteateCell(wb,sheet2row2,(short)0,"合计:");cteateCell(wb,sheet2row2,(short)1,"");cteateCell(wb,sheet2row2,(short)2,"");cteateCell(wb,sheet2row2,(short)3,"");cteateCell(wb,sheet2row2,(short)4,"");cteateCell(wb,sheet2row2,(short)5,"");cteateCell(wb,sheet2row2,(short)6,String.valueOf(total));cteateCell(wb,sheet2row2,(short)7,"100%");cteateCell(wb,sheet2row2,(short)8,gd.setScale(0, BigDecimal.ROUND_HALF_UP).toString());cteateCell(wb,sheet2row2,(short)9,"");cteateCell(wb,sheet2row2,(short)10,rc.setScale(0, BigDecimal.ROUND_HALF_UP).toString());//sheet3XSSFSheet sheet3 = wb.createSheet();sheet3.setColumnWidth(1, 4000); sheet3.setColumnWidth(3, 6000); sheet3.setColumnWidth(4, 6000); sheet3.setColumnWidth(5, 6000); XSSFRow sheet3row1 = sheet3.createRow((short) 0);sheet3.createFreezePane(0, 1);cteateCell(wb, sheet3row1, (short) 0, "序号");cteateCell(wb, sheet3row1, (short) 1, "时间");cteateCell(wb, sheet3row1, (short) 2, "姓名");cteateCell(wb, sheet3row1, (short) 3, "考核加(扣)分内容");cteateCell(wb, sheet3row1, (short) 4, "加(扣)分依据");cteateCell(wb, sheet3row1, (short) 5, "加(扣)分值");cteateCell(wb, sheet3row1, (short) 6, "备注");SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");for(int i =1;i <= pointHistoryList.size();i++){XSSFRow sheet3rowi = sheet3.createRow((short) i);cteateCell(wb, sheet3rowi, (short) 0, String.valueOf(i));cteateCell(wb, sheet3rowi, (short) 1, simpleDateFormat.format(pointHistoryList.get(i-1).getPointdate()));cteateCell(wb, sheet3rowi, (short) 2, pointHistoryList.get(i-1).getUser().getUsername());cteateCell(wb, sheet3rowi, (short) 3, pointHistoryList.get(i-1).getContent());cteateCell(wb, sheet3rowi, (short) 4, pointHistoryList.get(i-1).getEvidence());cteateCell(wb, sheet3rowi, (short) 5, pointHistoryList.get(i-1).getCount().toString());cteateCell(wb, sheet3rowi, (short) 6, "");}//给每个sheet页起名字wb.setSheetName(0, "绩效工资发放表");wb.setSheetName(1, "绩效工资统计表");wb.setSheetName(2, "考核加扣分统计表");wb.write(os);os.flush();os.close();System.out.println("文件生成");}@SuppressWarnings("deprecation")private void cteateCell(XSSFWorkbook wb, XSSFRow row, short col,String val) {//设置行高row.setHeight((short) 480);XSSFCell cell = row.createCell(col);cell.setCellValue(val);XSSFCellStyle cellstyle = wb.createCellStyle();//HSSFFont Font = wb.createFont();//Font.setFontHeightInPoints((short) 10);//cellstyle.setFont(Font);cellstyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);cellstyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); //下边框cellstyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框cellstyle.setBorderTop(XSSFCellStyle.BORDER_THIN);//上边框cellstyle.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框cell.setCellStyle(cellstyle);}@SuppressWarnings("deprecation")private void cteateNoborderCell(XSSFWorkbook wb, XSSFRow row, short col,String val) {//设置行高row.setHeight((short) 480);XSSFCell cell = row.createCell(col);cell.setCellValue(val);XSSFCellStyle cellstyle = wb.createCellStyle();//HSSFFont Font = wb.createFont();//Font.setFontHeightInPoints((short) 10);//cellstyle.setFont(Font);cellstyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);cell.setCellStyle(cellstyle);}@SuppressWarnings({ "deprecation", "static-access" })private void cteateNumberCell(XSSFWorkbook wb, XSSFRow row, short col,String val) {//设置行高row.setHeight((short) 480);//生成单元格XSSFCell cell = row.createCell(col);// 设置单元格内容为double类型cell.setCellValue(Float.valueOf(val));//生成单元格样式XSSFCellStyle cellstyle = wb.createCellStyle();XSSFDataFormat df = wb.createDataFormat(); // 此处设置数据格式cellstyle.setDataFormat(df.getFormat("#,##0.00"));//保留两位小数点,("#,#0"数据格式只显示整数)("#,##0.00"保留两位)cellstyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);cellstyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); //下边框cellstyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框cellstyle.setBorderTop(XSSFCellStyle.BORDER_THIN);//上边框cellstyle.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框cell.setCellStyle(cellstyle);}}
}



java Excel导出 xlsx格式(超简单)相关推荐

  1. C# Winfrom Excel表格导出 Aspose.Cells超简单方式

    C# Winfrom Excel表格导出 Aspose.Cells超简单方式 首先需要下载 Aspose.Cells.dll,Aspose.Slides.dll,Aspose.Words.dll 这三 ...

  2. .net开发时用NPOI导出xlsx格式的Excel时,打开导出的Excel遇到“部分内容有问题”的报错

    项目场景: 提示:这里简述项目相关背景: .NET 开发 C# 问题描述 用NPOI导出xlsx格式的Excel时,打开导出的Excel遇到"部分内容有问题"的报错 原因分析: 写 ...

  3. php导出xlsx格式excel

    php许多框架默认导出的都是csv格式的表格,office 打开会是乱码,现要求导出xlsx格式表格 /*** Csv文件导出* @param array $out_data 要导出的数据* @par ...

  4. 关于excel导出日期格式变化问题处理

    关于excel导出日期格式变化问题处理 1.问题描述:java web项目,本来部署在windows下,导出数据库数据是,创建日期格式为2018-08-08 08:08:08.因服务器性能问题,迁移服 ...

  5. 十七 bootstrap-table tableExport 导出xlsx格式表格

    原文:十七 bootstrap-table tableExport 导出xlsx格式表格 在[十六.bootstrap-table javascript导出数据]中,打开导出的表格时,总会弹出一个提示 ...

  6. Java Excel导出复杂excel表格样式之ExcelUtil工具类

    Java Excel导出包括普通导出及复杂表格样式,主要是对于需要进行行列合并的列进行特殊处理,计算清楚起始行,结束行,起始列,结束列. 普通导出可以是所有列,也可以是包含某些列,或者排除某些列: 1 ...

  7. java excel 导出数据文件格式无效

    java excel 导出数据文件格式无效 的原因 WritableWorkbook workbook=Workbook.createWorkbook(new File("h:\\a.xls ...

  8. java excel导出(表头合并,多行表头)

    java excel导出(表头合并,多行表头) 原创:http://www.cnblogs.com/NJM-F/p/10222152.html @RequestMapping(value=" ...

  9. java excel导出 模板_Java Excel 导出 模板

    上面导出PDF和EXCEL的问题是图片路径出错!!! 数据库中存存的图片路径是"../dishpic/722f464f-3883-42aa-901f-21706da9c582.png&quo ...

最新文章

  1. java 泛型解耦,T-MVP:泛型深度解耦下的MVP大瘦身
  2. Linux命令【第一篇】
  3. 如何在python中打开文件_Python文件处理:创建、打开、追加、读、写
  4. c语言中把一个数缩小十倍_小学数学易错点:一个数缩小10倍?缩小到它的十分之一?哪个正确?...
  5. html track标签,HTML track标签
  6. docker搭建swoole简易聊天室
  7. asp.net MVC初学体会.
  8. Android范围自定义,android – 如何在dagger 2.10中创建自定义范围模块
  9. matlab常用符号意思,matlab常用的符号
  10. Eviews10下载及安装
  11. Win10电脑时间无法同步解决方法
  12. c语言二维数组学习,C语言学习之二维数组的传参
  13. URL重定向(跳转)漏洞
  14. HC小区管理系统mysql如何修改密码
  15. Java 設計模式 - 適配器模式
  16. 没有发生GC也进入了安全点?这段关于安全点的JVM源码有点意思!
  17. flutter图片聊天泡泡_Flutter 非常丰富的消息气泡效果合集
  18. 将搜狗输入法默认的中文输入更改成英文
  19. Fast Reed-Solomon Interactive Oracle Proofs of Proximity学习笔记
  20. TFmini Plus 开关量输出说明

热门文章

  1. 计算机网络---ICMP、IGMP协议
  2. JavaScript学习(五十五)—组合继承
  3. .net解析传过来的xml_隧桥管廊BIM+GIS运维管理系统方案解析
  4. 当一个人把你所有联系方式删除,意味着什么?
  5. 好想找一个灵魂伴侣,然后带着他一起周游世界,会实现吗?
  6. 大学计算机老师说未来不需要程序员,都是机器自动生成代码。老师说估计20年,程序员就会被取代,现实吗?
  7. 十年后,若中国基建基本完成了,还有什么能大规模拉动经济?
  8. 以太坊源码学习(一)
  9. Spring IOC容器的依赖注入流程(收集和注册、分析和组装)
  10. Apache目录介绍