How to read and write excel sheet using java in Linux environment?

please provide me example..

waiting forĀ  u r reply..

Add Comment
  • 2 Answer(s)

    For Reading Excel File :

    import java.io.File;
    
    import java.io.FileInputStream;
    
    import java.io.IOException;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    
    import org.apache.poi.ss.usermodel.Row;
    
    import org.apache.poi.ss.usermodel.Sheet;
    
    import org.apache.poi.ss.usermodel.Workbook;
    
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    public class ReadExcelFile
    
    {
    public Sheet readExcel(String filePath, String fileName, String sheetName) throws IOException
    
    {
    
    File file = new File(filePath+"//"+fileName);
    
    FileInputStream inputStream = new FileInputStream(file);
    
    Workbook Workbook1 = null;
    
    String fileExtensionName = fileName.substring(fileName.indexOf("."));
    if(fileExtensionName.equals(".xlsx"))
    
    {
    
    Workbook1 = new XSSFWorkbook(inputStream);
    
    }
    
    else if(fileExtensionName.equals("xls"))
    
    {
    
    Workbook1 = new HSSFWorkbook(inputStream);
    
    }
    Sheet sheet1 = Workbook1.getSheet(sheetName);
    
    return sheet1;
    }
    }
    
    Train Answered on October 18, 2014.
    Add Comment

    For writing Excel File :

    import java.io.File;
    
    import java.io.FileInputStream;
    
    import java.io.FileNotFoundException;
    
    import java.io.FileOutputStream;
    
    import java.io.IOException;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    
    import org.apache.poi.ss.usermodel.Cell;
    
    import org.apache.poi.ss.usermodel.Row;
    
    import org.apache.poi.ss.usermodel.Sheet;
    
    import org.apache.poi.ss.usermodel.Workbook;
    
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    public class WriteExcelFile
    
    {
    public void writeExcel(String filePath, String fileName, String sheetName, String[] dataToWrite) throws IOException
    {
    File file = new File(filePath+"//"+fileName);
    FileInputStream inputStream = new FileInputStream(file);
    Workbook Workbook1 = null;
    
    String fileExtensionName = fileName.substring(fileName.indexOf("."));
    
    if(fileExtensionName.equals(".xlsx"))
    
    {
    
    Workbook1 = new XSSFWorkbook(inputStream);
    }
    else if(fileExtensionName.equals(".xls"))
    
    {
    
    Workbook1 = new HSSFWorkbook(inputStream);
    }
    Sheet sheet = Workbook1.getSheet(sheetName);
    int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();
    Row row = sheet.getRow(0);
    
    Row newRow = sheet.createRow(rowCount+1);
    
    for(int j = 0; j < row.getLastCellNum(); j++)
    
    {
    Cell cell = newRow.createCell(j);
    
    cell.setCellValue(dataToWrite[j]);
    }
    inputStream.close();
    
    FileOutputStream outputStream = new FileOutputStream(file);
    Workbook1.write(outputStream);
    
    outputStream.close();
    }
    public static void main(String[] args) throws IOException
    
    {
    
    String[] valueToWrite = {"Mr. F","Kolkata"};
    
    WriteExcelFile objExcelFile = new WriteExcelFile();
    
    objExcelFile.writeExcel(System.getProperty("user.dir")+"/src/infi/Auto","ExportExcel.xlsx","ExcelDemo",valueToWrite);
    
    }
    }
    
    Train Answered on October 18, 2014.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.