반응형
기존 버전
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | // import org.apache.poi.hssf.usermodel.*; HSSFWorkbook wb = new HSSFWorkbook(); // create a new sheet HSSFSheet s = wb.createSheet(); // declare a row object reference HSSFRow r = null; // declare a cell object reference HSSFCell c = null; // create 2 cell styles HSSFCellStyle cs = wb.createCellStyle(); HSSFCellStyle cs2 = wb.createCellStyle(); HSSFDataFormat df = wb.createDataFormat(); // create 2 fonts objects HSSFFont f = wb.createFont(); HSSFFont f2 = wb.createFont(); // Set font 1 to 12 point type, blue and bold f.setFontHeightInPoints((short) 12); f.setColor( HSSFColor.RED.index ); f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // Set font 2 to 10 point type, red and bold f2.setFontHeightInPoints((short) 10); f2.setColor( HSSFFont.RED.index ); f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // Set cell style and formatting cs.setFont(f); cs.setDataFormat(df.getFormat("#,##0.0")); // Set the other cell style and formatting cs2.setBorderBottom(cs2.BORDER_THIN); cs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); cs2.setFont(f2); // Define a few rows for(short rownum = (short)0; rownum < 30; rownum++) { HSSFRow r = s.createRow(rownum); for(short cellnum = (short)0; cellnum < 10; cellnum += 2) { HSSFCell c = r.createCell(cellnum); HSSFCell c2 = r.createCell(cellnum+1); c.setCellValue((double)rownum + (cellnum/10)); c2.setCellValue(new HSSFRichTextString("Hello! " + cellnum); } } // Save FileOutputStream out = new FileOutputStream("workbook.xls"); wb.write(out); out.close(); | cs |
new 버전
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | // import org.apache.poi.ss.usermodel.*; Workbook[] wbs = new Workbook[] { new HSSFWorkbook(), new XSSFWorkbook() }; for(int i=0; i<wbs.length; i++) { Workbook wb = wbs[i]; CreationHelper createHelper = wb.getCreationHelper(); // create a new sheet Sheet s = wb.createSheet(); // declare a row object reference Row r = null; // declare a cell object reference Cell c = null; // create 2 cell styles CellStyle cs = wb.createCellStyle(); CellStyle cs2 = wb.createCellStyle(); DataFormat df = wb.createDataFormat(); // create 2 fonts objects Font f = wb.createFont(); Font f2 = wb.createFont(); // Set font 1 to 12 point type, blue and bold f.setFontHeightInPoints((short) 12); f.setColor( IndexedColors.RED.getIndex() ); f.setBoldweight(Font.BOLDWEIGHT_BOLD); // Set font 2 to 10 point type, red and bold f2.setFontHeightInPoints((short) 10); f2.setColor( IndexedColors.RED.getIndex() ); f2.setBoldweight(Font.BOLDWEIGHT_BOLD); // Set cell style and formatting cs.setFont(f); cs.setDataFormat(df.getFormat("#,##0.0")); // Set the other cell style and formatting cs2.setBorderBottom(cs2.BORDER_THIN); cs2.setDataFormat(df.getFormat("text")); cs2.setFont(f2); // Define a few rows for(int rownum = 0; rownum < 30; rownum++) { Row r = s.createRow(rownum); for(int cellnum = 0; cellnum < 10; cellnum += 2) { Cell c = r.createCell(cellnum); Cell c2 = r.createCell(cellnum+1); c.setCellValue((double)rownum + (cellnum/10)); c2.setCellValue( createHelper.createRichTextString("Hello! " + cellnum) ); } } // Save String filename = "workbook.xls"; if(wb instanceof XSSFWorkbook) { filename = filename + "x"; } FileOutputStream out = new FileOutputStream(filename); wb.write(out); out.close(); } | cs |