簡體   English   中英

如何從 primefaces dataexporter 導出的 xlsx 列中刪除字符

[英]how to remove a character from an xlsx column exported by primefaces dataexporter

我有個問題。 我有一份使用 primefaces dataExporter 導出到 XLSX 的報告。 問題是電子表格數字列在相關數字之前包含一個引號,例如:

https://postimg.cc/YGXwfqrr

我對此沒有問題,關鍵是我的客戶要求不要發生這種情況,而我不知道該怎么做。 這是我如何導出的代碼:

在 primefaces 中:

<p:dataExporter type="xls" target="produto" fileName="produto" postProcessor="#{produtoBean.postProcessXLS}" />

GenericBaseBean(produtoBean 擴展自 GenericBaseBean):

public void postProcessXLS(Object document) {
    HSSFWorkbook book = (HSSFWorkbook) document;
    HSSFSheet sheet = book.getSheetAt(0);
    HSSFRow header = sheet.getRow(0);

    // Quantidade de colunas na planilha
    int colCount = header.getPhysicalNumberOfCells();
    // Quantidade de linhas na planilha
    int rowCount = sheet.getPhysicalNumberOfRows();

    // Pega o formato de número de decimal
    HSSFCellStyle decimal = book.createCellStyle();
    decimal.setDataFormat((short) 2);


    // Percorre as linhas da planilha
    for(int rowInd = 1; rowInd < rowCount; rowInd++) {
        HSSFRow row = sheet.getRow(rowInd);
        // Percorre as colunas da linha
        for(int cellInd = 1; cellInd < colCount; cellInd++) {
            // Pega a célula
            HSSFCell cell = row.getCell(cellInd);

            // Pega o conteúdo da célula
            String strVal = cell.getStringCellValue();

            if (strVal.contains("'")){
                cell.setCellType(HSSFCell.CELL_TYPE_BLANK);
                cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                strVal = strVal.replace("'","");
                System.out.println(cell.getStringCellValue());
            }

我正在嘗試檢查該字段是否有單引號並在此代碼段中專門刪除:

if (strVal.contains("'")){
                cell.setCellType(HSSFCell.CELL_TYPE_BLANK);
                cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                strVal = strVal.replace("'","");
                System.out.println(cell.getStringCellValue());
            }

但是當我調試 java 時沒有找到報價,但它出現在報告中。 有沒有辦法解決這個問題?

謝謝你!!!!

** 第一次編輯 **

感謝所有答案,但我仍在努力使其工作。 我是一名實習生,剛接觸編程,想在不向公司員工尋求更多幫助的情況下學習這一點,我已經嘗試了一段時間。 我使用了該主題的提示: Primefaces dataExporter to xls 浮點數變成電子表格單元格中的文本但我不知道我做錯了什么,說實話,我沒有正確理解這段代碼。 有人可以向我解釋我能做些什么來解決嗎?

public void postProcessXLS(Object document) {
    HSSFWorkbook book = (HSSFWorkbook) document;
    HSSFSheet sheet = book.getSheetAt(0);
    HSSFRow header = sheet.getRow(0);
    Workbook workbook = (Workbook) document;



    // Quantidade de colunas na planilha
    int colCount = header.getPhysicalNumberOfCells();
    // Quantidade de linhas na planilha
    int rowCount = sheet.getPhysicalNumberOfRows();

    // Pega o formato de número de decimal
    HSSFCellStyle decimal = book.createCellStyle();
    decimal.setDataFormat((short) 2);



    // Percorre as linhas da planilha
    for(int rowInd = 1; rowInd < rowCount; rowInd++) {
        HSSFRow row = sheet.getRow(rowInd);
        // Percorre as colunas da linha
        for(int cellInd = 1; cellInd < colCount; cellInd++) {
            // Pega a célula
            HSSFCell cell = row.getCell(cellInd);

            // Pega o conteúdo da célula
            String strVal = cell.getStringCellValue();

            if (strVal.matches("[0-9]+")){
                cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                CellStyle totalCellStyle = workbook.createCellStyle();
                totalCellStyle.setDataFormat(workbook.createDataFormat().getFormat("#,##0.00"));
                Cell currentCell = workbook.getSheetAt(0).getRow(0).getCell(0);
                currentCell.setCellValue(Double.parseDouble(currentCell.getStringCellValue()));
                currentCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                currentCell.setCellStyle(defaultCellStyle);
            }

            // Verifica se inicia com R$
            if(strVal.startsWith("R$")) {
                // Altera o tipo da célula para BRANCO e depois para NUMERICO
                cell.setCellType(HSSFCell.CELL_TYPE_BLANK);
                cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                // Remove os pontos dos separadores de grupo para ter somente o ponto da dezena
                strVal = strVal.replace(".", StringUtils.EMPTY);
                // Substitui a virgula por ponto
                strVal = strVal.replace(",", ".");
                // Retira o R$
                strVal = strVal.replace("R$", StringUtils.EMPTY);
                // Define o formato do número
                cell.setCellStyle(decimal);
                // Converte para double
                double dblVal = Double.valueOf(strVal);
                // Substitui o valor da célula para o valor formatado
                cell.setCellValue(dblVal);
            }

        }
    }
}

Excel 中的類型列可能是文本。 請嘗試更改號碼的類型。

教程: 在數字類型上更改 Excel 列

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM