List of usage examples for org.apache.poi.ss.usermodel Workbook createFont
Font createFont();
From source file:br.com.tecsinapse.exporter.style.TableCellStyle.java
License:LGPL
public CellStyle toCellStyle(Workbook wb) { CellStyle cellStyle = wb.createCellStyle(); Font font = wb.createFont(); if (getBackgroundColor() != null) { cellStyle.setFillForegroundColor(getBackgroundColor().getIndex()); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); }/*from w w w . j ava 2 s . co m*/ if (getBorder() != null) { cellStyle = border.toCellStyle(cellStyle); } if (getvAlign() != null) { cellStyle.setVerticalAlignment(vAlign.getCellStyleVAlign()); } if (gethAlign() != null) { cellStyle.setAlignment(hAlign.getCellStyleHAlign()); } configFont(font); cellStyle.setFont(font); if (cellFormat != null && !cellFormat.isEmpty()) { cellStyle.setDataFormat(wb.createDataFormat().getFormat(cellFormat)); } return cellStyle; }
From source file:br.ufpa.psi.comportamente.labgame.relatorios.RelatorioJogadasExperimento.java
License:Open Source License
public InputStream relatorioOntogenese(Long idExp) throws FileNotFoundException, IOException { Workbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet("Relatrio Ontognese"); JogadaDAO jogadaDAO = new JogadaDAO(); jogadaDAO.beginTransaction();//from www . j a v a 2 s . c om List<Jogada> jogadasAux = jogadaDAO.encontrarPorExperimento(idExp); jogadaDAO.stopOperation(false); ExperimentoDAO expDAO = new ExperimentoDAO(); expDAO.beginTransaction(); Experimento experimento = expDAO.find(Experimento.class, idExp); expDAO.stopOperation(false); JogadorDAO jogDAO = new JogadorDAO(); jogDAO.beginTransaction(); List<Jogador> jogadoresTotais = jogDAO.encontraPorExperimento(experimento); jogDAO.stopOperation(false); //CRIA O CABEALHO "ONTOGNESE int quantidadeColunas = 6; int quantidadeJogadoresPorCiclo = experimento.getTamanhoFilaJogadores(); //Vai pegar o valor de acordo com o experimento. int larguraColuna = 4600; int colunaAtual; int colunaFinalOntogenese = (quantidadeColunas * quantidadeJogadoresPorCiclo) + 1; // --- DEFINE AS PROPRIEDADES DAS CLULAS sheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 0)); sheet.addMergedRegion(new CellRangeAddress(0, 2, 1, 1)); sheet.setColumnWidth(0, 1500); sheet.setColumnWidth(1, 2000); sheet.addMergedRegion(new CellRangeAddress(0, 1, 2, colunaFinalOntogenese)); sheet.addMergedRegion(new CellRangeAddress(1, 2, colunaFinalOntogenese + 1, colunaFinalOntogenese + 1)); sheet.setColumnWidth(colunaFinalOntogenese + 1, 3000); sheet.addMergedRegion(new CellRangeAddress(1, 3, colunaFinalOntogenese + 2, colunaFinalOntogenese + 2)); sheet.setColumnWidth(colunaFinalOntogenese + 2, 4000); // --- FIM //for (int i = 0; i < quantidadeJogadoresPorCiclo; i++) //if(i == 1){ sheet.setColumnWidth(2, larguraColuna); // Campo com o nome do participante fica nessa coluna. colunaAtual = 2 + quantidadeColunas; //} else { sheet.setColumnWidth((colunaAtual), larguraColuna); colunaAtual += quantidadeColunas; sheet.setColumnWidth(colunaAtual, larguraColuna); //} // ---DEFINE AS CORES DE CADA FONTE XSSFFont fonteBranca = (XSSFFont) wb.createFont(); fonteBranca.setColor(new XSSFColor(Color.WHITE)); XSSFFont fonteNegra = (XSSFFont) wb.createFont(); fonteNegra.setColor(new XSSFColor(Color.BLACK)); XSSFFont fonteVermelha = (XSSFFont) wb.createFont(); fonteVermelha.setColor(new XSSFColor(Color.RED)); // --- FIM // --- DEFINE ESTILOS CLULAS //ESTILO COLUNA LINHA XSSFCellStyle estiloColunaLinha = (XSSFCellStyle) wb.createCellStyle(); estiloColunaLinha.setVerticalAlignment(VerticalAlignment.CENTER); estiloColunaLinha.setAlignment(CellStyle.ALIGN_CENTER); estiloColunaLinha.setFillForegroundColor(new XSSFColor(Color.LIGHT_GRAY)); estiloColunaLinha.setFillPattern(CellStyle.SOLID_FOREGROUND); estiloColunaLinha.getFont().setBold(true); estiloColunaLinha.setBorderBottom(BorderStyle.MEDIUM); estiloColunaLinha.setBorderLeft(BorderStyle.MEDIUM); estiloColunaLinha.setBorderRight(BorderStyle.MEDIUM); estiloColunaLinha.setBorderTop(BorderStyle.MEDIUM); XSSFCellStyle estiloColunaColuna = (XSSFCellStyle) wb.createCellStyle(); estiloColunaColuna.setVerticalAlignment(VerticalAlignment.CENTER); estiloColunaColuna.setAlignment(CellStyle.ALIGN_CENTER); estiloColunaColuna.getFont().setBold(true); //ESTILO CABEALHO XSSFCellStyle estiloCabecalhoColunaAB = (XSSFCellStyle) wb.createCellStyle(); estiloCabecalhoColunaAB.setVerticalAlignment(VerticalAlignment.CENTER); estiloCabecalhoColunaAB.setAlignment(CellStyle.ALIGN_CENTER); estiloCabecalhoColunaAB.setFillForegroundColor(new XSSFColor(Color.LIGHT_GRAY)); estiloCabecalhoColunaAB.setFillPattern(CellStyle.SOLID_FOREGROUND); estiloCabecalhoColunaAB.getFont().setBold(true); XSSFCellStyle estiloCabecalhoColunaP = (XSSFCellStyle) wb.createCellStyle(); estiloCabecalhoColunaP.setVerticalAlignment(VerticalAlignment.CENTER); estiloCabecalhoColunaP.setAlignment(CellStyle.ALIGN_CENTER); estiloCabecalhoColunaP.setFont(fonteNegra); estiloCabecalhoColunaP.getFont().setBold(true); //ESTILO MUDANA DE CICLO XSSFCellStyle estiloMudancaCiclo = (XSSFCellStyle) wb.createCellStyle(); estiloMudancaCiclo.setVerticalAlignment(VerticalAlignment.CENTER); estiloMudancaCiclo.setAlignment(CellStyle.ALIGN_CENTER); estiloMudancaCiclo.setFillForegroundColor(new XSSFColor(Color.RED)); estiloMudancaCiclo.setFillPattern(CellStyle.SOLID_FOREGROUND); estiloMudancaCiclo.setFont(fonteBranca); //ESTILO CICLO SEM MUDANA XSSFCellStyle estiloCicloSemMudanca = (XSSFCellStyle) wb.createCellStyle(); estiloCicloSemMudanca.setVerticalAlignment(VerticalAlignment.CENTER); estiloCicloSemMudanca.setAlignment(CellStyle.ALIGN_CENTER); estiloCicloSemMudanca.setFont(fonteNegra); //ESTILO NOME PARTICIPANTE XSSFCellStyle estiloNomeP = (XSSFCellStyle) wb.createCellStyle(); estiloNomeP.setVerticalAlignment(VerticalAlignment.CENTER); estiloNomeP.setAlignment(CellStyle.ALIGN_CENTER); estiloNomeP.setBorderBottom(BorderStyle.DOTTED); estiloNomeP.setBorderTop(BorderStyle.DOTTED); estiloNomeP.setFillForegroundColor(new XSSFColor(Color.BLACK)); estiloNomeP.setFillPattern(CellStyle.SOLID_FOREGROUND); estiloNomeP.setFont(fonteBranca); estiloNomeP.getFont().setBold(true); //ESTILO ESTABILIDADE XSSFCellStyle estiloEstabilidade = (XSSFCellStyle) wb.createCellStyle(); estiloEstabilidade.setVerticalAlignment(VerticalAlignment.CENTER); estiloEstabilidade.setAlignment(CellStyle.ALIGN_CENTER); estiloEstabilidade.setFont(fonteVermelha); estiloEstabilidade.getFont().setBold(true); // --- FIM int cr = 0; // --- CRIA PRIMEIRA COLUNA (CABEALHO) XSSFRow row1 = (XSSFRow) sheet.createRow((short) cr++); //CRIA CLULA 1 XSSFCell c1 = row1.createCell(0); c1.setCellValue("FASE"); c1.setCellStyle(estiloCabecalhoColunaAB); //CRIA CLULA 2 XSSFCell c2 = row1.createCell(1); c2.setCellValue("CICLO"); c2.setCellStyle(estiloCabecalhoColunaAB); //CRIA CLULA 3 (ONTOGNESE) XSSFCell cOnto = row1.createCell(2); cOnto.setCellValue("ONTOGNESE"); cOnto.setCellStyle(estiloCicloSemMudanca); //CRIA CLULA 'CC' XSSFRow row2 = (XSSFRow) sheet.createRow(1); XSSFCell cCC = row2.createCell(colunaFinalOntogenese + 1); cCC.setCellValue("CC"); cCC.setCellStyle(estiloCicloSemMudanca); //CRIA CLULA 'ESTABILIDADE' XSSFCell cEstab = row2.createCell(colunaFinalOntogenese + 2); cEstab.setCellValue("ESTABILIDADE"); cEstab.setCellStyle(estiloEstabilidade); // --- FIM int contadorCelulasCabecalho = 2; int contadorAcertosCultural = 0; XSSFRow row3 = (XSSFRow) sheet.createRow((short) 2); //GERA O CABEALHO DAS JOGADAS for (int i = 0; i < experimento.getTamanhoFilaJogadores(); i++) { //CRIA CLULA 3 NA LINHA 3 XSSFCell c3 = row3.createCell(contadorCelulasCabecalho++); c3.setCellValue("P"); c3.setCellStyle(estiloCabecalhoColunaP); //CRIA CLULA 4 NA LINHA 3 XSSFCell c4 = row3.createCell(contadorCelulasCabecalho++); c4.setCellValue("Linha"); c4.setCellStyle(estiloCabecalhoColunaP); //CRIA CLULA 5 NA LINHA 3 XSSFCell c5 = row3.createCell(contadorCelulasCabecalho++); c5.setCellValue("Cor"); c5.setCellStyle(estiloCabecalhoColunaP); //CRIA CLULA 6 NA LINHA 3 XSSFCell c6 = row3.createCell(contadorCelulasCabecalho++); c6.setCellValue("Col"); c6.setCellStyle(estiloCabecalhoColunaP); //CRIA CLULA 7 NA LINHA 3 XSSFCell c7 = row3.createCell(contadorCelulasCabecalho++); c7.setCellValue("CI"); c7.setCellStyle(estiloCabecalhoColunaP); //CRIA CLULA 8 NA LINHA 3 XSSFCell c8 = row3.createCell(contadorCelulasCabecalho++); c8.setCellValue("CI Cum"); c8.setCellStyle(estiloCabecalhoColunaP); } //VARI?VEIS INICIAIS DA ELABORAO DO RELATRIO int quantidadeCiclosTotais = (jogadasAux.size() / experimento.getTamanhoFilaJogadores()); int contRowJogadas = 4; int contadorCiclo = 1; //INICIA LISTA DOS JOGADORES POR ORDEM List<Jogador> jogadoresAtuais = new ArrayList<>(); int contOrdem = 1; for (int i = 0; i < quantidadeJogadoresPorCiclo; i++) { for (Jogador jgdr : jogadoresTotais) { if (jgdr.getOrdem() == contOrdem) { jogadoresAtuais.add(jgdr); contOrdem++; break; } } } //FAZ A REMOO DO(S) ELEMENTO(S) DA LISTA PRA POUPAR RECURSO EM BUSCA FUTURA jogadoresTotais.removeAll(jogadoresAtuais); //CRIA INSTNCIA CONTROLE DE PONTUAO CULTURAL int pontCulturalCiclo; //FOR (JOGADOR : JOGADORES POR CICLO) //CRIA LISTA DE CADA JOGADOR AT FIM DO CICLO for (int i = 0; i < quantidadeCiclosTotais; i++) { //PEGA JOGADAS DO CICLO List<Jogada> jogadasCiclo = new ArrayList<>(); for (Jogada jogada : jogadasAux) { if (jogada.getRodada() == i + 1) { jogadasCiclo.add(jogada); if (jogadasCiclo.size() == quantidadeJogadoresPorCiclo) { break; } } } //FAZ A REMOO DO(S) ELEMENTO(S) DA LISTA PRA POUPAR RECURSO EM BUSCA FUTURA jogadasAux.removeAll(jogadasCiclo); //VERIFICA SE A ORDEM MUDOU int contJogadoresIguais = 0; List<Jogada> jogadasARemover = new ArrayList<>(); for (Jogador jogador : jogadoresAtuais) { for (Jogada jogada : jogadasCiclo) { if (jogada.getJogador().compareTo(jogador) == 0) { jogador.setUltimaJogada(jogada); jogador.incrementaPontuacaoRelatorio(); jogadasARemover.add(jogada); contJogadoresIguais++; } } } jogadasCiclo.removeAll(jogadasARemover); boolean mudouGeracaoCiclo = false; if (contJogadoresIguais == quantidadeJogadoresPorCiclo) { //CONTINUA COM OS MESMOS JOGADORES } else { mudouGeracaoCiclo = true; Jogador jogadorARemover = new Jogador(); jogadoresAtuais.remove(0); for (Jogador jgdr : jogadoresTotais) { if (jgdr.getOrdem() == contOrdem) { //PEGA A PRIMEIRA POSIO PQ ESSA TEM QUE SER A NICA COM ELEMENTO jgdr.setUltimaJogada(jogadasCiclo.get(0)); jgdr.incrementaPontuacaoRelatorio(); jogadoresAtuais.add(jgdr); contOrdem++; jogadorARemover = jgdr; break; } } jogadoresTotais.remove(jogadorARemover); } // --- ENCERRA ETAPAS DE VERIFICAO, INICIA A POPULAO DE NOVA LINHA DO XLSX E //VERIFICA SE EXISTE PONTUAO CULTURAL. int contCellJogadas = 1; if (jogadoresAtuais.get(0).getUltimaJogada().getPontuacaoCultural() != 0) { pontCulturalCiclo = jogadoresAtuais.get(0).getUltimaJogada().getPontuacaoCultural(); contadorAcertosCultural++; } else { pontCulturalCiclo = 0; } XSSFRow row = (XSSFRow) sheet.createRow((short) contRowJogadas); XSSFCell cell = row.createCell(contCellJogadas++); //CICLO if (contRowJogadas == 4 || mudouGeracaoCiclo == true) { //QUANDO HOUVER MUDANA DA GERAO cell.setCellValue(contadorCiclo++); cell.setCellStyle(estiloMudancaCiclo); } else { cell.setCellValue(contadorCiclo++); cell.setCellStyle(estiloCicloSemMudanca); } for (int j = 0; j < quantidadeJogadoresPorCiclo; j++) { //P XSSFCell cell1 = row.createCell(contCellJogadas++); cell1.setCellValue(jogadoresAtuais.get(j).getNome()); cell1.setCellStyle(estiloNomeP); //Linha XSSFCell cell2 = row.createCell(contCellJogadas++); cell2.setCellValue(jogadoresAtuais.get(j).getUltimaJogada().getLinhaSelecionada()); cell2.setCellStyle(estiloColunaLinha); //Cor XSSFCell cell3 = row.createCell(contCellJogadas++); cell3.setCellValue(jogadoresAtuais.get(j).getUltimaJogada().getCorSelecionada()); cell3.setCellStyle(EstiloCelula.retornaEstilo( jogadoresAtuais.get(j).getUltimaJogada().getCorSelecionada(), wb, fonteBranca, fonteNegra)); //Col XSSFCell cell4 = row.createCell(contCellJogadas++); cell4.setCellValue(jogadoresAtuais.get(j).getUltimaJogada().getColunaSelecionada()); cell4.setCellStyle(estiloColunaColuna); //CI XSSFCell cell5 = row.createCell(contCellJogadas++); cell5.setCellValue(jogadoresAtuais.get(j).getUltimaJogada().getPontuacaoIndividual()); cell5.setCellStyle(estiloColunaColuna); //CI Cum XSSFCell cell6 = row.createCell(contCellJogadas++); cell6.setCellValue(jogadoresAtuais.get(j).getPontuacaoExibidaRelatorio()); cell6.setCellStyle(estiloColunaColuna); } //CC XSSFCell cell7 = row.createCell(contCellJogadas++); cell7.setCellValue(pontCulturalCiclo); cell7.setCellStyle(estiloCabecalhoColunaAB); // ESTABILIDADE XSSFCell cell8 = row.createCell(contCellJogadas); cell8.setCellValue((contadorAcertosCultural * 100) / (i + 1) + "%"); cell8.setCellStyle(estiloColunaColuna); contRowJogadas++; } //ESCREVE O ARQUIVO byte[] bytes; try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { wb.write(out); bytes = out.toByteArray(); } return new ByteArrayInputStream(bytes); }
From source file:campmanager.CampUI.java
public void exportToExcel() { JFrame parentFrame = new JFrame(); File fileToSave = null;//w ww .j av a 2 s .c o m ; JFileChooser fileChooser = new JFileChooser(); fileChooser.setDialogTitle("Export to Excel"); int userSelection = fileChooser.showSaveDialog(parentFrame); if (userSelection == JFileChooser.APPROVE_OPTION) { fileToSave = fileChooser.getSelectedFile(); // System.out.println("Save as file: " + fileToSave.getAbsolutePath()); } if (fileToSave != null) { String fileName = fileToSave.getAbsolutePath(); if (!fileName.endsWith(".xls")) fileName += ".xls"; try { DefaultTableModel dtm = (DefaultTableModel) jTable_records.getModel(); Workbook wb = new HSSFWorkbook(); CreationHelper createhelper = wb.getCreationHelper(); Sheet sheet = wb.createSheet("new sheet"); Row row = null; Cell cell = null; row = sheet.createRow(0); HSSFFont font = (HSSFFont) wb.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); HSSFCellStyle style = (HSSFCellStyle) wb.createCellStyle(); style.setFont(font); for (int t = 0; t < dtm.getColumnCount() - 1; t++) { cell = row.createCell(t); cell.setCellStyle(style); cell.setCellValue(dtm.getColumnName(t)); } HSSFCellStyle style_gray = (HSSFCellStyle) wb.createCellStyle(); style_gray.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex()); // style_gray.setFillPattern(CellStyle.ALT_BARS); for (int i = 1; i <= dtm.getRowCount(); i++) { row = sheet.createRow(i); for (int j = 0; j < dtm.getColumnCount() - 1; j++) { cell = row.createCell(j); if (i % 2 == 0) cell.setCellStyle(style_gray); cell.setCellValue(dtm.getValueAt(i - 1, j).toString()); } } FileOutputStream out = new FileOutputStream(fileName); wb.write(out); out.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } }
From source file:ch.bfh.lca._15h.library.export.ExportToExcel.java
/*** * Generate an Excel file based on a list of results. * Use the name of the fields described in the GenericResultRow to label the columns. * @param language Language of the label in the Excel file * @param sheetTitle Title of the sheet in the Excel file * @param tableTitle Title of the table in the Excel file * @param rows Arrays of rows to include in the listing * @param excelFilePath Path of the outputed file * @throws FileNotFoundException/*from ww w . j ava2 s. c o m*/ * @throws IOException */ public static void exportToExcel(Translation.TRANSLATION_LANGUAGE language, String sheetTitle, String tableTitle, GenericResultRow[] rows, String excelFilePath) throws FileNotFoundException, IOException { //Workbook wb = new HSSFWorkbook(); //xls Workbook wb = new SXSSFWorkbook(); //xlsx //create new sheet Sheet sheet1 = wb.createSheet(sheetTitle); Row row; Cell cell; String translation; int rowIndex = 0; //add title row = sheet1.createRow((short) rowIndex); cell = row.createCell(0); cell.setCellValue(tableTitle); CellStyle style = wb.createCellStyle(); Font font = wb.createFont(); font.setFontHeightInPoints((short) 24); //font.setFontName("Courier New"); style.setFont(font); cell.setCellStyle(style); //add rows for (int i = 0; i < rows.length; i++) { //if first line, write col names if (i == 0) { row = sheet1.createRow((short) rowIndex + 2); for (int j = 0; j < rows[i].getColNames().length; j++) { cell = row.createCell(j); //look for translation translation = Translation.getForKey(language, rows[i].getColNames()[j]); if (translation == null) { translation = rows[i].getColNames()[j]; //if doesn't found a translation for the column take name of col } cell.setCellValue(translation); } } row = sheet1.createRow((short) (rowIndex + i + 3)); for (int j = 0; j < rows[i].getColNames().length; j++) { cell = row.createCell(j); cell.setCellValue(rows[i].getValueAt(j).toString()); } } //write to the file FileOutputStream fileOut = new FileOutputStream(excelFilePath); wb.write(fileOut); fileOut.close(); wb.close(); }
From source file:cn.afterturn.easypoi.excel.imports.ExcelImportService.java
License:Apache License
private void createErrorCellStyle(Workbook workbook) { errorCellStyle = workbook.createCellStyle(); Font font = workbook.createFont(); font.setColor(Font.COLOR_RED); errorCellStyle.setFont(font);//ww w . jav a 2s .co m }
From source file:cn.com.zhbook.component.poi.PoiPerformanceTest.java
License:Apache License
static Map<String, CellStyle> createStyles(Workbook wb) { Map<String, CellStyle> styles = new HashMap<String, CellStyle>(); CellStyle style;//from w w w . ja v a 2 s . c om Font headerFont = wb.createFont(); headerFont.setFontHeightInPoints((short) 14); headerFont.setBold(true); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER); style.setFont(headerFont); style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); styles.put("header", style); Font monthFont = wb.createFont(); monthFont.setFontHeightInPoints((short) 12); monthFont.setColor(IndexedColors.RED.getIndex()); monthFont.setBold(true); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER); style.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setFont(monthFont); styles.put("red-bold", style); String[] nfmt = { "#,##0.00", "$#,##0.00", "m/d/yyyy" }; for (String fmt : nfmt) { style = wb.createCellStyle(); style.setDataFormat(wb.createDataFormat().getFormat(fmt)); styles.put(fmt, style); } return styles; }
From source file:com.actelion.research.spiritapp.ui.util.POIUtils.java
License:Open Source License
@SuppressWarnings("rawtypes") public static void exportToExcel(String[][] table, ExportMode exportMode) throws IOException { Class[] types = getTypes(table); Workbook wb = new XSSFWorkbook(); Map<String, CellStyle> styles = new HashMap<String, CellStyle>(); CellStyle style;/*from ww w . ja va2 s . c o m*/ DataFormat df = wb.createDataFormat(); Font font = wb.createFont(); font.setFontName("Serif"); font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setFontHeightInPoints((short) 15); style = wb.createCellStyle(); style.setFont(font); styles.put("title", style); font = wb.createFont(); font.setFontName("Serif"); font.setFontHeightInPoints((short) 10); style = wb.createCellStyle(); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderTop(CellStyle.BORDER_THIN); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); style.setAlignment(CellStyle.ALIGN_CENTER); style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setFont(font); style.setWrapText(true); style.setVerticalAlignment(CellStyle.VERTICAL_TOP); styles.put("th", style); font = wb.createFont(); font.setFontName("Serif"); font.setFontHeightInPoints((short) 9); style = wb.createCellStyle(); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setAlignment(CellStyle.ALIGN_LEFT); style.setFont(font); style.setWrapText(true); style.setVerticalAlignment(CellStyle.VERTICAL_TOP); styles.put("td", style); font = wb.createFont(); font.setFontName("Serif"); font.setFontHeightInPoints((short) 9); style = wb.createCellStyle(); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderTop(CellStyle.BORDER_THIN); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setAlignment(CellStyle.ALIGN_LEFT); style.setFont(font); style.setWrapText(true); style.setVerticalAlignment(CellStyle.VERTICAL_TOP); styles.put("td-border", style); font = wb.createFont(); font.setFontName("Serif"); font.setFontHeightInPoints((short) 9); style = wb.createCellStyle(); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setAlignment(CellStyle.ALIGN_RIGHT); style.setFont(font); style.setVerticalAlignment(CellStyle.VERTICAL_TOP); styles.put("td-double", style); font = wb.createFont(); font.setFontName("Serif"); font.setFontHeightInPoints((short) 9); style = wb.createCellStyle(); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setAlignment(CellStyle.ALIGN_RIGHT); style.setFont(font); style.setVerticalAlignment(CellStyle.VERTICAL_TOP); styles.put("td-right", style); font = wb.createFont(); font.setFontName("Serif"); font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setFontHeightInPoints((short) 9); style = wb.createCellStyle(); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setAlignment(CellStyle.ALIGN_RIGHT); style.setFont(font); style.setVerticalAlignment(CellStyle.VERTICAL_TOP); styles.put("td-bold", style); font = wb.createFont(); font.setFontName("Serif"); font.setFontHeightInPoints((short) 9); style = wb.createCellStyle(); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setAlignment(CellStyle.ALIGN_RIGHT); style.setFont(font); style.setDataFormat(df.getFormat("d.mm.yyyy h:MM")); style.setVerticalAlignment(CellStyle.VERTICAL_TOP); styles.put("td-date", style); Sheet sheet = wb.createSheet(); sheet.setFitToPage(true); Cell cell; int maxRows = 0; for (int r = 0; r < table.length; r++) { Row row = sheet.createRow(r); if (r == 0) { row.setRowStyle(styles.get("th")); } int rows = 1; for (int c = 0; c < table[r].length; c++) { cell = row.createCell(c); String s = table[r][c]; if (s == null) continue; rows = Math.max(rows, s.split("\n").length); try { if (exportMode == ExportMode.HEADERS_TOP && r == 0) { cell.setCellStyle(styles.get("th")); cell.setCellValue(s); } else if (exportMode == ExportMode.HEADERS_TOPLEFT && (r == 0 || c == 0)) { if (r == 0 && c == 0) { cell.setCellStyle(styles.get("td")); } else { cell.setCellStyle(styles.get("th")); } cell.setCellValue(s); } else if (types[c] == Double.class) { cell.setCellStyle(styles.get("td-double")); cell.setCellType(Cell.CELL_TYPE_NUMERIC); cell.setCellValue(Double.parseDouble(s)); } else if (types[c] == String.class) { cell.setCellStyle( styles.get(exportMode == ExportMode.HEADERS_TOPLEFT ? "td-border" : "td")); cell.setCellType(Cell.CELL_TYPE_NUMERIC); cell.setCellValue(s); } else { cell.setCellStyle(styles.get("td-right")); cell.setCellValue(s); } } catch (Exception e) { cell.setCellStyle(styles.get("td")); cell.setCellValue(s); } } maxRows = Math.max(maxRows, rows); row.setHeightInPoints(rows * 16f); } // Add footer notes if (footerData.size() > 0) { Row row = sheet.createRow(table.length); row.setHeightInPoints((footerData.size() * sheet.getDefaultRowHeightInPoints())); cell = row.createCell(0); sheet.addMergedRegion(new CellRangeAddress(row.getRowNum(), //first row (0-based) row.getRowNum(), //last row (0-based) 0, //first column (0-based) table[0].length - 1 //last column (0-based) )); //for ( String data : footerData ) { style = wb.createCellStyle(); style.setWrapText(true); cell.setCellStyle(style); cell.setCellValue(MiscUtils.flatten(footerData, "\n")); //} } footerData.clear(); autoSizeColumns(sheet); if (table.length > 0) { for (int c = 0; c < table[0].length; c++) { if (sheet.getColumnWidth(c) > 10000) sheet.setColumnWidth(c, 3000); } } if (exportMode == ExportMode.HEADERS_TOPLEFT) { for (int r = 1; r < table.length; r++) { sheet.getRow(r).setHeightInPoints(maxRows * 16f); } } File reportFile = IOUtils.createTempFile("export_", ".xlsx"); FileOutputStream out = new FileOutputStream(reportFile); wb.write(out); wb.close(); out.close(); Desktop.getDesktop().open(reportFile); }
From source file:com.adobe.acs.commons.mcp.impl.ProcessErrorReportExcelServlet.java
License:Apache License
CellStyle createHeaderStyle(Workbook wb) { XSSFCellStyle xstyle = (XSSFCellStyle) wb.createCellStyle(); XSSFColor header = new XSSFColor(new Color(79, 129, 189)); xstyle.setFillForegroundColor(header); xstyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); XSSFFont font = (XSSFFont) wb.createFont(); font.setColor(IndexedColors.WHITE.index); xstyle.setFont(font);/*from w w w. j a va 2s .c o m*/ return xstyle; }
From source file:com.aistor.common.utils.excel.ExportExcel.java
License:Open Source License
/** * ?//www . j a v a 2s .c o m * @param wb * @return ? */ private Map<String, CellStyle> createStyles(Workbook wb) { Map<String, CellStyle> styles = new HashMap<String, CellStyle>(); CellStyle style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); Font titleFont = wb.createFont(); titleFont.setFontHeightInPoints((short) 18); titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD); style.setFont(titleFont); styles.put("title", style); style = wb.createCellStyle(); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setBorderTop(CellStyle.BORDER_THIN); style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); styles.put("data", style); style = wb.createCellStyle(); style.cloneStyleFrom(styles.get("data")); // style.setWrapText(true); style.setAlignment(CellStyle.ALIGN_CENTER); style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); Font headerFont = wb.createFont(); headerFont.setFontHeightInPoints((short) 11); headerFont.setColor(IndexedColors.WHITE.getIndex()); style.setFont(headerFont); styles.put("header", style); return styles; }
From source file:com.AllenBarr.CallSheetGenerator.Generator.java
License:Open Source License
public int generateSheet(File file, Contributor contrib) { //create workbook file final String fileName = file.toString(); final Workbook wb; if (fileName.endsWith(".xlsx")) { wb = new XSSFWorkbook(); } else if (fileName.endsWith(".xls")) { wb = new HSSFWorkbook(); } else {//from www. j a v a 2s . c o m return 1; } //create sheet final Sheet sheet = wb.createSheet("Call Sheet"); final Header header = sheet.getHeader(); header.setCenter("Anderson for Iowa Call Sheet"); //add empty cells final Row[] row = new Row[22 + contrib.getDonationsLength()]; final Cell[][] cell = new Cell[6][22 + contrib.getDonationsLength()]; for (int i = 0; i < (22 + contrib.getDonationsLength()); i++) { row[i] = sheet.createRow((short) i); for (int j = 0; j < 6; j++) { cell[j][i] = row[i].createCell(j); } } //populate cells with data //column 1 cell[0][0].setCellValue(contrib.getName()); cell[0][3].setCellValue("Sex:"); cell[0][4].setCellValue("Party:"); cell[0][5].setCellValue("Phone #:"); cell[0][6].setCellValue("Home #:"); cell[0][7].setCellValue("Cell #:"); cell[0][8].setCellValue("Work #:"); cell[0][10].setCellValue("Email:"); cell[0][12].setCellValue("Employer:"); cell[0][13].setCellValue("Occupation:"); cell[0][15].setCellValue("Past Contact:"); cell[0][17].setCellValue("Notes:"); cell[0][21].setCellValue("Contribution History:"); //column 2 cell[1][3].setCellValue(contrib.getSex()); cell[1][4].setCellValue(contrib.getParty()); cell[1][5].setCellValue(contrib.getPhone()); cell[1][6].setCellValue(contrib.getHomePhone()); cell[1][7].setCellValue(contrib.getCellPhone()); cell[1][8].setCellValue(contrib.getWorkPhone()); cell[1][9].setCellValue("x" + contrib.getWorkExtension()); cell[1][10].setCellValue(contrib.getEmail()); cell[1][12].setCellValue(contrib.getEmployer()); cell[1][13].setCellValue(contrib.getOccupation()); cell[1][17].setCellValue(contrib.getNotes()); //column 4 cell[3][3].setCellValue("Salutation:"); cell[3][4].setCellValue("Age:"); cell[3][5].setCellValue("Spouse:"); cell[3][7].setCellValue("Address:"); cell[3][10].setCellValue("TARGET:"); //column 5 cell[4][0].setCellValue("VANID:"); cell[4][3].setCellValue(contrib.getSalutation()); cell[4][4].setCellValue(contrib.getAge()); cell[4][5].setCellValue(contrib.getSpouse()); cell[4][7].setCellValue(contrib.getStreetAddress()); cell[4][8].setCellValue(contrib.getCity() + ", " + contrib.getState() + " " + contrib.getZip()); //column 6 cell[5][0].setCellValue(contrib.getVANID()); //contribution cells for (int i = 0; i < contrib.getDonationsLength(); i++) { cell[0][i + 22].setCellValue(contrib.getDonation(i).getDonationDate()); cell[1][i + 22].setCellValue(contrib.getDonation(i).getRecipient()); cell[5][i + 22].setCellValue(contrib.getDonation(i).getAmount()); } //format cells //Name cell sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3)); final CellStyle leftBoldUnderline14Style = wb.createCellStyle(); final Font boldUnderline14Font = wb.createFont(); boldUnderline14Font.setBoldweight(Font.BOLDWEIGHT_BOLD); boldUnderline14Font.setUnderline(Font.U_SINGLE); boldUnderline14Font.setFontHeightInPoints((short) 14); boldUnderline14Font.setFontName("Garamond"); leftBoldUnderline14Style.setFont(boldUnderline14Font); leftBoldUnderline14Style.setAlignment(CellStyle.ALIGN_LEFT); cell[0][0].setCellStyle(leftBoldUnderline14Style); //field name cells final CellStyle rightBold10Style = wb.createCellStyle(); final Font bold10Font = wb.createFont(); bold10Font.setBoldweight(Font.BOLDWEIGHT_BOLD); bold10Font.setFontHeightInPoints((short) 10); bold10Font.setFontName("Garamond"); rightBold10Style.setFont(bold10Font); rightBold10Style.setAlignment(CellStyle.ALIGN_RIGHT); for (int i = 3; i < 22; i++) { cell[0][i].setCellStyle(rightBold10Style); } sheet.addMergedRegion(new CellRangeAddress(21, 21, 0, 1)); for (int i = 3; i < 11; i++) { cell[3][i].setCellStyle(rightBold10Style); } cell[4][0].setCellStyle(rightBold10Style); //field content cells final CellStyle left10Style = wb.createCellStyle(); final Font garamond10Font = wb.createFont(); garamond10Font.setFontHeightInPoints((short) 10); garamond10Font.setFontName("Garamond"); left10Style.setFont(garamond10Font); left10Style.setAlignment(CellStyle.ALIGN_LEFT); for (int i = 3; i < 5; i++) { cell[1][i].setCellStyle(left10Style); } //phone number cells final CellStyle phoneStyle = wb.createCellStyle(); phoneStyle.setFont(garamond10Font); phoneStyle.setAlignment(CellStyle.ALIGN_LEFT); final CreationHelper createHelper = wb.getCreationHelper(); phoneStyle.setDataFormat(createHelper.createDataFormat().getFormat("[<=9999999]###-####;(###) ###-####")); for (int i = 5; i < 9; i++) { cell[1][i].setCellStyle(phoneStyle); sheet.addMergedRegion(new CellRangeAddress(i, i, 1, 2)); } cell[1][9].setCellStyle(left10Style); //email through past contact for (int i = 10; i < 16; i++) { cell[1][i].setCellStyle(left10Style); } //notes CellStyle noteStyle = wb.createCellStyle(); noteStyle.cloneStyleFrom(left10Style); noteStyle.setWrapText(true); cell[1][17].setCellStyle(noteStyle); //column E for (int i = 3; i < 11; i++) { cell[4][i].setCellStyle(left10Style); } //VanID Cell final CellStyle right10Style = wb.createCellStyle(); right10Style.setFont(garamond10Font); right10Style.setAlignment(CellStyle.ALIGN_RIGHT); cell[5][0].setCellStyle(right10Style); //Notes cell sheet.addMergedRegion(new CellRangeAddress(17, 19, 1, 5)); //contribution cells final CellStyle date10Style = wb.createCellStyle(); date10Style.setFont(garamond10Font); date10Style.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy")); date10Style.setBorderBottom(CellStyle.BORDER_THIN); date10Style.setBorderTop(CellStyle.BORDER_THIN); date10Style.setBorderLeft(CellStyle.BORDER_THIN); date10Style.setBorderRight(CellStyle.BORDER_THIN); final CellStyle contributionStyle = wb.createCellStyle(); contributionStyle.cloneStyleFrom(left10Style); contributionStyle.setBorderBottom(CellStyle.BORDER_THIN); contributionStyle.setBorderTop(CellStyle.BORDER_THIN); contributionStyle.setBorderLeft(CellStyle.BORDER_THIN); contributionStyle.setBorderRight(CellStyle.BORDER_THIN); final CellStyle money10Style = wb.createCellStyle(); money10Style.setFont(garamond10Font); money10Style.setDataFormat( createHelper.createDataFormat().getFormat("_($* #,##0.00_);_($* (#,##0.00);_($* \"-\"??_);_(@_)")); money10Style.setBorderBottom(CellStyle.BORDER_THIN); money10Style.setBorderTop(CellStyle.BORDER_THIN); money10Style.setBorderLeft(CellStyle.BORDER_THIN); money10Style.setBorderRight(CellStyle.BORDER_THIN); for (int i = 22; i < 22 + contrib.getDonationsLength(); i++) { cell[0][i].setCellStyle(date10Style); cell[1][i].setCellStyle(contributionStyle); cell[2][i].setCellStyle(contributionStyle); cell[3][i].setCellStyle(contributionStyle); cell[4][i].setCellStyle(contributionStyle); sheet.addMergedRegion(new CellRangeAddress(i, i, 1, 4)); cell[5][i].setCellStyle(money10Style); } //resize columns sheet.autoSizeColumn(0); sheet.autoSizeColumn(1); try { FileOutputStream fileOut = new FileOutputStream(file); wb.write(fileOut); fileOut.close(); } catch (FileNotFoundException e) { return 1; } catch (IOException ex) { return 1; } return 0; }