0
votes

I'm trying to write some results to an excel file using Apache Poi in Java. I'm storing the results in String variables and then using those in the data.put call.The first two work fine but the third is resulting in blank cells. When I print to system.out.println() it displays fine ?

// Create File to save results

//Create Blank workbook XSSFWorkbook workbook = new XSSFWorkbook();

    // Create a blank sheet 
    XSSFSheet sheet = workbook.createSheet("Usage");

    // Write Header Row (Object[]) 
    Map<String, Object[]> data = new TreeMap<String, Object[]>(); 
    data.put("1", new Object[]{"Set-ID", "Title", "Usage" }); 
    int row = 1;

XML CODE to Navigate Node .....

for (int n = 0; n < childNodes.getLength(); n++) {
      Node currentnode = childNodes.item(n);
      result += node.getNodeValue();
      String NodeName = currentnode.getNodeName();
      String NodeText = currentnode.getTextContent();
      System.out.println("Child Node Name: " +NodeName);

 XML CODE to Navigate Node ..... 

      if (NodeText != null && !NodeText.isEmpty()){
      System.out.println("Child Node Text: " +NodeText);
      fileWriter.write(";"+";"+NodeText+";");
      fileWriter.write(System.lineSeparator());
      String line = String.valueOf(t);
      data.put(line, new Object[]{ t,SetID,Title,NodeText.trim()});
          }
                            }
try { 
        // this Writes the workbook gfgcontribute 
        FileOutputStream out = new FileOutputStream(new File("Usage.xlsx")); 
        workbook.write(out); 
        out.close(); 
        System.out.println("Usage.xlsx written successfully on disk."); 
    } 
    catch (Exception e) { 
        e.printStackTrace(); 
    } 

And my print output looks like this:

Set-Id : babf6fab-d841-4092-88f0-fcf99c4bfa90;

Title : METFORMIN HYDROCHLORIDE TABLET [A-S MEDICATION SOLUTIONS];

Attribute Node Name: code

Attribute Node Value: 34067-9

Parent Node Name: section

Child Nodes: 11

Child Node Name: #text

Child Node Text: Child Node Name: id

Child Node Name: #text

Child Node Text: Child Node Name: code

Child Node Name: #text

Child Node Text: Child Node Name: title

Child Node Text: INDICATIONS AND USAGE

Child Node Name: #text

Child Node Text:

Child Node Name: text

Child Node Text: Metformin hydrochloride tablets are indicated as an adjunct to diet and exercise to improve glycemic control in adults and children with type 2 diabetes mellitus.

When I open the Excel sheet I have the list of Set-IDs and the Titles but the usage column is blank?

1
There is nothing in your code related to apache poi. Or where do you think your code calls apache poi classes and methods? What is data.put doing? And where is t coming from?Axel Richter
Added the extra code and formatted a little better.Fxguy1
Again, where do you think your code adds something into your XSSFSheet sheet? All I can see now is that you not seems to know what your code does at all. As often only copy/pasted code together and now wondering why it not works.Axel Richter

1 Answers

0
votes

Found the issue. Cell text is limited to 32,767 characters and some of the text results were larger than that. Once I added a catch for text larger than the limit it worked fine.