javapandit.net
  • Home
  • Quick Java
    • Exception Handling
    • Collections Framework
  • Java Best Practices
  • Web Services
    • Web Service Basics
    • Ten Basic webservice concepts
    • XML
    • Apache Axis
    • Restful Web Services
  • JMS Concepts
    • JMS- MySQL
  • Hadoop
    • NoSQL DATABASEs
    • Apache Sqoop
    • Hadoop Interview Questions
  • Java 5
  • Java 8
    • Java 8 : Lambda Expressions
  • JDBC
  • Java Architect
    • Enterprise application re-platforming strategies
    • Java Memory Management
  • Java Programs
  • Technical Tips
    • How to set JAVA_HOME environment variable
    • How to create an auto increment field in Oracle?
    • Linux Commands
  • Best Java Interview Questions
    • Java Interview Questions- YouTube
  • Interview Questions
    • Java Tech interview Questions
    • Core Java Interview Questions >
      • core tech questions1
      • Java Collection interview questions
      • Java Concurrency
    • Servlets Interview Questions
    • JSP Interview Questions
    • Java Web Services Interview Questions
    • EJB Interview Questions
    • XML Interview Questions
    • JMS Interview Questions
  • Struts Interview Questions
    • Struts 2 Interview Questions
  • Java EE Architects Interview Questions
    • Java Architect Interview Questions
    • Top 10 reasons for Java Enterprise Application Performance Problems
    • Web Application Scalability Questions for IT Architect
  • JavaPandit's Blog
  • Web Services Interview Questions
  • Servlets And JSP
  • Oracle SOA Interview Questions
    • Open ESB /JBI
    • BPEL Language
  • Log4J
  • Ant
  • Maven
  • JMeter
  • JUnit
  • Apache POI Framework
  • ORCALE SERVICE BUS (OSB) Interview Questions
  • J2EE Patterns
    • Model-View-Controller (MVC)
    • Front Controller
    • DAO
    • Business Delegate
    • Session Facade
    • Service Locator
    • Transfer Object
    • Design Patterns >
      • Creational Patterns >
        • Singleton
      • Behavioural Patterns
      • Structural Patterns
    • Intercepting Filter
  • SQL Interview Questions/Lab
  • Best Wall Papers
    • Devotional Songs
  • Java Community
  • HIBERNATE
  • ORACLE CC&B
    • Oracle CC&B Interview Questions
  • Docker
  • Little Princess
    • Sai Tanvi Naming Ceremony Celebrations
    • Rice Feeding Ceremony
    • Sai Tanvi Gallery
  • APPSC Career Guidance
    • AP History
    • Indian Polity
    • Indian Economy
    • Science & Technology
    • Mental Ability and Reasoning
    • Disaster Management
    • Current Affairs and Events
    • General Sciences >
      • Biology
      • Physics
      • Chemistry
    • Previous Question Papers
  • About Us
  • Contact US

POI Sample Applications



2. Reading from Excel sheet :

import  java.io.FileInputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ReadExcel{
       
            public static String fileTobeRead = "D:/work/java-excel.xls";
           
            public static void main(String[] args){
                try{
                        HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileTobeRead));
                        HSSFSheet sheet = workbook.getSheetAt(0);
                        HSSFRow row = sheet.getRow(0);
                        HSSFCell cell = row.getCell((short)0);
                        System.out.prinntln("The top left cell -->"+ cell.getRichStringCellValue());
                    }catch(Exception e){
                        e.printStackTrace();
                    }
            }
}

3. Reading & Writing MS Word document using java :

import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.extractor.wordExtractor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ReadWord{

    private static String fileName= "d:/work/sample.doc";
   
    public static void main(String[] args){
   
        ReadWord rdWord new ReadWord();
        rdWord.createWord();
        rdWord.readWord();
    }
    // Writing into MSWord
    public void createWord(){
        try{
                POFSFileSystem out = new POIFSFileSystem(new FileInputStream(fileName));
                HWPFDocument doc = new HWPFDocument(out);
                Range range = doc.getRange();
                range.insertBefore("hai rams");
               FileOutputStream fout = new FileOutputStream(fileName);
                doc.write(fout);
              }catch(Exception e){
                e.printStackTrace();
            }
     }
   
    // Reading From MSWord
    public void readWord(){
        try{
                POFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName));
                HWPFDocument doc = new HWPFDocument(fs);
                WordExtractor we = new WordExtractor(do);
                String para = we.getTextFromPices();
                System.out.println("No of letters ------"+ para.length());
                System.out.println(" data ------------"+para);
            }catch(Exception e){
                e.printStackTrace();
            }
     }
}
    

   
   
Prev123Next
Powered by Create your own unique website with customizable templates.