List of usage examples for org.apache.poi.hssf.usermodel HSSFCellStyle setBorderBottom
@Override public void setBorderBottom(BorderStyle border)
From source file:org.sakaiproject.jsf.spreadsheet.SpreadsheetDataFileWriterXls.java
License:Educational Community License
private HSSFWorkbook getAsWorkbook(List<List<Object>> spreadsheetData) { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet();//from w w w .j av a2 s . c o m HSSFCellStyle headerCs = wb.createCellStyle(); Iterator<List<Object>> dataIter = spreadsheetData.iterator(); // Set the header style headerCs.setBorderBottom(HSSFCellStyle.BORDER_THIN); headerCs.setFillBackgroundColor(HSSFColor.BLUE_GREY.index); // Set the font HSSFCellStyle cellStyle = null; String fontName = ServerConfigurationService.getString("spreadsheet.font"); if (fontName != null) { HSSFFont font = wb.createFont(); font.setFontName(fontName); headerCs.setFont(font); cellStyle = wb.createCellStyle(); cellStyle.setFont(font); } // By convention, the first list in the list contains column headers. HSSFRow headerRow = sheet.createRow((short) 0); List<Object> headerList = dataIter.next(); for (short i = 0; i < headerList.size(); i++) { HSSFCell headerCell = createCell(headerRow, i); headerCell.setCellValue((String) headerList.get(i)); headerCell.setCellStyle(headerCs); sheet.autoSizeColumn(i); } short rowPos = 1; while (dataIter.hasNext()) { List<Object> rowData = dataIter.next(); HSSFRow row = sheet.createRow(rowPos++); for (short i = 0; i < rowData.size(); i++) { HSSFCell cell = createCell(row, i); Object data = rowData.get(i); if (data != null) { if (data instanceof Double) { cell.setCellValue(((Double) data).doubleValue()); } else { cell.setCellValue(data.toString()); } if (cellStyle != null) { cell.setCellStyle(cellStyle); } } } } return wb; }
From source file:pe.gob.mef.gescon.web.ui.BaseLegalMB.java
public void postProcessXLS(Object document) { HSSFWorkbook wb = (HSSFWorkbook) document; HSSFSheet sheet = wb.getSheetAt(0);//from ww w. ja va2 s. co m //Para los datos HSSFCellStyle centerStyle = wb.createCellStyle(); centerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFCellStyle centerGrayStyle = wb.createCellStyle(); centerGrayStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); centerGrayStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); centerGrayStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); HSSFCellStyle grayBG = wb.createCellStyle(); grayBG.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); grayBG.setFillPattern(CellStyle.SOLID_FOREGROUND); int i = 1; for (BaseLegal b : this.getListaBaseLegal()) { HSSFRow row = sheet.getRow(i); for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) { HSSFCell cell = row.getCell(j); if (i % 2 == 0) { if (j > 0) { cell.setCellStyle(centerGrayStyle); } else { cell.setCellStyle(grayBG); cell.setCellValue(b.getVnumero()); } } else { if (j > 0) { cell.setCellStyle(centerStyle); } else { cell.setCellValue(b.getVnumero()); } } } i++; } // Para la cabecera HSSFRow header = sheet.getRow(0); HSSFCellStyle headerStyle = wb.createCellStyle(); HSSFFont font = wb.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); headerStyle.setFont(font); for (int j = 0; j < header.getPhysicalNumberOfCells(); j++) { HSSFCell cell = header.getCell(j); cell.setCellStyle(headerStyle); sheet.autoSizeColumn(j); } }
From source file:pe.gob.mef.gescon.web.ui.BuenaPracticaMB.java
public void postProcessXLS(Object document) { HSSFWorkbook wb = (HSSFWorkbook) document; HSSFSheet sheet = wb.getSheetAt(0);/*from ww w . ja v a 2s . co m*/ //Para los datos HSSFCellStyle centerStyle = wb.createCellStyle(); centerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFCellStyle centerGrayStyle = wb.createCellStyle(); centerGrayStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); centerGrayStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); centerGrayStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); HSSFCellStyle grayBG = wb.createCellStyle(); grayBG.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); grayBG.setFillPattern(CellStyle.SOLID_FOREGROUND); int i = 1; for (Conocimiento c : this.getListaBuenaPractica()) { HSSFRow row = sheet.getRow(i); for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) { HSSFCell cell = row.getCell(j); if (i % 2 == 0) { if (j > 0) { cell.setCellStyle(centerGrayStyle); } else { cell.setCellStyle(grayBG); cell.setCellValue(c.getVtitulo()); } } else { if (j > 0) { cell.setCellStyle(centerStyle); } else { cell.setCellValue(c.getVtitulo()); } } } i++; } // Para la cabecera HSSFRow header = sheet.getRow(0); HSSFCellStyle headerStyle = wb.createCellStyle(); HSSFFont font = wb.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); headerStyle.setFont(font); for (int j = 0; j < header.getPhysicalNumberOfCells(); j++) { HSSFCell cell = header.getCell(j); cell.setCellStyle(headerStyle); sheet.autoSizeColumn(j); } }
From source file:pe.gob.mef.gescon.web.ui.ContenidoMB.java
public void postProcessXLS(Object document) { HSSFWorkbook wb = (HSSFWorkbook) document; HSSFSheet sheet = wb.getSheetAt(0);// ww w .j av a2 s .c o m //Para los datos HSSFCellStyle centerStyle = wb.createCellStyle(); centerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFCellStyle centerGrayStyle = wb.createCellStyle(); centerGrayStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); centerGrayStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); centerGrayStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); HSSFCellStyle grayBG = wb.createCellStyle(); grayBG.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); grayBG.setFillPattern(CellStyle.SOLID_FOREGROUND); int i = 1; for (Conocimiento c : this.getListaContenido()) { HSSFRow row = sheet.getRow(i); for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) { HSSFCell cell = row.getCell(j); if (i % 2 == 0) { if (j > 0) { cell.setCellStyle(centerGrayStyle); } else { cell.setCellStyle(grayBG); cell.setCellValue(c.getVtitulo()); } } else { if (j > 0) { cell.setCellStyle(centerStyle); } else { cell.setCellValue(c.getVtitulo()); } } } i++; } // Para la cabecera HSSFRow header = sheet.getRow(0); HSSFCellStyle headerStyle = wb.createCellStyle(); HSSFFont font = wb.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); headerStyle.setFont(font); for (int j = 0; j < header.getPhysicalNumberOfCells(); j++) { HSSFCell cell = header.getCell(j); cell.setCellStyle(headerStyle); sheet.autoSizeColumn(j); } }
From source file:pe.gob.mef.gescon.web.ui.OportunidadMB.java
public void postProcessXLS(Object document) { HSSFWorkbook wb = (HSSFWorkbook) document; HSSFSheet sheet = wb.getSheetAt(0);// www. j a v a 2s .c om //Para los datos HSSFCellStyle centerStyle = wb.createCellStyle(); centerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFCellStyle centerGrayStyle = wb.createCellStyle(); centerGrayStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); centerGrayStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); centerGrayStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); HSSFCellStyle grayBG = wb.createCellStyle(); grayBG.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); grayBG.setFillPattern(CellStyle.SOLID_FOREGROUND); int i = 1; for (Conocimiento c : this.getListaOportunidad()) { HSSFRow row = sheet.getRow(i); for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) { HSSFCell cell = row.getCell(j); if (i % 2 == 0) { if (j > 0) { cell.setCellStyle(centerGrayStyle); } else { cell.setCellStyle(grayBG); cell.setCellValue(c.getVtitulo()); } } else { if (j > 0) { cell.setCellStyle(centerStyle); } else { cell.setCellValue(c.getVtitulo()); } } } i++; } // Para la cabecera HSSFRow header = sheet.getRow(0); HSSFCellStyle headerStyle = wb.createCellStyle(); HSSFFont font = wb.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); headerStyle.setFont(font); for (int j = 0; j < header.getPhysicalNumberOfCells(); j++) { HSSFCell cell = header.getCell(j); cell.setCellStyle(headerStyle); sheet.autoSizeColumn(j); } }
From source file:pe.gob.mef.gescon.web.ui.PreguntaMB.java
public void postProcessXLS(Object document) { HSSFWorkbook wb = (HSSFWorkbook) document; HSSFSheet sheet = wb.getSheetAt(0);//w w w. java 2s .c om //Para los datos HSSFCellStyle centerStyle = wb.createCellStyle(); centerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFCellStyle centerGrayStyle = wb.createCellStyle(); centerGrayStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); centerGrayStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); centerGrayStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); HSSFCellStyle grayBG = wb.createCellStyle(); grayBG.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); grayBG.setFillPattern(CellStyle.SOLID_FOREGROUND); int i = 1; for (Pregunta p : this.getListaPregunta()) { HSSFRow row = sheet.getRow(i); for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) { HSSFCell cell = row.getCell(j); if (i % 2 == 0) { if (j > 0) { cell.setCellStyle(centerGrayStyle); } else { cell.setCellStyle(grayBG); cell.setCellValue(p.getVasunto()); } } else { if (j > 0) { cell.setCellStyle(centerStyle); } else { cell.setCellValue(p.getVasunto()); } } } i++; } // Para la cabecera HSSFRow header = sheet.getRow(0); HSSFCellStyle headerStyle = wb.createCellStyle(); HSSFFont font = wb.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); headerStyle.setFont(font); for (int j = 0; j < header.getPhysicalNumberOfCells(); j++) { HSSFCell cell = header.getCell(j); cell.setCellStyle(headerStyle); sheet.autoSizeColumn(j); } }
From source file:pe.gob.mef.gescon.web.ui.WikiMB.java
public void postProcessXLS(Object document) { HSSFWorkbook wb = (HSSFWorkbook) document; HSSFSheet sheet = wb.getSheetAt(0);// w w w . j av a 2s . com // Para la cabecera HSSFRow header = sheet.getRow(0); HSSFCellStyle headerStyle = wb.createCellStyle(); HSSFFont font = wb.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); headerStyle.setFont(font); for (int i = 0; i < header.getPhysicalNumberOfCells(); i++) { HSSFCell cell = header.getCell(i); cell.setCellStyle(headerStyle); sheet.autoSizeColumn(i); } HSSFCellStyle centerStyle = wb.createCellStyle(); centerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFCellStyle centerGrayStyle = wb.createCellStyle(); centerGrayStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); centerGrayStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); centerGrayStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); HSSFCellStyle grayBG = wb.createCellStyle(); grayBG.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); grayBG.setFillPattern(CellStyle.SOLID_FOREGROUND); int i = 1; for (Conocimiento c : this.getListaWiki()) { HSSFRow row = sheet.getRow(i); for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) { HSSFCell cell = row.getCell(j); if (i % 2 == 0) { if (j > 0) { cell.setCellStyle(centerGrayStyle); } else { cell.setCellStyle(grayBG); } } else { if (j > 0) { cell.setCellStyle(centerStyle); } } } i++; } }
From source file:poi.hssf.usermodel.examples.BigExample.java
License:Apache License
public static void main(String[] args) throws IOException { int rownum;// w w w.ja v a2 s . co m // create a new file FileOutputStream out = new FileOutputStream("workbook.xls"); // create a new workbook 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 3 cell styles HSSFCellStyle cs = wb.createCellStyle(); HSSFCellStyle cs2 = wb.createCellStyle(); HSSFCellStyle cs3 = wb.createCellStyle(); // create 2 fonts objects HSSFFont f = wb.createFont(); HSSFFont f2 = wb.createFont(); //set font 1 to 12 point type f.setFontHeightInPoints((short) 12); //make it red f.setColor(HSSFColor.RED.index); // make it bold //arial is the default font f.setBoldweight(f.BOLDWEIGHT_BOLD); //set font 2 to 10 point type f2.setFontHeightInPoints((short) 10); //make it the color at palette index 0xf (white) f2.setColor(HSSFColor.WHITE.index); //make it bold f2.setBoldweight(f2.BOLDWEIGHT_BOLD); //set cell stlye cs.setFont(f); //set the cell format see HSSFDataFromat for a full list cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)")); //set a thin border cs2.setBorderBottom(cs2.BORDER_THIN); //fill w fg fill color cs2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // set foreground fill to red cs2.setFillForegroundColor(HSSFColor.RED.index); // set the font cs2.setFont(f2); // set the sheet name to HSSF Test wb.setSheetName(0, "HSSF Test"); // create a sheet with 300 rows (0-299) for (rownum = 0; rownum < 300; rownum++) { // create a row r = s.createRow(rownum); // on every other row if ((rownum % 2) == 0) { // make the row height bigger (in twips - 1/20 of a point) r.setHeight((short) 0x249); } //r.setRowNum(( short ) rownum); // create 50 cells (0-49) (the += 2 becomes apparent later for (int cellnum = 0; cellnum < 50; cellnum += 2) { // create a numeric cell c = r.createCell(cellnum); // do some goofy math to demonstrate decimals c.setCellValue(rownum * 10000 + cellnum + (((double) rownum / 1000) + ((double) cellnum / 10000))); // on every other row if ((rownum % 2) == 0) { // set this cell to the first cell style we defined c.setCellStyle(cs); } // create a string cell (see why += 2 in the c = r.createCell(cellnum + 1); // set the cell's string value to "TEST" c.setCellValue("TEST"); // make this column a bit wider s.setColumnWidth(cellnum + 1, (int) ((50 * 8) / ((double) 1 / 20))); // on every other row if ((rownum % 2) == 0) { // set this to the white on red cell style // we defined above c.setCellStyle(cs2); } } } //draw a thick black border on the row at the bottom using BLANKS // advance 2 rows rownum++; rownum++; r = s.createRow(rownum); // define the third style to be the default // except with a thick black border at the bottom cs3.setBorderBottom(cs3.BORDER_THICK); //create 50 cells for (int cellnum = 0; cellnum < 50; cellnum++) { //create a blank type cell (no value) c = r.createCell(cellnum); // set it to the thick black border style c.setCellStyle(cs3); } //end draw thick black border // demonstrate adding/naming and deleting a sheet // create a sheet, set its title then delete it s = wb.createSheet(); wb.setSheetName(1, "DeletedSheet"); wb.removeSheetAt(1); //end deleted sheet // write the workbook to the output stream // close our file (don't blow out our file handles wb.write(out); out.close(); }
From source file:poi.hssf.usermodel.examples.Borders.java
License:Apache License
public static void main(String[] args) throws IOException { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("new sheet"); // Create a row and put some cells in it. Rows are 0 based. HSSFRow row = sheet.createRow(1);//from w w w.ja va 2 s . c om // Create a cell and put a value in it. HSSFCell cell = row.createCell(1); cell.setCellValue(4); // Style the cell with borders all around. HSSFCellStyle style = wb.createCellStyle(); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBottomBorderColor(HSSFColor.BLACK.index); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setLeftBorderColor(HSSFColor.GREEN.index); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setRightBorderColor(HSSFColor.BLUE.index); style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED); style.setTopBorderColor(HSSFColor.ORANGE.index); cell.setCellStyle(style); // Write the output to a file FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); }
From source file:poi.HSSFReadWrite.java
License:Apache License
/** * given a filename this outputs a sample sheet with just a set of * rows/cells.//from w w w. j a v a 2 s . co m */ private static void testCreateSampleSheet(String outputFilename) throws IOException { int rownum; HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); HSSFCellStyle cs = wb.createCellStyle(); HSSFCellStyle cs2 = wb.createCellStyle(); HSSFCellStyle cs3 = wb.createCellStyle(); HSSFFont f = wb.createFont(); HSSFFont f2 = wb.createFont(); f.setFontHeightInPoints((short) 12); f.setColor((short) 0xA); f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); f2.setFontHeightInPoints((short) 10); f2.setColor((short) 0xf); f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); cs.setFont(f); cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)")); cs2.setBorderBottom(HSSFCellStyle.BORDER_THIN); cs2.setFillPattern((short) 1); // fill w fg cs2.setFillForegroundColor((short) 0xA); cs2.setFont(f2); wb.setSheetName(0, "HSSF Test"); for (rownum = 0; rownum < 300; rownum++) { HSSFRow r = s.createRow(rownum); if ((rownum % 2) == 0) { r.setHeight((short) 0x249); } for (int cellnum = 0; cellnum < 50; cellnum += 2) { HSSFCell c = r.createCell(cellnum); c.setCellValue(rownum * 10000 + cellnum + (((double) rownum / 1000) + ((double) cellnum / 10000))); if ((rownum % 2) == 0) { c.setCellStyle(cs); } c = r.createCell(cellnum + 1); c.setCellValue(new HSSFRichTextString("TEST")); // 50 characters divided by 1/20th of a point s.setColumnWidth(cellnum + 1, (int) (50 * 8 / 0.05)); if ((rownum % 2) == 0) { c.setCellStyle(cs2); } } } // draw a thick black border on the row at the bottom using BLANKS rownum++; rownum++; HSSFRow r = s.createRow(rownum); cs3.setBorderBottom(HSSFCellStyle.BORDER_THICK); for (int cellnum = 0; cellnum < 50; cellnum++) { HSSFCell c = r.createCell(cellnum); c.setCellStyle(cs3); } s.addMergedRegion(new CellRangeAddress(0, 3, 0, 3)); s.addMergedRegion(new CellRangeAddress(100, 110, 100, 110)); // end draw thick black border // create a sheet, set its title then delete it wb.createSheet(); wb.setSheetName(1, "DeletedSheet"); wb.removeSheetAt(1); // end deleted sheet FileOutputStream out = new FileOutputStream(outputFilename); wb.write(out); out.close(); // wb.close(); }