Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Write and Read Excel Sheet Using Apache POI API

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 860
    Comment on it

    The Apache POI API is used to work with Excel files in java. By using Apache POI you can easily read and write the Records and data to Excel files. Here the below code can used to read and write the data in Excel files.

    import java.io.File;
    import java.io.FileOutputStream;
    import java.util.Map;
    import java.util.Set;
    import java.util.TreeMap;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    public class Writesheet 
    {
    
    
       public static void WriteSheet()
       {
    
          //Create blank workbook
          XSSFWorkbook workbook = new XSSFWorkbook(); 
    
          //Create a blank sheet
          XSSFSheet spreadsheet = workbook.createSheet("Employee Details");
    
          //Create row object
          XSSFRow row;
    
          //This data needs to be written (Object[])
          Map < String, Object[] > empinfo = new TreeMap < String, Object[] >();
          empinfo.put( "1", new Object[] { "EMP ID", "EMP NAME", "DESIGNATION" });
          empinfo.put( "2", new Object[] { "101", "Mayank", "Technical Manager" });
          empinfo.put( "3", new Object[] { "102", "Manish", "Software Developer" });
          empinfo.put( "4", new Object[] { "103", "Namita", "Software Developer" });
          empinfo.put( "5", new Object[] { "104", "Akhilesh", "Database Administrator" });
          empinfo.put( "6", new Object[] { "105", "Neelam", "Associate" });
    
          //Iterate over data and write to sheet
          Set < String > keyid = empinfo.keySet();
          int rowid = 0;
          for (String key : keyid)
          {
             row = spreadsheet.createRow(rowid++);
             Object [] objectArr = empinfo.get(key);
             int cellid = 0;
             for (Object obj : objectArr)
             {
                Cell cell = row.createCell(cellid++);
                cell.setCellValue((String)obj);
             }
          }
    
          //Write the workbook in file system
          FileOutputStream out = new FileOutputStream( 
          new File("Writesheet.xlsx"));
          workbook.write(out);
          out.close();
    
       }
    
       public static void ReadSheet(File file)throws Exception
        {
            FileInputStream fis = new FileInputStream(file);
            XSSFWorkbook workbook = new XSSFWorkbook(fis);
            XSSFSheet spreadsheet = workbook.getSheetAt(0);
            XSSFRow row;
            Iterator < Row > rowIterator = spreadsheet.iterator();
            while (rowIterator.hasNext())
            {
            row = (XSSFRow) rowIterator.next();
            Iterator< Cell > cellIterator = row.cellIterator();
            while ( cellIterator.hasNext())
            {
                Cell cell = cellIterator.next();
                switch (cell.getCellType())
                {
                    case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + " \t\t " );
                    break;
                    case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue() + " \t\t " );
                    break;
                }
            }
            System.out.println();
            }
            fis.close();
        }
    
       public static void main(String[] args) throws Exception 
       {
          //To Write The Excel Sheet
          WriteSheet();
    
          //To Read the Excel Sheet
          ReadSheet(new File("WriteSheet.xlsx"));
       }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: