简体   繁体   English

Excel VBA函数转换为Java poi

[英]Excel VBA function to Java poi

I'm new to java so please bear with me. 我是Java的新手,所以请多多包涵。 I know java apache poi has the following functions built in but I'm a little lost as to how to use it: 我知道java apache poi具有以下内置函数,但是我对如何使用它有些迷惑:

1) code_col = WorksheetFunction.Match("Code", sheet.Range("1:1"), 0) 1)code_col = WorksheetFunction.Match(“ Code”,sheet.Range(“ 1:1”),0)

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

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

If you could provide example of how the VBA code converts to its java apache poi equivalent it would be a great help. 如果您可以提供有关VBA代码如何转换为其等效的Java apache poi的示例,那将是一个很大的帮助。
So Im basically converting a VBA module to java. 因此,我基本上将VBA模块转换为Java。 And I use the apache poi library. 我使用apache poi库。 However, those 3 above lines of code I have difficulty converting to its java equivalent 但是,我上面的那三行代码很难转换为它的java等效代码

As said in my comment already, this question cannot be answered that general. 正如我在评论中已经说过的那样,这个问题无法得到一般性的回答。

Apache poi is made for creating files for Microsoft Office . Apache poi用于创建Microsoft Office文件。 So apache poi is able creating Excel files having the functions MATCH and COUNTIF stored in formulas in cells. 因此, apache poi能够创建具有功能MATCHCOUNTIF Excel文件,该文件存储在单元格的公式中。 It is also able to evaluate those formulas if it has access to all of the cells which are referenced in those formulas. 如果它可以访问这些公式中引用的所有单元格,则还可以评估这些公式。

So for a concrete example, having a MSExcelWithVBA.xlsm like this: 因此,对于一个具体示例,具有如下所示的MSExcelWithVBA.xlsm

在此处输入图片说明

and VBA code like this: 和这样的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

Then this VBA code prints to the immediate window: 然后,此VBA代码将打印到立即窗口:

code_col: 4
payee_count: 3
count_rows_in_region: 9

With the same MSExcelWithVBA.xlsm , a Java code like this: 使用相同的MSExcelWithVBA.xlsm ,像这样的Java代码:

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();
 }  
}

prints to System.out : 打印到System.out

code_col: 4.0
payee_count: 3.0
count_rows_in_sheet: 9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM