使用Apache POI及其名称获取Excel公式的内容

问题是我如何才能通过名字获得一个公式的内容。 在Excel中,您可以使用公式名称对表进行分组。 我想用Apache POI来获取这样一个名为table的公式的内容。 所以即使表扩展,我也总是得到最新的数据。 因为名字不会改变。

我做了一些编码,但到目前为止,我只能获得一张表的所有内容,而不是公式名的表。 我的代码在下面,谢谢。

package getTablesPOI; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; public class readExcel { // Creates a new instance of POIExcelReader public readExcel() { } /** * The main executable method to test readExcel method. * * @param args */ public static void main(String[] args) throws InvalidFormatException { readExcel excelStuff = new readExcel(); String cisFile = "C://Users//agoebbel//Desktop//PS//# Downloads - Files//POI_Test.xlsx"; String sheetName = "Main"; String searcher = "Something"; excelStuff.displayFromExcel(cisFile, sheetName, searcher); } /** * This method is used to display the Excel content to command line. * * @param cisFile * @param sheetName * @param searcher */ public void displayFromExcel(String cisFile, String sheetName, String searcher) throws InvalidFormatException { InputStream inputStream = null; try { inputStream = new FileInputStream(cisFile); } catch (FileNotFoundException e) { System.out.println("File not found in the specified path."); e.printStackTrace(); } try { OPCPackage opc = OPCPackage.open(inputStream); Workbook workbook; workbook = WorkbookFactory.create(opc); XSSFSheet sheet = (XSSFSheet) workbook.getSheet(sheetName); Iterator<Row> rows = sheet.rowIterator(); // String searcher = (new CellReference((Cell) rows)).formatAsString(); while (rows.hasNext()) { XSSFRow row = (XSSFRow) rows.next(); // display row number in the console. System.out.println("\n" + "Row No.: " + new Integer(row.getRowNum() + 1)); // once get a row its time to iterate through cells. Iterator<Cell> cells = row.cellIterator(); while (cells.hasNext()) { Cell cell = cells.next(); // Now we will get the cell type and display the values accordingly. switch (cell.getCellType()) //cell.getCellType () { case Cell.CELL_TYPE_FORMULA: { cell.getCellFormula(); switch (cell.getCachedFormulaResultType()) { case Cell.CELL_TYPE_NUMERIC: System.out.println("Last evaluated as: " + cell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: System.out.println("Last evaluated as \"" + cell.getRichStringCellValue() + "\""); break; } } case Cell.CELL_TYPE_NUMERIC: { // cell type numeric. System.out.println(cell.getNumericCellValue()); break; } case Cell.CELL_TYPE_STRING: { // cell type string. System.out.println(cell.getStringCellValue()); break; } default: { // types other than String and Numeric. System.out.println("Type not known."); break; } } } } } catch (IOException e) { e.printStackTrace(); } } } 

我相信你正在寻找的术语是命名范围 (尽pipe它们是在某些Excel版本的function区的“公式”选项卡中定义的)。 假设你的意思是:

在Excel中命名范围

然后,您需要从Apache POI中获得的关键方法是:

  • Workbook.getNumberOfNames()
  • Workbook.getName(string)
  • Workbook.getNameAt(INT)
  • Workbook.createName()

如果你知道范围的名字,并想要得到它所指的单元格,你可以做类似的事情

 Workbook wb = WorkbookFactory.create(new File("input.xls")); Name name = wb.getName("TestRange"); System.out.println("Named Range '" + name.getNameName() + "' points to " + name.getRefersToFormula()); 

对于上面的情况将打印

  Named Range 'TesRange' points to Sheet1!$A$1:$C$5