Apache POI Tutorial - How to read/write data from Excel file (Code)

For YouTube Video link Click here


package day1;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Writing {

	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		// 1.Read Workbook    > .xls : HSSF				.xlsx : XSSF 
		XSSFWorkbook wb=new XSSFWorkbook(new FileInputStream("C:\\Users\\mprem\\OneDrive\\Desktop\\data.xlsx"));
		//2.Read SHeet
		XSSFSheet sh=wb.getSheet("Sheet1");
		
		String data=sh.getRow(2).getCell(1).toString();
		System.out.println(data);
		// 1.Reading data from cell if row & column both are there
		sh.getRow(2).getCell(1).setCellValue("prem");
		//2.Reading data from cell if row is there & column is not  there
		sh.getRow(2).createCell(2).setCellValue("prem");
		//3.Reading data from cell if row  & column both are not  there
		sh.createRow(4).createCell(2).setCellValue("prem");

		wb.write(new FileOutputStream("C:\\Users\\mprem\\OneDrive\\Desktop\\data.xlsx"));
	}
}

Comments

  1. Thanks For Your Blog. Are you looking for the best Excel Automation Services to automate your repetitive tasks? You can effectively automate your repetitive tasks with the industry's best Excel Automation Services!

    ReplyDelete

Post a Comment