SpringBoot项目POI实现导入导出 POI的版本为4.0.0

(这里写自定义目录标题)SpringBoot项目POI实现Excel表格的导入导出

POI的版本为4.0.0,网上查的资料大多都是POI3.12版本的,查了好多4.0.0版本的单元格样式,整理了一下。代码如下
pom.xml注入依赖

        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>4.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>4.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-scratchpad</artifactId>
        <version>4.0.0</version>
    </dependency>

控制层导出代码
@RequestMapping(value = “/export”)
@ResponseBody
public void export(HttpServletResponse response) throws IOException {
List user =UserService.selectUser();

 HSSFWorkbook wb = new HSSFWorkbook();
 HSSFSheet sheet = wb.createSheet("系统价格导入");   
 HSSFCellStyle style=wb.createCellStyle();
 HSSFFont font=wb.createFont();              
 HSSFRow row = null;
 
 
 row = sheet.createRow(0);//创建第一个单元格
 row.setHeight((short) (26.25 * 20));
 
 
 Cell cell=row.createCell(0);
 cell.setCellValue("价格维护");//为第一行单元格设值
 //为第一个单元格设置样式
 style.setAlignment(HorizontalAlignment.CENTER); //左右居中
 style.setVerticalAlignment(VerticalAlignment.CENTER);//上下居中
 font.setFontName("黑体"); 
 font.setFontHeightInPoints((short)18);//设置字体大小
 font.setBold(true);//设置字体加粗
 style.setFont(font);
 cell.setCellStyle(style);
 
 /*为标题设计空间
 * firstRow从第1行开始
 * lastRow从第0行结束
       *  从第1个单元格开始
       *  从第3个单元格结束
 */
 
 
 
             
 CellRangeAddress rowRegion = new CellRangeAddress(0, 0, 0,12);
 sheet.addMergedRegion(rowRegion);

/*
CellRangeAddress columnRegion = new CellRangeAddress(1,4,0,0);
sheet.addMergedRegion(columnRegion);
*/

 /*
     * 动态获取数据库列 sql语句 select COLUMN_NAME from INFORMATION_SCHEMA.Columns where table_name='user' and table_schema='test'
     * 第一个table_name 表名字
     * 第二个table_name 数据库名称
  */
 row = sheet.createRow(1);
 //设置背景色
 //创建单元格样式
 HSSFCellStyle cellstyle=wb.createCellStyle();
 cellstyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); 
 cellstyle.setFillBackgroundColor(IndexedColors.GREEN.getIndex());//设置图案背景颜色              
 cellstyle.setAlignment(HorizontalAlignment.CENTER); //左右居中
 cellstyle.setVerticalAlignment(VerticalAlignment.CENTER);//上下居中
 cellstyle.setWrapText(true);//设置自动换行  
 row.setHeight((short) (22.50 * 20));//设置行高
 row.setRowStyle(cellstyle);
 row.createCell(0).setCellValue("序号");//为第一个单元格设值
 row.createCell(1).setCellValue("装置");
 row.createCell(2).setCellValue("价格分类");
 row.createCell(3).setCellValue("指标名称");
 row.createCell(4).setCellValue("日期");
 row.createCell(5).setCellValue("计算价格");

 
 for (int i = 0; i < user.size(); i++) {
    row = sheet.createRow(i + 2);
   User user = User.get(i);
    row.createCell(1).setCellValue(user.getDevice());
    row.createCell(2).setCellValue(user.getPricetype());
    row.createCell(3).setCellValue(user.getIndexname());
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String udate=sdf.format(user.getUdate());
    row.createCell(4).setCellValue(udate);
    row.createCell(5).setCellValue(user.getSumprice());
   
 }
 sheet.setDefaultRowHeight((short) (16.5 * 20));
 //列宽自适应
 for (int i = 0; i <= 13; i++) {
    sheet.autoSizeColumn(i);
    sheet.setColumnWidth(i,sheet.getColumnWidth(i)*17/10);
 }

 response.setContentType("application/vnd.ms-excel;charset=utf-8");
 OutputStream os = response.getOutputStream();
 response.setHeader("Content-disposition", "attachment;filename=user.xls");//默认Excel名称
 wb.write(os);
 os.flush();
 os.close();

}
前端代码

其他的基本网上查资料就可以查得到

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值