Last active
March 3, 2023 15:06
-
-
Save rodrigocananea/be363243264553cdd749ce67213c1d1a to your computer and use it in GitHub Desktop.
Exemplo de geração de tabela xlsx com Apache POI 3.17+
This file contains 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.io.FileOutputStream; | |
import java.io.IOException; | |
import org.apache.poi.ss.usermodel.CellStyle; | |
import org.apache.poi.ss.usermodel.FillPatternType; | |
import org.apache.poi.ss.usermodel.Font; | |
import org.apache.poi.ss.usermodel.HorizontalAlignment; | |
import org.apache.poi.ss.usermodel.IndexedColors; | |
import org.apache.poi.ss.usermodel.Row; | |
import org.apache.poi.ss.usermodel.Sheet; | |
import org.apache.poi.ss.usermodel.Workbook; | |
import org.apache.poi.xssf.usermodel.XSSFCellStyle; | |
import org.apache.poi.xssf.usermodel.XSSFColor; | |
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |
public class TabelaPoi { | |
public static void main(String[] args) throws IOException { | |
// Cria um novo arquivo do Excel | |
Workbook workbook = new XSSFWorkbook(); | |
// Cria uma nova planilha | |
Sheet sheet = workbook.createSheet("Tabela"); | |
// Define os tamanhos das colunas | |
sheet.setColumnWidth(0, 5000); | |
sheet.setColumnWidth(1, 5000); | |
sheet.setColumnWidth(2, 5000); | |
// Define um estilo de fonte para o cabeçalho da tabela | |
Font headerFont = workbook.createFont(); | |
headerFont.setBold(true); | |
headerFont.setColor(IndexedColors.WHITE.getIndex()); | |
headerFont.setFontHeightInPoints((short) 12); | |
// Define um estilo de célula para o cabeçalho da tabela | |
CellStyle headerCellStyle = workbook.createCellStyle(); | |
headerCellStyle.setFont(headerFont); | |
headerCellStyle.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex()); | |
headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); | |
headerCellStyle.setAlignment(HorizontalAlignment.CENTER); | |
// Define um estilo de fonte para os registros da tabela | |
Font recordFont = workbook.createFont(); | |
recordFont.setColor(IndexedColors.BLACK.getIndex()); | |
recordFont.setFontHeightInPoints((short) 10); | |
// Define um estilo de célula para os registros da tabela | |
XSSFCellStyle recordCellStyle1 = (XSSFCellStyle) workbook.createCellStyle(); | |
recordCellStyle1.setFont(recordFont); | |
recordCellStyle1.setFillForegroundColor(IndexedColors.WHITE.getIndex()); | |
recordCellStyle1.setFillPattern(FillPatternType.SOLID_FOREGROUND); | |
XSSFCellStyle recordCellStyle2 = (XSSFCellStyle) workbook.createCellStyle(); | |
recordCellStyle2.setFont(recordFont); | |
XSSFColor color2 = new XSSFColor(new byte[]{(byte) 220, (byte) 221, (byte) 255}, null); | |
recordCellStyle2.setFillForegroundColor(color2); | |
recordCellStyle2.setFillPattern(FillPatternType.SOLID_FOREGROUND); | |
// Cria o cabeçalho da tabela | |
Row headerRow = sheet.createRow(0); | |
headerRow.createCell(0).setCellValue("Nome"); | |
headerRow.createCell(1).setCellValue("Idade"); | |
headerRow.createCell(2).setCellValue("Profissão"); | |
// Aplica o estilo de célula ao cabeçalho da tabela | |
for (int i = 0; i < headerRow.getLastCellNum(); i++) { | |
headerRow.getCell(i).setCellStyle(headerCellStyle); | |
} | |
// Cria os registros da tabela | |
for (int i = 1; i <= 10; i++) { | |
Row recordRow = sheet.createRow(i); | |
recordRow.createCell(0).setCellValue("Pessoa " + i); | |
recordRow.createCell(1).setCellValue(20 + i); | |
recordRow.createCell(2).setCellValue("Uma profissão " + i + " onde será adotado informações de grande escala"); | |
// Aplica o estilo de célula alternadamente aos registros da tabela | |
for (int j = 0; j < recordRow.getLastCellNum(); j++) { | |
recordRow.getCell(j).setCellStyle(i % 2 == 0 ? recordCellStyle1 : recordCellStyle2); | |
} | |
} | |
for (int i = 0; i < sheet.getRow(0).getLastCellNum(); i++) { | |
sheet.autoSizeColumn(i); | |
} | |
// Salva o arquivo do Excel | |
FileOutputStream outputStream = new FileOutputStream("tabela.xlsx"); | |
workbook.write(outputStream); | |
workbook.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment