阅读使用Apache POI从Excel下拉列表的内容 [英] Read drop down list content from Excel using apache poi

查看:511
本文介绍了阅读使用Apache POI从Excel下拉列表的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Excel工作表特定的细胞创造一个下拉列表(数据验证),并宣读他们回来。

I need to create a drop down list (Data Validation) on a particular cell in an Excel sheet and read them back.

通过由的Apache POI ,我能够在一个Excel工作表创建一个下拉列表中提供的教程的帮助,但我也需要读取滴下拉列表中的内容时,再次读这一点,所以我可以呈现在用户界面相似的下拉列表。

With the help of tutorials provided by Apache POI, I am able to create a Drop-down list in an Excel sheet, but I also need to read the drop-down list content when reading that again, so that I can render a similar drop-down list on a UI.

有什么建议?

推荐答案

DataValidation存储,即使在HSSF工作簿,但它是在内页图书馆,由于它是私人因此获得它不会给应用程序的程序员。 我已经使用Java反射API来访问内部表。 这code是工作对我罚款。

DataValidation is stored even in HSSF workbook, but it is in Internal Sheet of the library and since it is private so access to it is not given to application programmer. I have used Java Reflection API to access internal sheet. This code is working fine for me.

private ArrayList<DVRecord> init(FileInputStream fis) throws InvalidFormatException, IOException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    HSSFWorkbook hWorkbook = (HSSFWorkbook) WorkbookFactory.create(fis);
    HSSFSheet hSheet = hWorkbook.getSheetAt(1); // sheet on which you want to read data validation
    Class c = org.apache.poi.hssf.usermodel.HSSFSheet.class;
    Field field = c.getDeclaredField("_sheet");
    field.setAccessible(true);
    Object internalSheet = field.get(hSheet);
    InternalSheet is = (InternalSheet) internalSheet;
    DataValidityTable dvTable = is.getOrCreateDataValidityTable();
    Class c2 = org.apache.poi.hssf.record.aggregates.DataValidityTable.class;
    Field field2 = c2.getDeclaredField("_validationList");
    field2.setAccessible(true);
    Object records = field2.get(dvTable);
    ArrayList<DVRecord> dvRecords = (ArrayList<DVRecord>) records;
    return dvRecords;
}

这篇关于阅读使用Apache POI从Excel下拉列表的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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