2

I want to generate excel sheet report. I having JSON object that i will be converted into JSONarray. Here is my sample code

JSONObject jsonObj = new JSONObject(jsonString.toString()); //convert to json object
JSONArray objSearchOrdersDto = jsonObj.getJSONArray("objSearchOrdersDto"); // convert to json array

for (int i = 0; i < objSearchOrdersDto.length(); ++i) 
{

    JSONObject rec = objSearchOrdersDto.getJSONObject(i);
    int OrderNumber = rec.getInt("OrderNumber");
    String strStatusType = rec.getString("strStatusType");
    int OrgUnitId = rec.getInt("OrgUnitId");
    System.out.println(OrderNumber+"\t"+strStatusType+"\t"+OrgUnitId); //want to excel file for this three field
}

Here i want to generate excel report only for these three field in for loop. Please give me suggestion.

3
  • Use Apache POI to do that Commented Mar 6, 2017 at 10:53
  • You can use Apache POI for this and use classes like XSSFWorkbook, XSSFSheet, Row, Cell. I have added a sample code snippet below, Please check - Mark as answered if it answers your query. Commented Mar 6, 2017 at 11:04
  • Its working, can it possible to open file in Append mode @SrikanthA . so i can continue with existing data for next time, Commented Mar 6, 2017 at 11:59

1 Answer 1

2

You can use Apache POI for this and use classes like XSSFWorkbook, XSSFSheet, Row, Cell.

JSONObject jsonObj = new JSONObject(jsonString.toString()); //convert to json object
JSONArray objSearchOrdersDto = jsonObj.getJSONArray("objSearchOrdersDto"); // convert to json array


XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Report");

        int rowCount = 0;
        for (int i = 0; i < objSearchOrdersDto.length(); ++i)
        {
            JSONObject rec = objSearchOrdersDto.getJSONObject(i);
            int OrderNumber = rec.getInt("OrderNumber");
            String strStatusType = rec.getString("strStatusType");
            int OrgUnitId = rec.getInt("OrgUnitId");

            Row row = sheet.createRow(++rowCount);
            Cell cell1 = row.createCell(1);
            cell1.setCellValue(OrderNumber);
            Cell cell2 = row.createCell(2);
            cell2.setCellValue(strStatusType);
            Cell cell3 = row.createCell(3);
            cell3.setCellValue(OrgUnitId);
            System.out.println(OrderNumber+"\t"+strStatusType+"\t"+OrgUnitId); //want to excel file for this three field
        }


        try (FileOutputStream outputStream = new FileOutputStream("Report.xlsx")) {
            workbook.write(outputStream);
        }

Required Imports -

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.