我有一个Excel文件,我需要从该Excel文件中的文本框中读取一个值.
我正在使用org.apache.poi库,我试图通过以下方式获取值:
List<HSSFObjectData> obj=workbook.getAllEmbeddedObjects();
for (int i = 0; i < obj.size(); i++) {
HSSFTextbox t = (HSSFTextbox) obj.get(i);
}
不幸的是,我无法将HSSFTextbox强制转换为HSSFObjectData元素.
有谁知道怎么办?
解决方法:
也许你可以这样做:
try {
InputStream input = new FileInputStream("qa-textbox.xls");
POIFSFileSystem fs = new POIFSFileSystem(input);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFPatriarch pat = sheet.getDrawingPatriarch();
List children = pat.getChildren();
Iterator it = children.iterator();
while(it.hasNext()) {
HSSFShape shape = (HSSFShape)it.next();
if (shape instanceof HSSFTextbox){
HSSFTextbox textbox = (HSSFTextbox)shape;
HSSFRichTextString richString = textbox.getString();
String str = richString.getString();
System.out.println("String: " + str);
System.out.println("String length: " + str.length());
}
}
} catch (IOException ex) {
ex.printStackTrace();
}