要做一个从一个数据库查询数据,每个小时查一次,然后写到一个表格里,这个表格保存到默认地址就可以。每天进行一次这个过程,又没有大神给讲解一下,重点是如何生成表格,然后每次都将数据写到这一个表格里,表格类似我贴的图片。
这是我昨天导数据的一个代码, 从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("导出完毕!");
}
借用楼上的思路,导出excel定义好列名,同时该excel的文件名用时间戳定义,不过建议封装工具类