Java-条件格式 - 将单元格设置为数字

发布于 2025-01-23 17:27:52 字数 2580 浏览 0 评论 0原文

有什么方法可以将字符串转换为数字?我一直在尝试使用Apache POI使用条件格式的规则,但是我做不到,因为Excel文件具有数字,但是由于字符串和规则不起作用。这是代码:

公共类有条件的法式{

public static void main(String[] args) throws IOException {
    String excelFilePath = "Excel.xlsx";
    FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
     
    Workbook workbook = new XSSFWorkbook(inputStream);
    Sheet firstSheet = workbook.getSheetAt(0);
    Iterator<Row> iterator = firstSheet.iterator();
     
    while (iterator.hasNext()) {
        Row nextRow = iterator.next();
        Iterator<Cell> cellIterator = nextRow.cellIterator();
        firstSheet.setColumnHidden(4, true);
         
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();
          
            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue());
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue());
                    break;
            }
            
            System.out.print(" - ");
        }
        System.out.println();
    }
    
            /* Access conditional formatting facet layer */
            SheetConditionalFormatting my_cond_format_layer = firstSheet.getSheetConditionalFormatting();
            
            /* Rule */
            ConditionalFormattingRule my_rule = my_cond_format_layer.createConditionalFormattingRule(ComparisonOperator.EQUAL, "5.00");
            
            /* RED color */
            FontFormatting my_rule_pattern = my_rule.createFontFormatting();
            my_rule_pattern.setFontColorIndex(IndexedColors.RED.getIndex());
            
            /* background Yellow */
            PatternFormatting fill_pattern = my_rule.createPatternFormatting();
            fill_pattern.setFillBackgroundColor(IndexedColors.YELLOW.index);
             
             /* Cell Range Address */
            CellRangeAddress[] my_data_range = {CellRangeAddress.valueOf("F1:F15")};
            
            /* Attach rule to cell range */
            my_cond_format_layer.addConditionalFormatting(my_data_range,my_rule);
    
    inputStream.close();
    FileOutputStream fileOut = new FileOutputStream("file.xlsx");
    workbook.write(fileOut);
    fileOut.close();
    System.out.println("Successfully Created workbook");
}

}

Is there any way of converting a string to numeric? I've been trying to use a rule of Conditional Formatting with apache poi but i can't do it because the excel file has numbers but as strings and the rule doesn't work. Here's the code:

public class ConditionalFormatting {

public static void main(String[] args) throws IOException {
    String excelFilePath = "Excel.xlsx";
    FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
     
    Workbook workbook = new XSSFWorkbook(inputStream);
    Sheet firstSheet = workbook.getSheetAt(0);
    Iterator<Row> iterator = firstSheet.iterator();
     
    while (iterator.hasNext()) {
        Row nextRow = iterator.next();
        Iterator<Cell> cellIterator = nextRow.cellIterator();
        firstSheet.setColumnHidden(4, true);
         
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();
          
            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue());
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue());
                    break;
            }
            
            System.out.print(" - ");
        }
        System.out.println();
    }
    
            /* Access conditional formatting facet layer */
            SheetConditionalFormatting my_cond_format_layer = firstSheet.getSheetConditionalFormatting();
            
            /* Rule */
            ConditionalFormattingRule my_rule = my_cond_format_layer.createConditionalFormattingRule(ComparisonOperator.EQUAL, "5.00");
            
            /* RED color */
            FontFormatting my_rule_pattern = my_rule.createFontFormatting();
            my_rule_pattern.setFontColorIndex(IndexedColors.RED.getIndex());
            
            /* background Yellow */
            PatternFormatting fill_pattern = my_rule.createPatternFormatting();
            fill_pattern.setFillBackgroundColor(IndexedColors.YELLOW.index);
             
             /* Cell Range Address */
            CellRangeAddress[] my_data_range = {CellRangeAddress.valueOf("F1:F15")};
            
            /* Attach rule to cell range */
            my_cond_format_layer.addConditionalFormatting(my_data_range,my_rule);
    
    inputStream.close();
    FileOutputStream fileOut = new FileOutputStream("file.xlsx");
    workbook.write(fileOut);
    fileOut.close();
    System.out.println("Successfully Created workbook");
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜警司 2025-01-30 17:27:52

“ Excel文件具有数字,但作为字符串”:应该避免这种问题,而不是解决此问题。将Excel中的数字存储为字符串将使用电子表格计算导致多个问题,而不仅仅是使用条件格式。

解决方法是,

ConditionalFormattingRule my_rule = my_cond_format_layer.createConditionalFormattingRule("VALUE(F1)=5.00");

这将使用一个公式条件格式规则,而不是直接相等的单元格进行比较。该公式使用Excel的value函数始终从单元格中获取数字值。

"the excel file has numbers but as strings": This should be avoided rather than working around this problem. Storing numbers in Excel as string will lead to multiple problems using the spreadsheet calculation, not only using conditional formatting.

The workaround would be

ConditionalFormattingRule my_rule = my_cond_format_layer.createConditionalFormattingRule("VALUE(F1)=5.00");

This uses a formula conditional formatting rule instead of the comparison the cell to equal directly. The formula uses Excel's VALUE function to always get numeric values from cells.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文