POI操作Excel換行

網上很多博客操作Excel換行的都是Office2007之前的格式,一直沒找到支持擴展名爲.xlsx的換行方式,被折騰到要死,無奈之下只能去官網看看,發現了同時支持.xlsx和.xls兩種擴展的。現在列出如下:

  Workbook wb = new XSSFWorkbook();   //or new HSSFWorkbook();
  Sheet sheet = wb.createSheet();

  Row row = sheet.createRow(2);
  Cell cell = row.createCell(2);
  cell.setCellValue("Use \n with word wrap on to create a new line");

   //to enable newlines you need set a cell styles with wrap=true
  CellStyle cs = wb.createCellStyle();
  cs.setWrapText(true);
  cell.setCellStyle(cs);

  //increase row height to accomodate two lines of text
  row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));

  //adjust column width to fit the content
  sheet.autoSizeColumn((short)2);

  FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx");
  wb.write(fileOut);
  fileOut.close();

官網鏈接如下:
POI NewLinesInCells

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章