Awayzcx930714 2017-04-27 02:02 采纳率: 50%
浏览 1655
已采纳

java定时查询数据库然后生成表格,保存到项目默认路径下,怎么弄?

要做一个从一个数据库查询数据,每个小时查一次,然后写到一个表格里,这个表格保存到默认地址就可以。每天进行一次这个过程,又没有大神给讲解一下,重点是如何生成表格,然后每次都将数据写到这一个表格里,表格类似我贴的图片。图片说明

  • 写回答

2条回答 默认 最新

  • 卜吃糖的小孩 2017-04-27 02:49
    关注

    这是我昨天导数据的一个代码, 从solr中查询出数据然后倒入到excel中,跟你这个差不多再加个定时就可以了。希望可以帮到你。
    private static int PAGE_SEZE=100; //一次解析条数
    /**
    * 创建表格
    * @param path 文件保存路径
    * @throws IOException
    * @throws SolrServerException
    */
    public void creatSheet1(String path) throws IOException, SolrServerException{
    XSSFWorkbook wb=new XSSFWorkbook();//excel文件对象

    XSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    style.setFillForegroundColor((short)43);
    style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
    XSSFFont font = wb.createFont();
    font.setFontHeightInPoints((short) 16);

              XSSFSheet sheet = wb.createSheet("sheet1");//工作表对象  
    
              XSSFRow row = sheet.createRow(0);  
              XSSFCell cell = row.createCell(0);  
                cell.setCellValue("enterpriseId");  
                cell.setCellStyle(style); 
    
                cell = row.createCell(1);  
                cell.setCellValue("title");  
                cell.setCellStyle(style); 
    
                cell = row.createCell(2);  
                cell.setCellValue("content");  
                cell.setCellStyle(style); 
    
                cell = row.createCell(3);  
                cell.setCellValue("url");  
                cell.setCellStyle(style); 
    
                cell = row.createCell(4);  
                cell.setCellValue("createDate");  
                cell.setCellStyle(style); 
    
                cell = row.createCell(5);  
                cell.setCellValue("resource");  
                cell.setCellStyle(style); 
    
                cell = row.createCell(6);  
                cell.setCellValue("pubDate");  
                cell.setCellStyle(style); 
    
                cell = row.createCell(7);  
                cell.setCellValue("getTime");  
                cell.setCellStyle(style); 
    
                cell = row.createCell(8);  
                cell.setCellValue("enterpriseName");  
                cell.setCellStyle(style); 
    
                cell = row.createCell(9);  
                cell.setCellValue("lableType");  
                cell.setCellStyle(style); 
    
                cell = row.createCell(10);  
                cell.setCellValue("rank");  
                cell.setCellStyle(style); 
    
                cell = row.createCell(11);  
                cell.setCellValue("contentType");  
                cell.setCellStyle(style); 
    
               int s =0;
               for(int in=0;;in++){
                      System.out.println("开始解析 , 从"+in*PAGE_SEZE+" 条到  "+ PAGE_SEZE);
                      SolrQuery query = new SolrQuery();
                        query.set("q", "pubDate:[2017-01-26 TO 2017-04-26] AND lableType:* ");
                        query.set("start", in*PAGE_SEZE);
                        query.set("rows", PAGE_SEZE);
                        query.set("fl", "enterpriseId,title,content,url,createDate,resource,pubDate,getTime,enterpriseName,lableType,rank,contentType");
                        query.set("sort", " pubDate desc");
                        QueryResponse  qr= solrServer.query(query);
                        SolrDocumentList docs = qr.getResults();
    
                        int count = 0;
                         for(SolrDocument doc : docs){
                                count++;
                                row = sheet.createRow((int) s + 1);  //行号
                                Map<String, Object> v = doc.getFieldValueMap();
                                row.createCell(0).setCellValue((String) v.get("enterpriseId")); 
                                row.createCell(1).setCellValue((String) v.get("title"));  //solr中的文章Id
                                row.createCell(2).setCellValue((String) (v.get("content") == null ? "": v.get("content")));  
                                row.createCell(3).setCellValue((String) v.get("url"));  
                                row.createCell(4).setCellValue((String) v.get("createDate"));  
                                row.createCell(5).setCellValue((String) v.get("resource")); 
                                row.createCell(6).setCellValue((String) v.get("pubDate"));  
                                row.createCell(7).setCellValue((String) v.get("getTime")); 
                                row.createCell(8).setCellValue((String) v.get("enterpriseName")); 
                                List<String> lableType = (List<String>)doc.getFieldValue("lableType");
                                String str = "";
                                for(String ss : lableType){
                                    str+= ss+",";
                                }
                                row.createCell(9).setCellValue(str);
                                row.createCell(10).setCellValue((String) (v.get("rank")== null ? "": v.get("rank"))); 
                                row.createCell(11).setCellValue((String) v.get("contentType"));
                                s++;
                        }
                         if(count < PAGE_SEZE ){
                                System.out.println("解析完毕。");
                             break;
                         }
               }
               FileOutputStream out=new FileOutputStream(path);  
               wb.write(out);  
               out.close();  
               System.out.println("导出完毕!");
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置