Configure the spreadsheet created by java with jxl

Asked

Viewed 757 times

2

I am developing an application that generates an excel spreadsheet with data collected from other files, for this I am using jxl, the application is already generating the spreadsheet correctly, but I need to change some settings of these spreadsheets, such as column size, I also need to place the entire sheet so that the information is displayed centralized. But the only thing I could modify was the font style and the background color, I couldn’t find where I could make these other modifications. If you can help me, thank you.

Update

Code (with centralized headers but columns not yet aligned):

if (arquivos != null) {
    for (int x = 0; x < arquivos.length; x++) {
        if (arquivos[x].getName().endsWith("pdf")) {
            File f = arquivos[x];
            try (RandomAccessBufferedFileInputStream acesso = new RandomAccessBufferedFileInputStream(f.getAbsolutePath())) {
                PDFParser parser = new PDFParser(acesso);
                parser.parse();
                COSDocument cosDoc = parser.getDocument();
                PDFTextStripper pdfStripper = new PDFTextStripper();
                PDDocument pdDoc = new PDDocument(cosDoc);

                WritableWorkbook planilha = Workbook.createWorkbook(new File(outPath + f.getName().replace(".pdf", ".xls"))); // Cria um arquivo XLS
                WritableSheet aba = planilha.createSheet("Lote " + f.getName().replace(".pdf", ""), 0); // Nome da aba do arquivo XLS

                String cabecalho[] = new String[4];
                cabecalho[0] = "Página";
                cabecalho[1] = "Autorização";
                cabecalho[2] = "Status Leitura";
                cabecalho[3] = "Retorno Ticket Log";

                Colour bckcolor = Colour.DARK_BLUE; // Cor do fundo do cabeçalho
                WritableCellFormat cellFormat = new WritableCellFormat();
                cellFormat.setBackground(bckcolor);

                cellFormat.setAlignment(Alignment.CENTRE);

                WritableFont fonte = new WritableFont(WritableFont.ARIAL); // Formato da fonte do cabeçalho
                fonte.setColour(Colour.WHITE); // Cor da fonte do cabeçalho
                cellFormat.setFont(fonte);

                WritableCellFormat cellFormat2 = new WritableCellFormat();
                cellFormat2.setAlignment(Alignment.CENTRE);

                for (int z = 0; z < cabecalho.length; z++) {
                    Label label = new Label(z, 0, cabecalho[z]);
                    aba.addCell(label);
                    WritableCell cell = aba.getWritableCell(z, 0);
                    cell.setCellFormat(cellFormat);
                }

                List<String> linhasGravadas = new ArrayList<>();

                for (int i = 1; i <= pdDoc.getNumberOfPages(); i++) {
                    pdfStripper.setStartPage(i);
                    pdfStripper.setEndPage(i);
                    String parsedText = pdfStripper.getText(pdDoc);

                    String aut = "";
                    String Status = "";
                    String pagina = Integer.toString(i);


                    Label label = new Label(0, i, pagina);
                    aba.addCell(label);

                    label = new Label(1, i, aut);
                    aba.addCell(label);

                    label = new Label(2, i, Status);
                    aba.addCell(label);
                }

                planilha.write();
                planilha.close();
                acesso.close();
            } catch (WriteException ex) {
                Logger.getLogger(testeXML.class.getName()).log(Level.SEVERE, null, ex);
            }
            f.renameTo(new File(outPath , f.getName()));
        }
    }
} 

inserir a descrição da imagem aqui

1 answer

4


You can center the text of a cell using the method setAlignment class WritableCellFormat. For example:

WritableCellFormat cellFormat = new WritableCellFormat();
cellFormat.setAlignment(Alignment.CENTRE);

This format can be passed in the constructor of a Label, e. g:

Label label = new Label(0, 1, "conteúdo", cellFormat);

Alternatively you can also set this format separately with the method setCellFormat, e. g.:

Label outroLabel = new Label(0, 1, "outro conteúdo");
outroLabel.setCellFormat(cellFormat);

The column size can be configured with the method setColumnView class WritableSheet. For example:

WritableSheet sheet = workbook.createSheet("Minha aba", 0);
// primeira coluna com espaço para 30 caracteres
sheet.setColumnView(0, 30);
// segunda coluna com espaço para 50 caracteres
sheet.setColumnView(1, 50);

Sources:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.