java poi excel操作示例(四)
e comment and set the text+author
CreationHelper factory = wb.getCreationHelper();
// When the comment box is visible, have it show in a 1x3 space
ClientAnchor anchor = factory.createClientAnchor();
anchor.setCol1(cell.getColumnIndex());
anchor.setCol2(cell.getColumnIndex() + 1);
anchor.setRow1(row.getRowNum());
anchor.setRow2(row.getRowNum() + 3);
Drawing drawing = sheet1.createDrawingPatriarch();
Comment comment = drawing.createCellComment(anchor);
RichTextString str = factory.createRichTextString("Hello, World!");
comment.setString(str);
comment.setAuthor("Apache POI");
// Assign the comment to the cell
cell.setCellComment(comment);
// Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL);
// link.setAddress("http://poi.apache.org/");
// cell.setHyperlink(link);
// cell.setCellStyle(cellStyle);
Row row3 = sheet2.createRow((short) 2);
row3.createCell(0).setCellValue(1.1);
row3.createCell(1).setCellValue(new Date());
row3.createCell(2).setCellValue(Calendar.getInstance());
row3.createCell(3).setCellValue("a string");
row3.createCell(4).setCellValue(true); www.2cto.com
row3.createCell(5).setCellType(Cell.CELL_TYPE_ERROR);
//获取sheet的每个单元格的值
Sheet sheet = wb.getSheetAt(0);
for (Iterator rit = sheet.rowIterator(); rit.hasNext();) {
Row row4 = rit.next();
for (Iterator| cit = row4.cellIterator(); cit.hasNext();) {
|
Cell cell4 = cit.next();
// do something here
}
}
Sheet sheet5 = wb.getSheetAt(1);
for (Row row5 : sheet5) {
for (Cell cell5 : row5) {
CellReference cellRef = new CellReference(row5.getRowNum(),
cell5.getColumnIndex());
System.out.print(cellRef.formatAsString());
System.out.print(" - ");
switch (cell5.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.println(cell5.getRichStringCellValue()
.getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell5)) {
System.out.println(cell5.getDateCellValue());
} else {
System.out.println(cell5.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.println(cell5.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
System.out.println(cell5.getCellFormula());
break;
default:
System.out.println();
}
}
}
wb.write(fileOut);
fileOut.close();
}
public void test2() throws IOException {
Workbook wb = new XSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
wb.write(fileOut);
fileOut.close();
}
public static void main(String[] args) throws IOExc