Thanks for your reply Stephen, but i got to know the problem by using jxl.jar so i have switched to POI. Here i am getting the below Exception so can you please solve my problem if it is known:
java.io.IOException: Unable to read entire header; 0 bytes read; expected 512 bytes
at org.apache.poi.poifs.storage.HeaderBlockReader.alertShortRead(HeaderBlockReader.java:149)
at org.apache.poi.poifs.storage.HeaderBlockReader.<init>(HeaderBlockReader.java:85)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:151)
at POIExcelReader.displayFromExcel(POIExcelReader.java:48)
at POIExcelReader.main(POIExcelReader.java:72)
I have used below code:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
*
* @author Sreenu
*/
public class POIExcelReader {
/** Creates a new instance of POIExcelReader */
public POIExcelReader() {
}
/**
* This method is used to display the Excel content to command line.
*
* @param xlsPath
*/
public void displayFromExcel(
String xlsPath) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(xlsPath);
} catch (FileNotFoundException e) {
System.out.println("File not found in the specified path.");
e.printStackTrace();
}
POIFSFileSystem fileSystem = null;
try {
fileSystem = new POIFSFileSystem(inputStream);
HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
HSSFSheet sheet = workBook.getSheetAt(0);
HSSFRow newrow = sheet.createRow(5);
System.out.println("Row created");
HSSFCell cell = newrow.createCell(5);
System.out.println("Cell created");
cell.setCellValue("Hi this is the first trail in POI");
System.out.println("Writes data into excel");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* The main executable method to
test displayFromExcel method.
*
* @param args
*/
public static void main(String[] args) {
POIExcelReader poiExample = new POIExcelReader();
String xlsPath = "C:/Eclipse_workspace/Project/Result.xls";
poiExample.displayFromExcel(xlsPath);
}
}