zoukankan      html  css  js  c++  java
  • poi生成excel文件

    一, poi生成excel文件

      1)引入依赖

    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <!-- 报表 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>
        
        <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.9</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.9</version>
        </dependency>

    二,调用

    @Controller
    public class PoiController {
        
        @Autowired
        private LogMapper logMapper;
        
        @RequestMapping("getpoi")
        public void getpoi(HttpServletResponse response) {
            //获取登录日志表
            List<Log> logList=logMapper.getloglist();
            //创建对象 SXSSFWorkbook用于处理大量数据问题,使用内存达到效果,再结合分页查询,一个表达到指定容量就开启下张表;
            //SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
            HSSFWorkbook workbook = new HSSFWorkbook();
            //创建表
            HSSFSheet sheet = workbook.createSheet("登录日志表");
            //创建第一行,填充名称
            HSSFRow row = sheet.createRow(0);
            
            //获取第一行每个单元格进行填充
            HSSFCell createCell0 = row.createCell(0);
            createCell0.setCellValue("id");
            
            HSSFCell createCell1 = row.createCell(1);
            createCell1.setCellValue("用户IP地址");
            
            HSSFCell createCell2 = row.createCell(2);
            createCell2.setCellValue("用户登录时间");
            
            HSSFCell createCell3 = row.createCell(3);
            createCell3.setCellValue("用户名");
            
            HSSFCell createCell4 = row.createCell(4);
            createCell4.setCellValue("前端登录/后端登录");
            HSSFRow rowi = null;
            HSSFCell createCell =null;
            for (int i = 0; i < logList.size(); i++) {
                //创建第二行,填充内容
                 rowi = sheet.createRow(i+1);
                 
                createCell = rowi.createCell(0);//获取第一个单元格
                createCell.setCellValue(logList.get(i).getiId());
                
                createCell = rowi.createCell(1);//获取第二个单元格
                createCell.setCellValue(logList.get(i).getiIp());
                
                createCell = rowi.createCell(2);//获取第三个单元格
                createCell.setCellValue(logList.get(i).getiDate());
                
                createCell = rowi.createCell(3);//获取第四个单元格
                createCell.setCellValue(logList.get(i).getuName());
                
                createCell = rowi.createCell(4);//获取第五个单元格
                createCell.setCellValue("1".equals(logList.get(i).getiType())?"前端登录":"后端登录");
                    
            }
            //写出
            //这个流从response获取,再配置响应头信息就可以实现下载;
            OutputStream out=null;
            try {
                out = new FileOutputStream(new File("F:/test","test.xls"));
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
            
            try {
                
                workbook.write(out);
                
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    三,效果展示

     

     

  • 相关阅读:
    001 数电 (1) : 数制和码制
    js页面滚动浮动层智能定位(MooTools)实例页面
    Value  of type java.lang.String cannot be converted to JSONObject 错误解决
    关于线程池的shutdown()方法
    Can't create handler inside thread that has not called Looper.prepare()
    配置 Win7 和 IIS7 以支持WCF
    复制JSON对象
    js页面滚动浮动层智能定位(jQuery)实例页面
    Enum和string的相互转换
    TBCompressor 2.4.2修改版,可对目录中的JS/CSS文件进行压缩
  • 原文地址:https://www.cnblogs.com/hi-feng/p/8017136.html
Copyright © 2011-2022 走看看