Java添加、读取Excel公式

操作excel表格用公式来处理数据时,可通过创建公式来运算数据,或通过读取公式来获取数据信息来源。本文以通过Java代码来演示在Excel中创建及读取公式的方法。这里使用了Excel Java类库(Free Spire.XLS for Java 免费版),在官网下载获取文件包后,解压,将lib文件夹下的jar文件导入Java程序;或者通过maven仓库下载并导入。导入结果如下:

1. 创建公式

import com.spire.xls.*;

public class AddFormula {
    public static void main(String[] args) {
        //创建Workbook对象
        Workbook wb = new Workbook();

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

        //声明两个变量
        int currentRow = 1;
        String currentFormula = null;

        //设置列宽
        sheet.setColumnWidth(1, 32);
        sheet.setColumnWidth(2, 16);

        //写入用于测试的数据到单元格
        sheet.getCellRange(currentRow,1).setValue("测试数据:");
        sheet.getCellRange(currentRow,2).setNumberValue(1);
        sheet.getCellRange(currentRow,3).setNumberValue(2);
        sheet.getCellRange(currentRow,4).setNumberValue(3);
        sheet.getCellRange(currentRow,5).setNumberValue(4);
        sheet.getCellRange(currentRow,6).setNumberValue(5);

        //写入文本
        currentRow += 2;
        sheet.getCellRange(currentRow,1).setValue("公式:") ; ;
        sheet.getCellRange(currentRow,2).setValue("结果:");

        //设置单元格格式
        CellRange range = sheet.getCellRange(currentRow,1,currentRow,2);
        range.getStyle().getFont().isBold(true);
        range.getStyle().setKnownColor(ExcelColors.LightGreen1);
        range.getStyle().setFillPattern(ExcelPatternType.Solid);
        range.getStyle().getBorders().getByBordersLineType(BordersLineType.EdgeBottom).setLineStyle(LineStyleType.Medium);

        //算数运算
        currentFormula = "=1/2+3*4";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //日期函数
        currentFormula = "=TODAY()";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);
        sheet.getCellRange(currentRow,2).getStyle().setNumberFormat("YYYY/MM/DD");

        //时间函数
        currentFormula = "=NOW()";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);
        sheet.getCellRange(currentRow,2).getStyle().setNumberFormat("H:MM AM/PM");

        //IF函数
        currentFormula = "=IF(B1=5,\"Yes\",\"No\")";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //PI函数
        currentFormula = "=PI()";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //三角函数
        currentFormula = "=SIN(PI()/6)";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //计数函数
        currentFormula = "=Count(B1:F1)";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //最大值函数
        currentFormula = "=MAX(B1:F1)";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //平均值函数
        currentFormula = "=AVERAGE(B1:F1)";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //求和函数
        currentFormula = "=SUM(B1:F1)";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //保存文档
        wb.saveToFile("AddFormulas.xlsx",FileFormat.Version2013);
        wb.dispose();
    }
}

公式创建结果:

2.读取公式

import com.spire.xls.*;

public class ReadFormula {
    public static void main(String[] args) {
        //加载Excel文档
        Workbook wb = new Workbook();
        wb.loadFromFile("AddFormulas.xlsx");

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

        //遍历B1到B13的单元格
        for (Object cell: sheet.getCellRange("B1:B13"))
        {
            CellRange cellRange = (CellRange)cell;

            //判断单元格是否含有公式
            if (cellRange.hasFormula())
            {
                //打印单元格及公式
                String certainCell = String.format("单元格[%d, %d]含有公式:", cellRange.getRow(), cellRange.getColumn());
                System.out.println(certainCell + cellRange.getFormula());
            }
        }
    }
}

公式读取结果:

原文地址:https://blog.51cto.com/eiceblue/2465869

时间: 2024-07-30 16:40:02

Java添加、读取Excel公式的相关文章

java poi读取excel公式,返回计算值(转)

import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFCell; import java.io.*; public class FormulaToString { /** * @pa

C# 创建、读取Excel公式

对于数据量较大的表格,需要计算一些特殊数值时,我们通过运用公式能有效提高我们数据处理的速度和效率,对于后期数据的增删改查等的批量操作也很方便.此外,对于某些数值的信息来源,我们也可以通过读取数据中包含的公式来获取.下面的示例中将分享通过C# 来创建.读取Excel公式的方法. 工具使用 Spire.XLS for .NET 8.0下载安装该类库后,注意在程序中添加引用Spire.Xls.dll(dll文件可在安装路径下的Bin文件夹中获取)代码示例(供参考) [示例1]创建Excel公式 C#

Java Poi 读取excel 对所有类型进行处理

1.最近做了一个批量导入功能 , 发现poi读取excel的日期类型会出现问题,源于日期类型分为以下几种: ①.yyyy/MM/dd ②.HH:mm:ss ③.yyyy/MM/dd HH:mm:ss 2.解决思路: 日期,数字的类型都是数值的, 所有需要对每一个进行区分,根据cell.getCellStyle().getDataFormat() 方法  可以得到excel 格子中的short类型的值 ,从断点中得知 yyyy/MM/dd 格式的值是 14 HH:mm:ss  格式的值是 21 y

Java中读取Excel功能实现_POI

这里使用apache的poi进行读取excel 1,新建javaproject 项目:TestExcel 2,导入包 包下载地址:http://poi.apache.org/download.html#POI-3.10-FINAL 百度网盘下载:http://pan.baidu.com/s/1i365mQT 导入根目录下.lib.ooxml-lib下的所有jar 4,操作读取excel import java.io.File; import java.io.IOException; import

java poi读取excel

POI实现java读取excel 1.下载POI的jar包 , 云盘下载地址: http://pan.baidu.com/s/1jH59hdk commons-fileupload-1.3.jarcommons-io-2.4.jardom4j-1.6.1.jarpoi-3.10-beta2.jarpoi-ooxml-3.10-beta2.jarpoi-ooxml-schemas-3.10-beta2.jarpoi-scratchpad-3.10-beta2.jarxmlbeans-2.3.0.j

java poi 读取excel文件随笔

需求:最近的项目需要将app的上传菜品功能移到pc端来实现,主要难点就是图片的批量导入,因为现在的框架是公司自己开发的,我实在不敢恭维,上传文件我用js传到服务器,在后台来读....  为什么传到服务器,因为现在浏览器的安全性提高之后,input file 获取不到真实的绝对地址.... jar:poi-3.12.jar    poi-ooxml-3.12.jar  poi-ooxml-schemas-3.8-20120326.jar  xmlbeans-2.3.0.jar 没用同版本的是因为之

java poi读取excel文件

poi-3.9-20121203.jar poi-ooxml-3.9-20121203.jar poi-ooxml-schemas-3.9-20121203.jar stax-api-1.0.1.jar xmlbeans-2.3.0.jar 上面几个jar包缺一不可,有时候会出现ClassNotFound之类的错误,是因为jar缺少或不兼容的原因,上面jar包已经过测试,可以使用. 读取excel文件代码如下: public class ExcelUtils {// 对外提供读取excel文件的

Java——jxl读取Excel文件

1.创建文件流,打开EXCEL文件(jxi不支持.xlsx文件,支持.xls) FileInputStream excelFile = new FileInputStream(excelPath); Workbook workbook = Workbook.getWorkbook(excelFile); 2.切换到对应文件名 Sheet excelSheet = workbook.getSheet(sheetName); 3.获取实际行数和列数 int rows = excelSheet.get

JAVA POI读取Excel中Cell为null的处理

空数据:没有任何编辑过的单元格(非空格) 有时候我们需要对根据每一列的信息进行处理,这里就会出现易错的缺陷. 1.不需要这些空数据 row = sheet.getRow(i); for (Cell c : row) { //处理 } 2.需要这些空数据 row = sheet.getRow(i); for (int j=0;j<row.getLastCellNum();j++) { //处理 } 注:如有更好的方式,欢迎交流.