- Workbook wb = new HSSFWorkbook();
- FileOutputStream fileOut = new FileOutputStream("workbook.xls");
- wb.write(fileOut);
- fileOut.close();
- Workbook wb = new XSSFWorkbook();
- FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
- wb.write(fileOut);
- fileOut.close();
- Workbook wb = new HSSFWorkbook(); // or new XSSFWorkbook();
- Sheet sheet1 = wb.createSheet("new sheet");
- Sheet sheet2 = wb.createSheet("second sheet");
- //注意,表的名稱是Excel不得超過31個字符
- //並不能包含任何如下字符:
- //0x0000
- //0x0003
- //冒號(:)
- //反斜槓(\)
- //星號(*)
- //問號(?)
- //正斜線(/)
- //打開方括號([)
- //右方括號(])
- //你可使用org.apache.poi.ss.util.WorkbookUtil#createSafeSheetName(String nameProposal)}
- //一個安全的方式,以創建有效的名稱,該實用程序替換無效字符用空格('')
- String safeName = WorkbookUtil.createSafeSheetName("[O'Brien's sales*?]"); // returns " O'Brien's sales "
- Sheet sheet3 = wb.createSheet(safeName);
- FileOutputStream fileOut = new FileOutputStream("workbook.xls");
- wb.write(fileOut);
- fileOut.close();
- Workbook wb = new HSSFWorkbook();
- //Workbook wb = new XSSFWorkbook();
- CreationHelper createHelper = wb.getCreationHelper();
- Sheet sheet = wb.createSheet("new sheet");
- // 建立一行,位置爲0
- Row row = sheet.createRow((short)0);
- // 建立一個單元格並賦值
- Cell cell = row.createCell(0);
- cell.setCellValue(1);
- row.createCell(1).setCellValue(1.2);
- //設置String的值
- row.createCell(2).setCellValue(
- createHelper.createRichTextString("This is a String"));
- //設置日期格式
- CellStyle cellStyle = wb.createCellStyle();
- cellStyle.setDataFormat(
- createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
- cell = row.createCell(3);
- cell.setCellValue(new Date());
- cell.setCellStyle(cellStyle);
- cell = row.createCell(4);
- cell.setCellValue(Calendar.getInstance());
- cell.setCellStyle(cellStyle);
- row.createCell(5).setCellValue(new Date());
- FileOutputStream fileOut = new FileOutputStream("workbook.xls");
- wb.write(fileOut);
- fileOut.close();
- sheet.shiftRows(starRow + 1, sheet.getLastRowNum(), 1, true, false);
- for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
- CellRangeAddress range = sheet.getMergedRegion(i);
- if (range.getFirstRow()==starRow&& starRow==range.getLastRow()) {
- //插入合併的行
- sheet.addMergedRegion(new CellRangeAddress(starRow + 1,
- starRow + 1, range.getFirstColumn(), range
- .getLastColumn()));
- }
- }
- createCell(wb, row, (short) 0, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM);
- createCell(wb, row, (short) 1, CellStyle.ALIGN_CENTER_SELECTION, CellStyle.VERTICAL_BOTTOM);
- createCell(wb, row, (short) 2, CellStyle.ALIGN_FILL, CellStyle.VERTICAL_CENTER);
- createCell(wb, row, (short) 3, CellStyle.ALIGN_GENERAL, CellStyle.VERTICAL_CENTER);
- createCell(wb, row, (short) 4, CellStyle.ALIGN_JUSTIFY, CellStyle.VERTICAL_JUSTIFY);
- createCell(wb, row, (short) 5, CellStyle.ALIGN_LEFT, CellStyle.VERTICAL_TOP);
- createCell(wb, row, (short) 6, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_TOP);
- public void insertRow(int starRow) {
- sheet.shiftRows(starRow + 1, sheet.getLastRowNum(), 1, true, false);
- for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
- Region range = sheet.getMergedRegionAt(i);
- if (range.getRowFrom() == starRow && starRow == range.getRowTo()) {
- sheet.addMergedRegion(new Region(starRow + 1,
- (short) (starRow + 1), range.getColumnFrom(), range
- .getColumnTo()));
- }
- }
- HSSFRow fromRow = sheet.getRow(starRow);
- HSSFRow newRow = sheet.getRow(starRow + 1);
- HSSFCell newCell = null;
- HSSFCell fromCell = null;
- for (int i = 0; i < sheet.getRow(starRow).getPhysicalNumberOfCells(); i++) {
- fromCell = fromRow.getCell((short) i);
- if (fromCell == null) {
- continue;
- }
- newCell = newRow.createCell((short) i);
- newCell.setCellStyle(fromCell.getCellStyle());
- int cType = fromCell.getCellType();
- newCell.setCellType(cType);
- }
- }
- /** * 拷貝Excel行 * 從 origSheet的origStartRow行到origEndRow行,複製到newsheet從targetStartNum位置開始 * @param origSheet 原sheet * @param newsheet 目標sheet * @param origStartRow 開始行 * @param origEndRow 結束行 * @param targetStartNum 目標開始行 **/
- public void copyRows(HSSFSheet origSheet, HSSFSheet newsheet,
- int origStartRow, int origEndRow, int targetStartNum) {
- if ((origStartRow == -1) || (origEndRow == -1)
- || origEndRow < origStartRow) {
- return;
- }
- // 拷貝合併的單元格
- Region region = null;
- for (int i = 0; i < origSheet.getNumMergedRegions(); i++) {
- region = origSheet.getMergedRegionAt(i);
- if ((region.getRowFrom() >= origStartRow)
- && (region.getRowTo() <= origEndRow)) {
- region.setRowFrom(region.getRowFrom() + targetStartNum);
- region.setRowTo(region.getRowTo() + targetStartNum);
- newsheet.addMergedRegion(region);
- }
- }
- HSSFRow fromRow = null;
- HSSFRow newRow = null;
- HSSFCell newCell = null;
- HSSFCell fromCell = null;
- // 拷貝行並填充數據
- for (int i = 0; i <= origEndRow; i++) {
- fromRow = origSheet.getRow(i);
- if (fromRow == null) {
- continue;
- }
- newRow = newsheet.createRow(i - origStartRow + targetStartNum);
- newRow.setHeight(fromRow.getHeight());
- for (int j = fromRow.getFirstCellNum(); j < fromRow
- .getPhysicalNumberOfCells(); j++) {
- fromCell = fromRow.getCell((short) j);
- if (fromCell == null) {
- continue;
- }
- newCell = newRow.createCell((short) j);
- newCell.setCellStyle(fromCell.getCellStyle());
- int cType = fromCell.getCellType();
- newCell.setCellType(cType);
- switch (cType) {
- case HSSFCell.CELL_TYPE_STRING:
- newCell.setCellValue(fromCell.getStringCellValue());
- break;
- case HSSFCell.CELL_TYPE_NUMERIC:
- newCell.setCellValue(fromCell.getNumericCellValue());
- break;
- case HSSFCell.CELL_TYPE_FORMULA:
- newCell.setCellFormula(fromCell.getCellFormula());
- break;
- case HSSFCell.CELL_TYPE_BOOLEAN:
- newCell.setCellValue(fromCell.getBooleanCellValue());
- break;
- case HSSFCell.CELL_TYPE_ERROR:
- newCell.setCellValue(fromCell.getErrorCellValue());
- break;
- default:
- newCell.setCellValue(fromCell.getStringCellValue());
- break;
- }
- }
- }
- }