zoukankan      html  css  js  c++  java
  • java 操作excel文件之系列二:《读取数据库的内容写入excel文件》

    要写入的文件:test.xls(sheet名也叫:test)

        public void createExcelTest(){

    //创建excel,设置sheet,表头, HSSFWorkbook workbook=new HSSFWorkbook(); HSSFSheet sheet=workbook.createSheet("test"); HSSFRow row=sheet.createRow(0); HSSFCell cell=row.createCell(0); cell.setCellValue("序号"); cell=row.createCell(1); cell.setCellValue("查询词"); cell=row.createCell(2); cell.setCellValue("cityid"); cell=row.createCell(3); cell.setCellValue("cityname"); cell=row.createCell(4); cell.setCellValue("showtype"); cell=row.createCell(5); cell.setCellValue("cateid");
    //操作数据库,从数据库中取出数据 Calendar c = Calendar.getInstance(); Date date = new Date(); c.setTime(date); c.add(Calendar.DAY_OF_YEAR, -30); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); String time = simpleDateFormat.format(c.getTime()); System.out.println(time); String sql = String.format("select `keywords`,`cityid` from testTable where dt>%s limit 10000", time); logger.info("执行的sql:" + sql); List<List<Object>> results = HiveQuery.executeSql(sql);

    //将数据库查询结果写入excel,创建行和单元格 for(int i=0;i<results.size();i++) { HSSFRow row1=sheet.createRow(i+1); row1.createCell(0).setCellValue(i); row1.createCell(1).setCellValue(results.get(i).get(0).toString()); row1.createCell(2).setCellValue(results.get(i).get(1).toString()); } try { FileOutputStream fos = new FileOutputStream("test.xls"); workbook.write(fos); fos.close(); }catch (IOException e){ e.printStackTrace(); } }

      

    下面是创建好的excel文件的内容:

  • 相关阅读:
    别老想着怎么用好RPC框架,你得多花时间琢磨原理
    业务代码真的会有这么多坑?
    mac全部看视频屏幕闪烁适用于白果黑果
    iterm2 proxy配置
    源端和目标端数据对比
    frp内网穿透,rdp远程连接Windows计算机
    frp rdp远程桌面
    按块提交抽取数据
    创建索引被锁ora-00054资源正忙,但指定以nowait方式
    2017-9-17C#笔记(方法,方法参数 ,foreach语句)
  • 原文地址:https://www.cnblogs.com/zhaijing/p/7200762.html
Copyright © 2011-2022 走看看