我需要生成其中某些值是分数的列。在这种情况下,Excel将该值显示为计算值或日期(为准备屏幕截图,我手动添加了空间)。
我认为解决方案是应用功能:
= TEXT(值,\“ 0 /#0 \”)
所以,我的问题是如何使用Java Aspose库将函数应用于单元格?这是最好的解决方案吗?
答案 0 :(得分:1)
好吧,这是MS Excel的行为,它将分数值转换为DateTime或数字(计算)值。您可以通过将数字格式应用于指定的单元格来轻松应对。请参阅带有注释的示例代码,以获取有关如何使用Aspose.Cells for Java在单元格中显示分数值的参考。另外,请参见有关如何将公式应用于单元格的代码: 例如 示例代码:
Workbook workbook = new Workbook();
//Get the first worksheet (default sheet)
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
//Input fraction values to the cells.
Cell cell1 = cells.get("A1");
double val1 = 1/2d;
cell1.putValue(val1);
Cell cell2 = cells.get("A2");
double val2 = 19/4d;
cell2.putValue(val2);
Cell cell3 = cells.get("A3");
double val3 = 5/3d;
cell3.putValue(val3);
//Create a style object.
Style style = workbook.createStyle();
//Set the numbers formatting
style.setCustom("# ?/?");
//Apply style to the cells.
cell1.setStyle(style);
cell2.setStyle(style);
cell3.setStyle(style);
//Apply formula to a cell
Cell cell4 = cells.get("B1");
cell4.setFormula("=TEXT(A1,\"0/#0\")");
//Even you may directly put numeric value as text value.
Cell cell5 = cells.get("C1");
cell5.putValue("1/2", false);
//Note: if you double cliked in the cell, it will be converted to numeric DateTime notations.
//Save the file
workbook.save("f:\\files\\out1.xls");
注意:我是Aspose的支持开发人员/传播者。