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 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误