蓗乐 2022-01-30 18:37 采纳率: 66.7%
浏览 41
已结题

用spire.xls复制EXCEL表格时,里边的函数为何无法运行?

IF function中的 currentFormula = "IF(A1=H1,I1,J1)";及currentFormula = "=IF(A2=H2,I2,J2))";没有运行,考虑过应该是加载文档的问题。
回复修改后的代码,一经测试成功必采。

直接上问题代码:


import com.spire.xls.*;

public class Test {
    public static void main(String args[]){
        //加载文档
        Workbook wb = new Workbook();
        wb.loadFromFile("原始数据.xlsx");

        //获取第一张工作表
        Worksheet sheet = wb.getWorksheets().get(0);

        //Declare two variables: currentRow、currentFormula
        int currentRow = 1;
        String currentFormula = null;

        //Set the column width
        sheet.setColumnWidth(1, 32);
        sheet.setColumnWidth(2, 16);

        //Write test data into cells
        sheet.getCellRange(currentRow,1).setValue("AI.data");
        sheet.getCellRange(currentRow,2).setNumberValue(1);
        sheet.getCellRange(currentRow,3).setNumberValue(2);


        //Write text in cells
        currentRow += 2;
        sheet.getCellRange(currentRow,1).setValue("Formulas or functions:") ; ;
        sheet.getCellRange(currentRow,2).setValue("result:");

        //Format cells
        CellRange range = sheet.getCellRange(currentRow,1,currentRow,2);
        range.getStyle().getFont().isBold(true);
        range.getStyle().setKnownColor(ExcelColors.BlueGray);
        range.getStyle().setFillPattern(ExcelPatternType.Solid);
        range.getStyle().getBorders().getByBordersLineType(BordersLineType.EdgeBottom).setLineStyle(LineStyleType.Medium);

        //IF function
        currentFormula = "IF(A1=H1,I1,J1)";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //IF function
        currentFormula = "=IF(A2=H2,I2,J2))";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);


        //保存文档
        wb.saveToFile("system.xlsx",ExcelVersion.Version2013);
    }
}

img

img

  • 写回答

2条回答 默认 最新

  • 「已注销」 2022-01-30 19:58
    关注

    数据文件发一下

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月8日
  • 已采纳回答 1月31日
  • 创建了问题 1月30日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵