user1135595
user1135595

Reputation:

Apache Poi - Algo

I'm working on to fetch data from database and show on excel. I am using Apache poi library to generate excel.

here's the snippet for generating excel.

public class GenerateReport {

    public void showReport() {

    List<T> t = sampleDao.getAllData();


    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet();
    HSSFRow row     = null;
        HSSFCell column = null;

        HSSFRow row1 = sheet.createRow(1);

        HSSFCell c11 = row1.createCell(1);
        HSSFCell c12 = row1.createCell(2);
        HSSFCell c13 = row1.createCell(3);
        HSSFCell c14 = row1.createCell(4);
        HSSFCell c15 = row1.createCell(5);

        c11.setCellValue("ID");
        c12.setCellValue("Date");
        c13.setCellValue("Time");
        c14.setCellValue("YES/NO");
        c15.setCellValue("Action");



for (final T sampleT: t) {

            ....algo and what should be written 

         }


    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    externalContext.setResponseContentType("application/vnd.ms-excel");
    externalContext.setResponseHeader("Content-Disposition",
                "attachment; filename=\"Sample Report");
    workbook.write(externalContext.getResponseOutputStream());
    facesContext.responseComplete();

    }
    }

//getter setter for DAO

public void setSampleDao(
                SampleDao sampleDao) {
            this.sampleDao = sampleDao;
        }

        public SampleDao getSampleDao() {
            return sampleDao;
        }

    }


sampleDao.getAllData();

getAllData() method returns data which needs to be displayed via query.

I need help how to get data and what should be written in this for loop

for (final T sampleT: t) {

                ....algo and what should be written 

             }

There's a condition that if c14.setCellValue("YES/NO"); is Yes, then under this cell c15.setCellValue("Action"), a particular action is to be written.


ID Date Time YES/NO Action


Yes/No data is coming from database (sampleDao.getAllData();) , if Yes is present then i will have write a particular action in Action Column, after fetching the second data, it will check the previous Yes/No, if both are same/ or changed, i need to write Action according to it.

Can somebody help me

  1. in writing snippet in for loop with conditions.
  2. and how to set data in cells.

Upvotes: 0

Views: 291

Answers (2)

Maxwell Cheng
Maxwell Cheng

Reputation: 1190

You can set data in cell like this

`

Cell cell = row.getCell(excelColNum);
            if (cell == null) {
    cell = row.createCell(excelColNum);
}
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue( cellValue);

`

Upvotes: 0

sethu
sethu

Reputation: 8411

When you have written so much, I fail to understand why its difficult to fill up the loop.

Here's the algorithm/pseudo code...

String previousYesNo=null":
int rowcounter=2;
for (final T sampleT: t) {
  create hssf row with rowcounter
  create hssf cell for the 5 columns
  set data to all the columns from the sampleT object
  String yesNo=sampleT.getYesNO();
  if(previousYesNo!=null && prviousYesNo.equals(yesNo)){
    set action cell value to something
  }else{
    set action cell value to something else
  }
  previousYesNo=yesNo;
  rowCounter++;
}

To set data in cells:

HSSFRow row = sheet.getRow(rowIdx);
HSSFCell cell = row.getCell(colIdx);
//if its a string.. otherwise choose the correct cell type
cell.setCellType(HSSFCell..CELL_TYPE_STRING);
cell.setCellValue(new HSSFRichTextString(data));

Upvotes: 2

Related Questions