gpt4 book ai didi

java - Excel VBA函数转Java poi

转载 作者:行者123 更新时间:2023-12-02 12:09:39 27 4
gpt4 key购买 nike

我是java新手,请耐心等待。我知道 java apache poi 内置了以下功能,但我对如何使用它有点迷失:

1) code_col = WorksheetFunction.Match("代码",sheet.Range("1:1"), 0)

2) count = WorksheetFunction.CountIf(ref_sheet.Range("A:A"), 收款人)

3) lr =sheet.Cells(1, 1).CurrentRegion.Rows.count

如果您可以提供 VBA 代码如何转换为其等效的 java apache poi 的示例,那将是一个很大的帮助。
所以我基本上将 VBA 模块转换为 java。我使用 apache poi 库。然而,上面的 3 行代码我很难转换成它的 java 等价代码

最佳答案

正如我在评论中所说,这个问题不能笼统地回答。

Apache poi 用于为 Microsoft Office 创建文件。因此,apache poi 能够创建 Excel 文件,其中的函数 MATCHCOUNTIF 存储在单元格的公式中。如果它有权访问这些公式中引用的所有单元格,它还能够计算这些公式。

举一个具体的例子,有一个像这样的 MSExcelWithVBA.xlsm:

enter image description here

VBA代码如下:

Sub test()
Set this_sheet = ThisWorkbook.Worksheets("Sheet1")
code_col = WorksheetFunction.Match("Code", this_sheet.Range("1:1"), 0)
Debug.Print "code_col: " & code_col
payee = "b"
payee_count = WorksheetFunction.CountIf(this_sheet.Range("A:A"), payee)
Debug.Print "payee_count: " & payee_count
count_rows_in_region = this_sheet.Cells(1, 1).CurrentRegion.Rows.Count
Debug.Print "count_rows_in_region: " & count_rows_in_region
End Sub

然后此 VBA 代码将打印到立即窗口:

code_col: 4
payee_count: 3
count_rows_in_region: 9

使用相同的 MSExcelWithVBA.xlsmJava 代码如下:

import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.formula.WorkbookEvaluator;
import org.apache.poi.ss.formula.eval.*;

public class ExcelEvaluateSomeFunctions{

public static void main(String[] args) throws Exception {
Workbook workbook = WorkbookFactory.create(new FileInputStream("MSExcelWithVBA.xlsm"));
Sheet this_sheet = workbook.getSheet("Sheet1");
CreationHelper helper = workbook.getCreationHelper();

XSSFFormulaEvaluator formulaevaluator = (XSSFFormulaEvaluator)helper.createFormulaEvaluator();
WorkbookEvaluator workbookevaluator = formulaevaluator._getWorkbookEvaluator();
ValueEval valueeval = null;

//code_col = WorksheetFunction.Match("Code", this_sheet.Range("1:1"), 0)
double code_col = Double.NaN;
valueeval = workbookevaluator.evaluate("MATCH(\"Code\", " + this_sheet.getSheetName() + "!1:1, 0)", null);
if (valueeval instanceof NumberEval) {
code_col = ((NumberEval)valueeval).getNumberValue();
}
System.out.println("code_col: " + code_col);

//payee_count = WorksheetFunction.CountIf(this_sheet.Range("A:A"), payee)
String payee = "b";
double payee_count = Double.NaN;
valueeval = workbookevaluator.evaluate("COUNTIF(" + this_sheet.getSheetName() + "!A:A, \"" + payee + "\")", null);
if (valueeval instanceof NumberEval) {
payee_count = ((NumberEval)valueeval).getNumberValue();
}
System.out.println("payee_count: " + payee_count);

//count_rows_in_region = this_sheet.Cells(1, 1).CurrentRegion.Rows.Count
//this is not possible since apache poi does not know the concept of CurrentRegion
//best you could do:
int count_rows_in_sheet = this_sheet.getLastRowNum()+1;
System.out.println("count_rows_in_sheet: " + count_rows_in_sheet);

workbook.close();
}
}

打印到System.out:

code_col: 4.0
payee_count: 3.0
count_rows_in_sheet: 9

关于java - Excel VBA函数转Java poi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46631755/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com