Obtain textbox value from Excel in Java

半城伤御伤魂 提交于 2019-12-17 10:04:29

问题


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?


回答1:


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



回答2:


this will help to read your excel sheet

HSSFWorkbook workbook = new HSSFWorkbook(fs);
      for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) {
          //the OLE2 Class Name of the object
          String oleName = obj.getOLE2ClassName();
          if (oleName.equals("Worksheet")) {
              DirectoryNode dn = (DirectoryNode) obj.getDirectory();
              HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(dn, fs, false);
              //System.out.println(entry.getName() + ": " + embeddedWorkbook.getNumberOfSheets());
        readSheetElements(embeddedWorkbook);

          } else if (oleName.equals("Document")) {
              DirectoryNode dn = (DirectoryNode) obj.getDirectory();
              HWPFDocument embeddedWordDocument = new HWPFDocument(dn, fs);
              //System.out.println(entry.getName() + ": " + embeddedWordDocument.getRange().text());
          }  else if (oleName.equals("Presentation")) {
              DirectoryNode dn = (DirectoryNode) obj.getDirectory();
              SlideShow embeddedPowerPointDocument = new SlideShow(new HSLFSlideShow(dn, fs));
              //System.out.println(entry.getName() + ": " + embeddedPowerPointDocument.getSlides().length);
          } else {
              if(obj.hasDirectoryEntry()){
                  // The DirectoryEntry is a DocumentNode. Examine its entries to find out what it is
                  DirectoryNode dn = (DirectoryNode) obj.getDirectory();
                  for (Iterator entries = dn.getEntries(); entries.hasNext();) {
                      Entry entry = (Entry) entries.next();
                      //System.out.println(oleName + "." + entry.getName());
                  }
              } else {
                  // There is no DirectoryEntry
                  // Recover the object's data from the HSSFObjectData instance.
                  byte[] objectData = obj.getObjectData();
              }
          }
      }

    public void readSheetElements(HSSFWorkbook embeddedWorkbook) {
     HSSFSheet  hSSFSheet = embeddedWorkbook.getSheet(0);
     java.util.Iterator<Row>    rowItr = hSSFSheet .rowIterator() ;
    while(rowItr .hasNext()) {

     java.util.Iterator<Cell>   cellItr  = rowItr.next().cellIterator() ;
     // read cell value from each cell

    }


来源:https://stackoverflow.com/questions/13967168/obtain-textbox-value-from-excel-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!