Configure the spreadsheet created by java with jxl

2

I'm developing an application that generates an excel sheet with data collected from other files, for this I'm using jxl, the application is already generating the worksheet correctly, however I need to change some of the settings of these worksheets, such as the size of the column, I also need to place the entire worksheet in such a way that the information is displayed centrally. But the only thing I could change was the font style and background color, I did not find where I can make these other modifications. If you can help me, thank you.

Update

Code (with 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()));
        }
    }
} 

    
asked by anonymous 17.08.2016 / 16:31

1 answer

4

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

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

This format can be passed in the constructor of a Label , eg:

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

Alternatively you can also set this format separately with the setCellFormat , eg:

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

Column size can be configured with the setColumnView " of 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:

17.08.2016 / 21:25