Friday, 1 February 2019

Apache Poi: Writing to Column with Particular Header

package dummyarti;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

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


public class PoiData {

//declared for finding row position of result column
public static int  intRowwithResult;
public static int  intColwithResult;

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub


FileInputStream fr=new FileInputStream("D:\\TstArea\\data.xlsx");

XSSFWorkbook wb=new XSSFWorkbook(fr);
XSSFSheet ws=wb.getSheet("Sheet1") ;


Iterator<Row> r1= ws.iterator();
Row row1=r1.next();  // Poitns to first row
Iterator<Cell> c1= row1.iterator(); //Will Hold Cells of first row


while(c1.hasNext()){  //traverse till last empty cell is reached in a row
Cell cellvalue=c1.next(); //Hold value of cell of a row


boolean b1=cellvalue.toString().equalsIgnoreCase("result"); //checking if cell has value "result in it


if (b1) {
System.out.println(cellvalue.getRowIndex()); //get row, column location if result is found
System.out.println(cellvalue.getColumnIndex());

    intRowwithResult=cellvalue.getRowIndex();
    intColwithResult=cellvalue.getColumnIndex();

}


//XSSFRow row=ws.getRow(intRowwithResult);
intRowwithResult=intRowwithResult+1;
//
for(int j=intRowwithResult;j<10;j++) {


XSSFRow rowtowrite=ws.createRow(j);
XSSFCell cell = rowtowrite.createCell(intColwithResult);
cell.setCellValue("sumit");


//
//
}
//


FileOutputStream out = new FileOutputStream(new File("D:\\TstArea\\data.xlsx"));
        wb.write(out);
        wb.close();



}

}
}

No comments:

Post a Comment

Spring Boot : Exception Handler 14