【问题标题】:Insert fraction as text to Excel with Aspose library使用 Aspose 库将分数作为文本插入 Excel
【发布时间】:2018-07-31 13:06:22
【问题描述】:

我需要生成一些值是分数的列。在这种情况下,Excel 将此值显示为计算值,甚至显示为日期(为了准备屏幕截图,我手动添加了空格)。

我认为解决方案是应用函数:

=TEXT(值, \"0/#0\")

那么,我的问题是如何使用 java Aspose 库将函数应用于单元格?这是最好的解决方案吗?

【问题讨论】:

    标签: java excel aspose


    【解决方案1】:

    嗯,这是 MS Excel 的行为,它将分数转换为 DateTime 或数字(计算)值。您可以通过将数字格式应用于指定的单元格来轻松应对它。有关如何使用 Aspose.Cells for Java 在单元格中显示小数值的参考,请参阅带有 cmets 的示例代码。另外,请参阅有关如何将公式应用于单元格的代码: 例如 示例代码:

     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 担任支持开发人员/宣传员。

    【讨论】:

    • 实际上我对这种方法有疑问。 double val3 = 5/3d; cell3.putValue(val3);
    • 您发现了什么问题,请详细说明?
    • 对不起,我没有完成评论。实际上我决定使用这种方法cell5.putValue("1/2", false); 并且到目前为止为我工作。非常感谢
    • 很高兴知道你已经把它整理好了,不客气。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 2019-02-04
    • 1970-01-01
    • 2012-11-23
    相关资源
    最近更新 更多