1

I am trying to export data to an excel and I am using StringBuffer to append columns and rows. I wrote the necessary column headers and to these headers I am populating the values. I get the dates like in the code snippet. The some of the values under the dates are null. So, I am facing a problem in having the empty data for the dates, the cell is treated as empty and the values are not getting placed into the right column, I tried checking for null and adding a 0 instead but that did not work as well.

I am not sure how I can tackle this problem. I want to get that particulat date column, for example, for column 'require201809' if a value exists then populate that value else leave it empty.

StringBuffer strBf = new StringBuffer();
strBf.append("BW");
for (String date : sortedOrderedDates) {
    strBf.append(";require" + date);
    strBf.append(";free" + date);
}
strBf.append("\n");
for (String skulId : pro.getSkulGrMap().keySet()) {
    strBf.append(pro.getBWId());
    strBf.append(";");
    for (String dateKey : productData.get(skulId).getOrderAmount().keySet()) {
        if(productData.get(skulId).getOrderAmount().get(dateKey)== null){
            strBf.append("0");
            strBf.append(";");
        }else{
            strBf.append(productData.get(skulId).getOrderAmount().get(dateKey));    
            strBf.append(";");
        }
        if(productData.get(skulId).getProductAmount().get(dateKey) != null){
            strBf.append(productData.get(skulId).getProductAmount().get(dateKey));
            strBf.append(";");
        }else{
            strBf.append("0");
            strBf.append(";");
        }
    }
    strBf.append("\n");
}

I would be really helpful if someone can help me with this or atleast give an idea as to how I can tackle this.

Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35
gopi
  • 11
  • 3
  • Please don't use StringBuffer as it was replaced by StringBuilder more than ten years ago. btw `strBf.append("0;");` is faster than adding one String at a time. – Peter Lawrey Aug 10 '18 at 13:56
  • If the empty fields are being dropped it is most like a problem with how you are reading the data, not how you are writing it. An empty field for a `null` value should be fine. – Peter Lawrey Aug 10 '18 at 13:57
  • Do you have an example of what happens when you try to insert a blank? It sounds like you're saying the columns get "shifted"? Have you tried a space with similar results? Going a different direction, I wonder if you might consider a DataTable approach according to this article: https://stackoverflow.com/questions/8207869/how-to-export-datatable-to-excel – LBW Aug 10 '18 at 14:02
  • I changed the way of reading the data, if there is no data for a datefield then I added null to it, before that I skipped looking into fields whether there was a value available or not. The problem was in function getProductAmount(). And like suggested I added a space for null values while writing and it worked. All of you thank you for the suggestions. – gopi Aug 13 '18 at 08:10

0 Answers0