java分组写入Excel

来源:互联网 发布:linux vim 复制 编辑:程序博客网 时间:2024/06/11 03:23
public void writeExcel(String url)
 {
  List<List<Synsetcn>> dataList = sc.getGroupList();
  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));
   int rowIndex = 1;
   for (List<Synsetcn> list : dataList) {
    for (Synsetcn synsetcn :list){
     XSSFRow row = sheet.createRow(rowIndex);
     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.getSynset_id().startsWith("7"))
        {
         if(synsetcn.getChinese().contains(chinese))
         {
          if(sb != null)
          {
           sb = sb + "||" + chinese;
          }
          else
          {
           sb = chinese;
          }
         }
        }
       }
       cell.setCellValue(sb);
       cell.setCellStyle(style);
      }
     }
     rowIndex++;
    }
    rowIndex++;
   }
   workbook.write(os);
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }