Java-条件格式 - 将单元格设置为数字
有什么方法可以将字符串转换为数字?我一直在尝试使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“ Excel文件具有数字,但作为字符串”:应该避免这种问题,而不是解决此问题。将Excel中的数字存储为字符串将使用电子表格计算导致多个问题,而不仅仅是使用条件格式。
解决方法是,
这将使用一个公式条件格式规则,而不是直接相等的单元格进行比较。该公式使用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
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.