Last active
December 17, 2015 05:49
-
-
Save thaniaclair/5561209 to your computer and use it in GitHub Desktop.
Exportação de dados para arquivos da Microsoft (XLS, DOC), utilizando a API: Apache POI.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.List; | |
| import org.apache.poi.hssf.usermodel.HSSFCell; | |
| import org.apache.poi.hssf.usermodel.HSSFCellStyle; | |
| import org.apache.poi.hssf.usermodel.HSSFFont; | |
| import org.apache.poi.hssf.usermodel.HSSFRow; | |
| import org.apache.poi.hssf.usermodel.HSSFSheet; | |
| import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |
| import org.apache.poi.ss.usermodel.Font; | |
| /** | |
| * Exportação de dados para arquivos da Microsoft (XLS, DOC), utilizando a API: Apache POI. | |
| * @author thania.clair | |
| */ | |
| public class POIExport { | |
| private static final String DEFAULT_EXTENSION = ".xls"; | |
| private static final String DEFAULT_FILENAME = "exportacao"; | |
| private String filename; | |
| private String extension; | |
| private POITable table; | |
| private POIRow headerRow; | |
| private POIRow footerRow; | |
| private HSSFSheet sheet; | |
| private HSSFWorkbook workbook; | |
| private int startRow = 0; | |
| public POIExport(ExportDTO eDTO) { | |
| this.headerRow = eDTO.getHeaderRow(); | |
| this.footerRow = eDTO.getFooterRow(); | |
| this.table = eDTO.getTable(); | |
| this.extension = DEFAULT_EXTENSION; | |
| this.filename = DEFAULT_FILENAME; | |
| } | |
| /** | |
| * Construtor para export que possui modelo a ser seguido. | |
| * @param eDTO {@link ExportDTO}. | |
| * @param workbook {@link HSSFWorkbook}. | |
| */ | |
| public POIExport(ExportDTO eDTO, HSSFWorkbook workbook) { | |
| this(eDTO); | |
| this.workbook = workbook; | |
| this.startRow = eDTO.getStartRow(); | |
| } | |
| /** | |
| * Gera o documento com os dados a serem exportados. | |
| */ | |
| public void generateSheet() { | |
| if (workbook == null) { | |
| workbook = new HSSFWorkbook(); | |
| createSheet(); | |
| } else { | |
| this.sheet = workbook.getSheetAt(0); | |
| } | |
| createHeader(); | |
| createContent(); | |
| createFooter(); | |
| } | |
| /** | |
| * Cria e inicializa um documento de trabalho, com o nome do arquivo. | |
| */ | |
| private void createSheet() | |
| { | |
| sheet = workbook.createSheet(filename); | |
| int columns = table.getNumberOfColumns(); | |
| for (int idx = 0; idx < columns; idx++) | |
| sheet.setColumnWidth(idx, 6000); | |
| } | |
| /** | |
| * Cria o conteúdo do documento. | |
| */ | |
| private void createContent() | |
| { | |
| HSSFCellStyle centerStyle = workbook.createCellStyle(); | |
| centerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); | |
| centerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); | |
| int rowNumber; | |
| //verifica se há um valor inicial de espaçamento para as informações, | |
| //caso não possua verifica se possui header para tambem adicionar o espaçamento. | |
| if(startRow == 0) | |
| rowNumber = hasHeader() ? 1 : 0; | |
| else | |
| rowNumber = startRow; | |
| List<POIRow> rows = table.getRows(); | |
| for (POIRow row : rows) { | |
| HSSFRow r = sheet.createRow(rowNumber++); | |
| int colNumber = 0; | |
| for (POIItem item : row.getItems()) { | |
| HSSFCell c = r.createCell(colNumber++); | |
| if (c.getCellStyle() == null) | |
| c.setCellStyle(centerStyle); | |
| c.setCellValue(item.getValue()); | |
| } | |
| } | |
| } | |
| /** | |
| * Cria o cabeçalho do documento. | |
| */ | |
| private void createHeader() | |
| { | |
| if (!hasHeader()) return; | |
| HSSFCellStyle headerStyle = getCenterStyle(); | |
| HSSFFont font = workbook.createFont(); | |
| font.setBoldweight(Font.BOLDWEIGHT_BOLD); | |
| headerStyle.setFont(font); | |
| HSSFRow rh = sheet.createRow(0); | |
| rh.setRowStyle(headerStyle); | |
| int columns = headerRow.getSize(); | |
| for (int idx = 0; idx < columns; idx++) { | |
| HSSFCell ch = rh.createCell(idx); | |
| ch.setCellValue(headerRow.getItem(idx).getValue()); | |
| ch.setCellStyle(headerStyle); | |
| } | |
| } | |
| /** | |
| * Cria o rodapé do documento. | |
| */ | |
| private void createFooter() { | |
| if (!hasFooter()) return; | |
| HSSFCellStyle footerStyle = getCenterStyle(); | |
| HSSFFont font = workbook.createFont(); | |
| font.setBoldweight(Font.BOLDWEIGHT_BOLD); | |
| footerStyle.setFont(font); | |
| HSSFRow rh = sheet.createRow(sheet.getLastRowNum() + 1); | |
| rh.setRowStyle(footerStyle); | |
| int columns = footerRow.getSize(); | |
| for (int idx = 0; idx < columns; idx++) { | |
| HSSFCell ch = rh.createCell(idx); | |
| ch.setCellValue(footerRow.getItem(idx).getValue()); | |
| ch.setCellStyle(footerStyle); | |
| } | |
| } | |
| /** | |
| * Inicializa e retorna um estilo centralizado. | |
| * @return {@link HSSFCellStyle} centralizado. | |
| */ | |
| private HSSFCellStyle getCenterStyle() | |
| { | |
| HSSFCellStyle centerStyle = workbook.createCellStyle(); | |
| centerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); | |
| return centerStyle; | |
| } | |
| public String getFullFilename() { | |
| return filename + extension; | |
| } | |
| public boolean hasHeader() { | |
| return headerRow != null && headerRow.getSize() > 0; | |
| } | |
| public boolean hasFooter() { | |
| return footerRow != null && footerRow.getSize() > 0; | |
| } | |
| public POITable getTable() { | |
| return table; | |
| } | |
| public void setTable(POITable table) { | |
| this.table = table; | |
| } | |
| public POIRow getHeaderRow() { | |
| return headerRow; | |
| } | |
| public void setHeaderRow(POIRow headerRow) { | |
| this.headerRow = headerRow; | |
| } | |
| public POIRow getFooterRow() { | |
| return footerRow; | |
| } | |
| public void setFooterRow(POIRow footerRow) { | |
| this.footerRow = footerRow; | |
| } | |
| public String getFilename() { | |
| return filename; | |
| } | |
| public void setFilename(String filename) { | |
| this.filename = filename; | |
| } | |
| public String getExtension() { | |
| return extension; | |
| } | |
| public void setExtension(String extension) { | |
| this.extension = extension; | |
| } | |
| public HSSFWorkbook getWorkbook() { | |
| return workbook; | |
| } | |
| public int getStartRow() { | |
| return startRow; | |
| } | |
| public void setStartRow(int startRow) { | |
| this.startRow = startRow; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Representa um item na tabela de dados para exportação. | |
| * @author thania.clair | |
| */ | |
| public class POIItem { | |
| private String value; | |
| public POIItem() { } | |
| public POIItem(String value) { | |
| this.value = value; | |
| } | |
| public String getValue() { | |
| return value; | |
| } | |
| public void setValue(String value) { | |
| this.value = value; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.List; | |
| /** | |
| * Representa uma linha na tabela de dados para exportação. | |
| * @author thania.clair | |
| */ | |
| public class POIRow { | |
| private List<POIItem> items; | |
| public void addPOIItem(POIItem item) { | |
| if (items == null) items = new ArrayList<POIItem>(); | |
| items.add(item); | |
| } | |
| public List<POIItem> getItems() { | |
| return items; | |
| } | |
| public void setItems(List<POIItem> items) { | |
| this.items = items; | |
| } | |
| public int getSize() { | |
| if (!hasItems()) return 0; | |
| return items.size(); | |
| } | |
| public POIItem getItem(int index) { | |
| return items.get(index); | |
| } | |
| public boolean hasItems() { | |
| return items != null && !items.isEmpty(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.List; | |
| /** | |
| * Representa a tabela de dados para exportação. | |
| * @author thania.clair | |
| */ | |
| public class POITable { | |
| private List<POIRow> rows; | |
| public void addPOIRow(POIRow row) { | |
| if (rows == null) rows = new ArrayList<POIRow>(); | |
| rows.add(row); | |
| } | |
| public List<POIRow> getRows() { | |
| return rows; | |
| } | |
| public void setRows(List<POIRow> rows) { | |
| this.rows = rows; | |
| } | |
| public boolean hasRows() { | |
| return rows != null && !rows.isEmpty(); | |
| } | |
| public int getNumberOfColumns() { | |
| if (!hasRows()) return 0; | |
| return rows.get(0).getSize(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment