java利用poi写入Excel

来源:互联网 发布:js confirm返回值 编辑:程序博客网 时间:2024/05/20 18:03
public void writeExcel(String url)
 {
  List<Synsetcn> list = sc.groupBySynsetId();
  Set<String> chineseList = sc.getAllSynsetcn().getChineseList();
  XSSFWorkbook workbook = new XSSFWorkbook();
  XSSFSheet sheet = workbook.createSheet();
  OutputStream os = null;
  XSSFCellStyle style = workbook.createCellStyle();
  XSSFFont font = workbook.createFont();
  font.setColor(HSSFColor.RED.index);
  style.setFont(font);
  try {
   os = new FileOutputStream(new File(url));
   XSSFRow row1 = sheet.createRow(0);
   XSSFCell cell1 = row1.createCell(0);
   XSSFCell cell2 = row1.createCell(1);
   cell1.setCellValue("词语");
   cell2.setCellValue("同义词");
   for (int rowIndex = 1; rowIndex <= list.size(); rowIndex++) {
    XSSFRow row = sheet.createRow(rowIndex);
    Synsetcn synsetcn = list.get(rowIndex-1);
    for (int cellNum = 0; cellNum < 3; cellNum++) {
     XSSFCell cell = row.createCell(cellNum);
     switch (cellNum) {
     case 0:
      cell.setCellValue(synsetcn.getSynset_id());
      break;
     case 1:
      cell.setCellValue(synsetcn.getChinese());
      break;
     case 2:
      String sb = null;
      for(String s : chineseList){
       String chinese = s;
       if(synsetcn.getChinese().contains(chinese)){
        if(sb != null){
         sb = sb + "||" + chinese;
        }else{
         sb = chinese;
        }
       }
      }
      cell.setCellValue(sb);
      cell.setCellStyle(style);
     }
    }
   }
   workbook.write(os);
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}
原创粉丝点击