java poi excel操作示例(二)
reference
namedCell.setRefersToFormula(reference);
// 2. create named range for a single cell using cellreference
// Name namedCel2 = wb.createName();
// namedCel2.setNameName(cname);
// reference = sname+"!A1"; // cell reference
// namedCel2.setRefersToFormula(reference);
//
// // 3. create named range for an area using AreaReference
// Name namedCel3 = wb.createName();
// namedCel3.setNameName(cname);
// reference = sname+"!A1:C5"; // area reference
// namedCel3.setRefersToFormula(reference);
//
// // 4. create named formula
// Name namedCel4 = wb.createName();
// namedCel4.setNameName("my_sum");
// namedCel4.setRefersToFormula("SUM(sname+!$I$2:$I$6)");
// Note that sheet name is Excel must not exceed 31 characters
// and must not contain any of the any of the following characters:
// 0x0000
// 0x0003
// colon (:)
// backslash (\)
// asterisk (*)
// question mark ( )
// forward slash (/)
// opening square bracket ([)
// closing square bracket (])
// You can use
// org.apache.poi.ss.util.WorkbookUtil#createSafeSheetName(String
// nameProposal)}
// for a safe way to create valid names, this utility replaces invalid
// characters with a space (' ')"
//设置单元格值
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet1.createRow((short) 0);
// Create a cell and put a value in it.
Cell cell = row.createCell(0);
cell.setCellValue(1);
// Or do it on one line.
row.createCell(1).setCellValue(1.2);
row.createCell(2).setCellValue("This is a string");
row.createCell(3).setCellValue(true);
Row row2 = sheet2.createRow(0);
// Create a cell and put a date value in it. The first cell is not
// styled
// as a date.
Cell cell2 = row2.createCell(0);
cell2.setCellValue(new Date());
// we style the second cell as a date (and time). It is important to
// create a new cell style from the workbook otherwise you can end up
// modifying the built in style and effecting not only this cell but
// other cells.
//设置单元格日期格式
CellStyle cellStyle = wb.createCellStyle();
DataFormat format = wb.createDataFormat();
cellStyle.setDataFormat(format.getFormat("yyyy/mm/dd"));
// style.setDataFormat(format.getFormat("#,##0.0000"));
//设置单元格字体
Font font = wb.createFont();
font.setFontHeightInPoints((short) 24);
font.setFontName("Courier New");
font.setItalic(true);
font.setStrikeout(true);
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
cellStyle.setFont(font);
cellStyle.setWrapText(true);
cell2 = row2.createCell(1);
cell2.setCellValue(new Date());
cell2.setCellStyle(cellStyle);
// you can also set date as java.util.Calendar
cell2 = row2.createCell(2);
cell2.setCellValue(Calendar.getInstance());
cell2.setCellStyle(cellStyle);
//
// Create th