Apache POI:用于标识excel表中的表并读取它们的API [英] Apache POI : API to identify tables in the excel sheet and read them

查看:190
本文介绍了Apache POI:用于标识excel表中的表并读取它们的API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法返回工作表中存在的表格列表?
我的要求是从表格中的多个表中获取数据。

解决方案

我们假设您正在使用 XSSF API用于 .xlsx excel文件。
如果表是由 Insert-> Table 创建的,那么您可以使用以下内容读取它们:

  XSSFWorkbook工作簿=新的XSSFWorkbook(新文件(test.xlsx)); 
int numberOfSheets = workbook.getNumberOfSheets(); (int sheetIdx = 0; sheetIdx< numberOfSheets; sheetIdx ++)
{
XSSFSheet sheet = workbook.getSheetAt(sheetIdx);
列表< XSSFTable> tables = sheet.getTables(); $ x
(XSSFTable t:tables){
System.out.println(t.getDisplayName());
System.out.println(t.getName());
System.out.println(t.getNumerOfMappedColumns());
}
}

如果通过你的意思是任何有边框的东西,那么你必须创建一个非平凡的算法,读取每个单元格的所有单元格并检查边界(例如 leftBorderColor rightBorderColor topBorderColor bottomBorderColor ),并定义什么包含检查是否找到它。


Is there any method which returns a list of tables present in the sheet? My requirement is to fetch data from multiple tables present on the sheet.

解决方案

Let's assume that you are using the XSSF API for .xlsx excel files. If the tables were created by Insert->Table then you can read them by using this :

XSSFWorkbook workbook = new XSSFWorkbook(new File("test.xlsx"));
int numberOfSheets = workbook.getNumberOfSheets();
for(int sheetIdx = 0; sheetIdx < numberOfSheets; sheetIdx++) {
    XSSFSheet sheet = workbook.getSheetAt(sheetIdx);
    List<XSSFTable> tables = sheet.getTables();
    for(XSSFTable t : tables) {
        System.out.println(t.getDisplayName());
        System.out.println(t.getName());
        System.out.println(t.getNumerOfMappedColumns());
    }
}

If by table you mean anything that has a border then you have to create a non-trivial algorithm that reads all the cells of every sheet and checks the boundaries (e.g. leftBorderColor, rightBorderColor, topBorderColor, bottomBorderColor) and by defining what consists a table check if you've found it.

这篇关于Apache POI:用于标识excel表中的表并读取它们的API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆