List of usage examples for org.apache.poi.ss.usermodel Sheet createRow
Row createRow(int rownum);
From source file:cl.a2r.wsmicampov2.common.utils.Excel.java
public static void GenerateExcel(String fileUrl, String sheetName, List<Integer> dataList) { File archivo = new File(fileUrl); Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet(sheetName); int rowNum = 1; for (Integer data : dataList) { Row row = sheet.createRow(rowNum++); row.createCell(0).setCellValue(data); }// w ww . j av a 2s .c o m try { FileOutputStream salida = new FileOutputStream(archivo); workbook.write(salida); workbook.close(); } catch (FileNotFoundException ex) { AppLog.logInfo(ex.getMessage(), ex); } catch (IOException ex) { AppLog.logInfo(ex.getMessage(), ex); } catch (Exception ex) { AppLog.logInfo(ex.getMessage(), ex); } }
From source file:clases.Funciones.java
private String GenerateReporte_xls(JTable t, int open) { String rutaArchivo = ""; try {/*from w ww . ja v a 2s. co m*/ char rt = p.ReturnPropiedad(p.Ruta_SaveReports) .charAt(p.ReturnPropiedad(p.Ruta_SaveReports).length() - 1); if ("/".equalsIgnoreCase(String.valueOf(rt))) { rutaArchivo = p.ReturnPropiedad(p.Ruta_SaveReports) + ReturnNombreUsuario().replace(" ", "_") + rutaArchivo + "_" + GetFechaAndHourActual().replace(":", "_").replace(" ", "_").replace("-", "_") + ".xls"; } else { rutaArchivo = p.ReturnPropiedad(p.Ruta_SaveReports) + "/" + ReturnNombreUsuario().replace(" ", "_") + rutaArchivo + "_" + GetFechaAndHourActual().replace(":", "_").replace(" ", "_").replace("-", "_") + ".xls"; } File archivoXLS = new File(rutaArchivo); if (archivoXLS.exists()) archivoXLS.delete(); archivoXLS.createNewFile(); Workbook libro = new HSSFWorkbook(); FileOutputStream archivo = new FileOutputStream(archivoXLS); Sheet hoja = libro.createSheet(ReturnDatosFisicos(this.Datos_Nombre)); for (int f = 0; f < t.getRowCount() + 1; f++) { Row fila = hoja.createRow(f); for (int c = 0; c < t.getColumnCount(); c++) { Cell celda = fila.createCell(c); if (f == 0) { celda.setCellValue(String.valueOf(t.getColumnName(c))); } else { celda.setCellValue(String.valueOf(t.getValueAt(f - 1, c))); } hoja.autoSizeColumn(c); } } libro.write(archivo); archivo.close(); if (open > 0) { Desktop.getDesktop().open(archivoXLS); } } catch (IOException ex) { Alert(ex.getMessage()); } return rutaArchivo; }
From source file:Clientes.editaCliente.java
private void bt_actualiza2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_actualiza2ActionPerformed // TODO add your handling code here: javax.swing.JFileChooser jF1 = new javax.swing.JFileChooser(); jF1.setFileFilter(new ExtensionFileFilter("Excel document (*.xls)", new String[] { "xls" })); String ruta = null;/*w w w . j a v a 2s .com*/ if (jF1.showSaveDialog(null) == jF1.APPROVE_OPTION) { ruta = jF1.getSelectedFile().getAbsolutePath(); if (ruta != null) { File archivoXLS = new File(ruta + ".xls"); try { if (archivoXLS.exists()) archivoXLS.delete(); archivoXLS.createNewFile(); Workbook libro = new HSSFWorkbook(); FileOutputStream archivo = new FileOutputStream(archivoXLS); Sheet hoja = libro.createSheet("Clientes"); for (int ren = 0; ren < (t_datos.getRowCount() + 1); ren++) { Row fila = hoja.createRow(ren); for (int col = 0; col < t_datos.getColumnCount(); col++) { Cell celda = fila.createCell(col); if (ren == 0) { celda.setCellValue(columnas[col]); } else { try { celda.setCellValue(t_datos.getValueAt(ren - 1, col).toString()); } catch (Exception e) { celda.setCellValue(""); } } } } libro.write(archivo); archivo.close(); Desktop.getDesktop().open(archivoXLS); } catch (Exception e) { System.out.println(e); JOptionPane.showMessageDialog(this, "No se pudo realizar el reporte"); } } } }
From source file:cn.afterturn.easypoi.excel.export.base.BaseExportService.java
License:Apache License
/** * ? Cells//from w ww . j av a 2 s. c o m */ public int[] createCells(Drawing patriarch, int index, Object t, List<ExcelExportEntity> excelParams, Sheet sheet, Workbook workbook, short rowHeight, int cellNum) { try { ExcelExportEntity entity; Row row = sheet.getRow(index) == null ? sheet.createRow(index) : sheet.getRow(index); if (rowHeight != -1) { row.setHeight(rowHeight); } int maxHeight = 1, listMaxHeight = 1; // ???? int margeCellNum = cellNum; int indexKey = createIndexCell(row, index, excelParams.get(0)); cellNum += indexKey; for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { entity = excelParams.get(k); if (entity.getList() != null) { Collection<?> list = getListCellValue(entity, t); int listIndex = 0, tmpListHeight = 0; if (list != null && list.size() > 0) { int tempCellNum = 0; for (Object obj : list) { int[] temp = createCells(patriarch, index + listIndex, obj, entity.getList(), sheet, workbook, rowHeight, cellNum); tempCellNum = temp[1]; tmpListHeight += temp[0]; listIndex++; } cellNum = tempCellNum; listMaxHeight = Math.max(listMaxHeight, tmpListHeight); } } else { Object value = getCellValue(entity, t); if (entity.getType() == BaseEntityTypeConstants.STRING_TYPE) { createStringCell(row, cellNum++, value == null ? "" : value.toString(), index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity); } else if (entity.getType() == BaseEntityTypeConstants.DOUBLE_TYPE) { createDoubleCell(row, cellNum++, value == null ? "" : value.toString(), index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity); } else { createImageCell(patriarch, entity, row, cellNum++, value == null ? "" : value.toString(), t); } if (entity.isHyperlink()) { row.getCell(cellNum - 1).setHyperlink(dataHandler.getHyperlink( row.getSheet().getWorkbook().getCreationHelper(), t, entity.getName(), value)); } } } maxHeight += listMaxHeight - 1; if (indexKey == 1 && excelParams.get(1).isNeedMerge()) { excelParams.get(0).setNeedMerge(true); } for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { entity = excelParams.get(k); if (entity.getList() != null) { margeCellNum += entity.getList().size(); } else if (entity.isNeedMerge() && maxHeight > 1) { for (int i = index + 1; i < index + maxHeight; i++) { if (sheet.getRow(i) == null) { sheet.createRow(i); } sheet.getRow(i).createCell(margeCellNum); sheet.getRow(i).getCell(margeCellNum).setCellStyle(getStyles(false, entity)); } PoiMergeCellUtil.addMergedRegion(sheet, index, index + maxHeight - 1, margeCellNum, margeCellNum); margeCellNum++; } } return new int[] { maxHeight, cellNum }; } catch (Exception e) { LOGGER.error("excel cell export error ,data is :{}", ReflectionToStringBuilder.toString(t)); LOGGER.error(e.getMessage(), e); throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e); } }
From source file:cn.afterturn.easypoi.excel.export.base.BaseExportService.java
License:Apache License
/** * List??Cells// w w w. j a va2s . c om */ public void createListCells(Drawing patriarch, int index, int cellNum, Object obj, List<ExcelExportEntity> excelParams, Sheet sheet, Workbook workbook, short rowHeight) throws Exception { ExcelExportEntity entity; Row row; if (sheet.getRow(index) == null) { row = sheet.createRow(index); if (rowHeight != -1) { row.setHeight(rowHeight); } } else { row = sheet.getRow(index); if (rowHeight != -1) { row.setHeight(rowHeight); } } for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { entity = excelParams.get(k); Object value = getCellValue(entity, obj); if (entity.getType() == BaseEntityTypeConstants.STRING_TYPE) { createStringCell(row, cellNum++, value == null ? "" : value.toString(), row.getRowNum() % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity); if (entity.isHyperlink()) { row.getCell(cellNum - 1).setHyperlink(dataHandler.getHyperlink( row.getSheet().getWorkbook().getCreationHelper(), obj, entity.getName(), value)); } } else if (entity.getType() == BaseEntityTypeConstants.DOUBLE_TYPE) { createDoubleCell(row, cellNum++, value == null ? "" : value.toString(), index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity); if (entity.isHyperlink()) { row.getCell(cellNum - 1).setHyperlink(dataHandler.getHyperlink( row.getSheet().getWorkbook().getCreationHelper(), obj, entity.getName(), value)); } } else { createImageCell(patriarch, entity, row, cellNum++, value == null ? "" : value.toString(), obj); } } }
From source file:cn.afterturn.easypoi.excel.export.base.BaseExportService.java
License:Apache License
/** * //from w ww. j a v a 2 s . c om */ public void addStatisticsRow(CellStyle styles, Sheet sheet) { if (statistics.size() > 0) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("add statistics data ,size is {}", statistics.size()); } Row row = sheet.createRow(sheet.getLastRowNum() + 1); Set<Integer> keys = statistics.keySet(); createStringCell(row, 0, "?", styles, null); for (Integer key : keys) { createStringCell(row, key, DOUBLE_FORMAT.format(statistics.get(key)), styles, null); } statistics.clear(); } }
From source file:cn.afterturn.easypoi.excel.export.ExcelExportService.java
License:Apache License
/** * /*from www . jav a2s. c o m*/ */ private int createHeaderRow(ExportParams title, Sheet sheet, Workbook workbook, int index, List<ExcelExportEntity> excelParams, int cellIndex) { Row row = sheet.getRow(index) == null ? sheet.createRow(index) : sheet.getRow(index); int rows = getRowNums(excelParams, true); row.setHeight(title.getHeaderHeight()); Row listRow = null; if (rows >= 2) { listRow = sheet.createRow(index + 1); listRow.setHeight(title.getHeaderHeight()); } int groupCellLength = 0; CellStyle titleStyle = getExcelExportStyler().getTitleStyle(title.getColor()); for (int i = 0, exportFieldTitleSize = excelParams.size(); i < exportFieldTitleSize; i++) { ExcelExportEntity entity = excelParams.get(i); // ?groupName??? if (StringUtils.isBlank(entity.getGroupName()) || i == 0 || !entity.getGroupName().equals(excelParams.get(i - 1).getGroupName())) { if (groupCellLength > 1) { sheet.addMergedRegion( new CellRangeAddress(index, index, cellIndex - groupCellLength, cellIndex - 1)); } groupCellLength = 0; } if (StringUtils.isNotBlank(entity.getGroupName())) { createStringCell(row, cellIndex, entity.getGroupName(), titleStyle, entity); createStringCell(listRow, cellIndex, entity.getName(), titleStyle, entity); groupCellLength++; } else if (StringUtils.isNotBlank(entity.getName())) { createStringCell(row, cellIndex, entity.getName(), titleStyle, entity); } if (entity.getList() != null) { // ??? int tempCellIndex = cellIndex; cellIndex = createHeaderRow(title, sheet, workbook, rows == 1 ? index : index + 1, entity.getList(), cellIndex); List<ExcelExportEntity> sTitel = entity.getList(); if (StringUtils.isNotBlank(entity.getName()) && sTitel.size() > 1) { PoiMergeCellUtil.addMergedRegion(sheet, index, index, tempCellIndex, tempCellIndex + sTitel.size() - 1); } /*for (int j = 0, size = sTitel.size(); j < size; j++) { createStringCell(rows == 2 ? listRow : row, cellIndex, sTitel.get(j).getName(), titleStyle, entity); cellIndex++; }*/ cellIndex--; } else if (rows > 1 && StringUtils.isBlank(entity.getGroupName())) { createStringCell(listRow, cellIndex, "", titleStyle, entity); PoiMergeCellUtil.addMergedRegion(sheet, index, index + rows - 1, cellIndex, cellIndex); } cellIndex++; } if (groupCellLength > 1) { PoiMergeCellUtil.addMergedRegion(sheet, index, index, cellIndex - groupCellLength, cellIndex - 1); } return cellIndex; }
From source file:cn.afterturn.easypoi.excel.export.ExcelExportService.java
License:Apache License
/** * ?//from w ww. java 2 s . c o m */ public int createTitle2Row(ExportParams entity, Sheet sheet, Workbook workbook, int fieldWidth) { Row row = sheet.createRow(0); row.setHeight(entity.getTitleHeight()); createStringCell(row, 0, entity.getTitle(), getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null); for (int i = 1; i <= fieldWidth; i++) { createStringCell(row, i, "", getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null); } PoiMergeCellUtil.addMergedRegion(sheet, 0, 0, 0, fieldWidth); if (entity.getSecondTitle() != null) { row = sheet.createRow(1); row.setHeight(entity.getSecondTitleHeight()); CellStyle style = workbook.createCellStyle(); style.setAlignment(HorizontalAlignment.RIGHT); createStringCell(row, 0, entity.getSecondTitle(), style, null); for (int i = 1; i <= fieldWidth; i++) { createStringCell(row, i, "", getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null); } PoiMergeCellUtil.addMergedRegion(sheet, 1, 1, 0, fieldWidth); return 2; } return 1; }
From source file:cn.afterturn.easypoi.excel.export.template.ExcelExportOfTemplateUtil.java
License:Apache License
/** * Row//w ww . j a va 2s .com * * @param sheet * @param rowIndex * @param isCreate * @param rows * @return */ private Row createRow(int rowIndex, Sheet sheet, boolean isCreate, int rows) { for (int i = 0; i < rows; i++) { if (isCreate) { sheet.createRow(rowIndex++); } else if (sheet.getRow(rowIndex++) == null) { sheet.createRow(rowIndex - 1); } } return sheet.getRow(rowIndex - rows); }
From source file:cn.bzvs.excel.export.base.ExcelExportBase.java
License:Apache License
/** * ? Cells/*from www . j a v a 2s. c o m*/ * * @param patriarch * @param index * @param t * @param excelParams * @param sheet * @param workbook * @param rowHeight * @return * @throws Exception */ public int createCells(Drawing patriarch, int index, Object t, List<ExcelExportEntity> excelParams, Sheet sheet, Workbook workbook, short rowHeight) throws Exception { ExcelExportEntity entity; Row row = sheet.createRow(index); row.setHeight(rowHeight); int maxHeight = 1, cellNum = 0; int indexKey = createIndexCell(row, index, excelParams.get(0)); cellNum += indexKey; for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { entity = excelParams.get(k); if (entity.getList() != null) { Collection<?> list = getListCellValue(entity, t); int listC = 0; if (list != null && list.size() > 0) { for (Object obj : list) { createListCells(patriarch, index + listC, cellNum, obj, entity.getList(), sheet, workbook); listC++; } } cellNum += entity.getList().size(); if (list != null && list.size() > maxHeight) { maxHeight = list.size(); } } else { Object value = getCellValue(entity, t); if (entity.getType() == BaseEntityTypeConstants.StringType) { createStringCell(row, cellNum++, value == null ? "" : value.toString(), index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity); if (entity.isHyperlink()) { row.getCell(cellNum - 1).setHyperlink(dataHanlder.getHyperlink( row.getSheet().getWorkbook().getCreationHelper(), t, entity.getName(), value)); } } else if (entity.getType() == BaseEntityTypeConstants.DoubleType) { createDoubleCell(row, cellNum++, value == null ? "" : value.toString(), index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity); if (entity.isHyperlink()) { row.getCell(cellNum - 1).setHyperlink(dataHanlder.getHyperlink( row.getSheet().getWorkbook().getCreationHelper(), t, entity.getName(), value)); } } else { createImageCell(patriarch, entity, row, cellNum++, value == null ? "" : value.toString(), t); } } } // ???? cellNum = 0; for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { entity = excelParams.get(k); if (entity.getList() != null) { cellNum += entity.getList().size(); } else if (entity.isNeedMerge()) { for (int i = index + 1; i < index + maxHeight; i++) { sheet.getRow(i).createCell(cellNum); sheet.getRow(i).getCell(cellNum).setCellStyle(getStyles(false, entity)); } sheet.addMergedRegion(new CellRangeAddress(index, index + maxHeight - 1, cellNum, cellNum)); cellNum++; } } return maxHeight; }