javaexcelcastingapache-poihssf

Obtain textbox value from Excel in Java


I have an Excel file and I need to read a value from a textbox inside that Excel file.

I am using org.apache.poi library and I tried to obtain the value in the following way:

   List<HSSFObjectData> obj=workbook.getAllEmbeddedObjects();
   for (int i = 0; i < obj.size(); i++) {           
       HSSFTextbox t = (HSSFTextbox) obj.get(i);
   }

Unfortunetly I couldn't cast HSSFTextbox to a HSSFObjectData element.

Does anyone know how could this be done?


Solution

  • Maybe you can do like this:

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