public void createDateCell() throws Exception {Workbook wb = new HSSFWorkbook();// Workbook wb = new XSSFWorkbook();CreationHelper createHelper = wb.getCreationHelper();Sheet sheet = wb.createSheet("new sheet");// Create a row and put some cells in it. Rows are 0 based.Row row = sheet.createRow(0);// Create a cell and put a date value in it. The first cell is not// styled// as a date.Cell cell = row.createCell(0);cell.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();cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy/MM/dd hh:mm"));cell = row.createCell(1);// cell.setCellValue(new Date());Date date = new Date();cell.setCellValue(createHelper.createRichTextString(date.toString()));cell.setCellStyle(cellStyle);// you can also set date as java.util.Calendarcell = row.createCell(2);cell.setCellValue(Calendar.getInstance());cell.setCellStyle(cellStyle);// Write the output to a fileFileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();}
public void createCellType() throws Exception{Workbook wb = new HSSFWorkbook();Sheet sheet = wb.createSheet("new sheet");Row row = sheet.createRow((short)2);row.createCell(0).setCellValue(1.1);row.createCell(1).setCellValue(new Date());row.createCell(2).setCellValue(Calendar.getInstance());row.createCell(3).setCellValue("a string");row.createCell(4).setCellValue(true);row.createCell(5).setCellType(HSSFCell.CELL_TYPE_ERROR);// Write the output to a fileFileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();}
public void createBorder() throws Exception {Workbook wb = new HSSFWorkbook();Sheet sheet = wb.createSheet("new sheet");// Create a row and put some cells in it. Rows are 0 based.Row row = sheet.createRow(1);// Create a cell and put a value in it.Cell cell = row.createCell(1);cell.setCellValue(4);// Style the cell with borders all around.CellStyle style = wb.createCellStyle();style.setBorderBottom(CellStyle.BORDER_THIN);style.setBottomBorderColor(IndexedColors.BLACK.getIndex());style.setBorderLeft(CellStyle.BORDER_THIN);style.setLeftBorderColor(IndexedColors.GREEN.getIndex());style.setBorderRight(CellStyle.BORDER_THIN);style.setRightBorderColor(IndexedColors.BLUE.getIndex());style.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED);style.setTopBorderColor(IndexedColors.BLACK.getIndex());cell.setCellStyle(style);// Write the output to a fileFileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();}
InputStream inp = new FileInputStream("D:\\hjn.xls");HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));ExcelExtractor extractor = new ExcelExtractor(wb);extractor.setFormulasNotResults(true);extractor.setIncludeSheetNames(true);String text = extractor.getText();System.out.println(text);
public void fillAndColors() throws Exception{Workbook wb = new HSSFWorkbook();Sheet sheet = wb.createSheet("new sheet");// Create a row and put some cells in it. Rows are 0 based.Row row = sheet.createRow((short) 1);// Aqua backgroundCellStyle style = wb.createCellStyle();style.setFillBackgroundColor(IndexedColors.BLUE.getIndex());style.setFillPattern(CellStyle.ALIGN_FILL);Cell cell = row.createCell((short) 1);cell.setCellValue("X");cell.setCellStyle(style);// Orange "foreground", foreground being the fill foreground not the font color.style = wb.createCellStyle();style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row.createCell((short) 2);cell.setCellValue("X");cell.setCellStyle(style);// Write the output to a fileFileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();}
public void mergingCell() throws Exception{Workbook wb = new HSSFWorkbook();Sheet sheet = wb.createSheet("new sheet");Row row = sheet.createRow((short) 1);Cell cell = row.createCell((short) 1);cell.setCellValue("This is a test of merging");sheet.addMergedRegion(new CellRangeAddress(1, // first row (0-based)4, // last row (0-based)1, // first column (0-based)6 // last column (0-based)));// Write the output to a fileFileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();}
public void createFont() throws Exception{Workbook wb = new HSSFWorkbook();Sheet sheet = wb.createSheet("new sheet");// Create a row and put some cells in it. Rows are 0 based.Row row = sheet.createRow(1);// Create a new font and alter it.Font font = wb.createFont();font.setFontHeightInPoints((short)24);font.setFontName("Courier New");font.setItalic(true);font.setStrikeout(true);// Fonts are set into a style so create a new one to use.CellStyle style = wb.createCellStyle();style.setFont(font);// Create a cell and put a value in it.Cell cell = row.createCell(1);cell.setCellValue("This is a test of fonts");cell.setCellStyle(style);// Write the output to a fileFileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();}
HSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet = wb.createSheet();HSSFRow row = sheet.createRow((short) 0);HSSFCell cell = row.createCell((short) 0);cell.setCellValue("Default Palette");//apply some colors from the standard palette,// as in the previous examples.//we'll use red text on a lime backgroundHSSFCellStyle style = wb.createCellStyle();style.setFillForegroundColor(HSSFColor.LIME.index);style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);HSSFFont font = wb.createFont();font.setColor(HSSFColor.RED.index);style.setFont(font);cell.setCellStyle(style);//save with the default paletteFileOutputStream out = new FileOutputStream("default_palette.xls");wb.write(out);out.close();//now, let's replace RED and LIME in the palette// with a more attractive combination// (lovingly borrowed from freebsd.org)cell.setCellValue("Modified Palette");//creating a custom palette for the workbookHSSFPalette palette = wb.getCustomPalette();//replacing the standard red with freebsd.org redpalette.setColorAtIndex(HSSFColor.RED.index,(byte) 153, //RGB red (0-255)(byte) 0, //RGB green(byte) 0 //RGB blue);//replacing lime with freebsd.org goldpalette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);//save with the modified palette// note that wherever we have previously used RED or LIME, the// new colors magically appearout = new FileOutputStream("modified_palette.xls");wb.write(out);out.close();
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=trueCellStyle cs = wb.createCellStyle();cs.setWrapText(true);cell.setCellStyle(cs);//increase row height to accomodate two lines of textrow.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));//adjust column width to fit the contentsheet.autoSizeColumn((short)2);FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx");wb.write(fileOut);fileOut.close();
?
?換行的步驟:
在需要換行的地方加上\n?? cell.setCellValue("Use \n with word wrap on to create a new line");
Workbook wb = new HSSFWorkbook();Sheet sheet = wb.createSheet("Sheet1");//sets the print area for the first sheetwb.setPrintArea(0, "$A$1:$C$2");//Alternatively:wb.setPrintArea(0, //sheet index0, //start column1, //end column0, //start row0 //end row);FileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();
Workbook wb = new HSSFWorkbook();Sheet sheet = wb.createSheet("format sheet");PrintSetup ps = sheet.getPrintSetup();sheet.setAutobreaks(true);ps.setFitHeight((short)1);ps.setFitWidth((short)1);// Create various cells and rows for spreadsheet.FileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();
public void shiftRow() throws Exception{Workbook wb = new HSSFWorkbook();Sheet sheet = wb.createSheet("row sheet");for(int i=0;i<20;i++){Row row=sheet.createRow(i);Cell cell=row.createCell(0);cell.setCellValue(""+i);}// Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5)//把第6-11行向上移動5行sheet.shiftRows(5, 10,-5);FileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();}