
Hello Sir, I am new to Java, I have Started Java Core by myself. I want to make a project which include 3-4 Excel file to handle.Please explain how to read and write Excel file in Java.Is it tough to do this kind of project for a beginner.
Thanks

Here is a java code that reads the excel file using Apache POI api.
import java.io.*;
import java.util.*;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
public class ReadExcel{
public static void main(String[]args){
short a=0;
short b=1;
int i=0;
ArrayList list1=new ArrayList();
ArrayList list2=new ArrayList();
String value1="", value2="";
String filename ="C:/data.xls";
if(filename != null && !filename.equals("")){
try{
FileInputStream fs =new FileInputStream(filename);
HSSFWorkbook wb = new HSSFWorkbook(fs);
for(int k = 0; k < wb.getNumberOfSheets(); k++){
int j=i+1;
HSSFSheet sheet = wb.getSheetAt(k);
int rows = sheet.getPhysicalNumberOfRows();
for(int r = 1; r < rows; r++){
HSSFRow row = sheet.getRow(r);
int cells = row.getPhysicalNumberOfCells();
HSSFCell cell1 = row.getCell(a);
value1 = cell1.getStringCellValue();
HSSFCell cell2 = row.getCell(b);
value2 = cell2.getStringCellValue();
list1.add(value1);
list2.add(value2);
}
i++;
}
}catch(Exception e){
}
}
for(int j=0;j<list1.size();j++){
System.out.println(list1.get(j).toString()+"\t"+list2.get(j).toString());
}
}
}
For more information, visit the following link:
http://www.roseindia.net/tutorial/java/poi/readExcelFile.html

Now to create an excel file or to add data to excel file, here is a code.
import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
public class CreateExcelFile{
public static void main(String[]args){
try{
String filename="c:/hello.xls" ;
HSSFWorkbook hwb=new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("new sheet");
HSSFRow rowhead= sheet.createRow((short)0);
rowhead.createCell((short) 0).setCellValue("SNo");
rowhead.createCell((short) 1).setCellValue("First Name");
rowhead.createCell((short) 2).setCellValue("Last Name");
rowhead.createCell((short) 3).setCellValue("Username");
rowhead.createCell((short) 4).setCellValue("E-mail");
rowhead.createCell((short) 5).setCellValue("Country");
HSSFRow row= sheet.createRow((short)1);
row.createCell((short) 0).setCellValue("1");
row.createCell((short) 1).setCellValue("Rose");
row.createCell((short) 2).setCellValue("India");
row.createCell((short) 3).setCellValue("roseindia");
row.createCell((short) 4).setCellValue("hello@roseindia.net");
row.createCell((short) 5).setCellValue("India");
FileOutputStream fileOut = new FileOutputStream(filename);
hwb.write(fileOut);
fileOut.close();
System.out.println("Your excel file has been generated!");
} catch ( Exception ex ) {
System.out.println(ex);
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.