Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Excel file Handling in Java
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


geeta kalra
Excel file Handling in Java
2 Answer(s)      2 years and 6 months ago
Posted in : Java Beginners


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


Advertisement
View Answers

June 21, 2012 at 3:18 PM


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



June 21, 2012 at 3:29 PM


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);

}
}
}


Related Tutorials/Questions & Answers:
Excel file Handling in Java
Excel file Handling in Java  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...://www.roseindia.net/tutorial/java/poi/readExcelFile.html   Now to create an excel file
File handling in Java
handling in Java? Thanks   Hi, File handling is very important... handling features in their api. You can read more at File Handling in Java. Examples of file handling in Java: Java I/0 Examples Working With Files
Advertisements
File Handling in Java
File Handling in Java  Hi, While opening a file in Java developers... was trying to find some tutorials about handling file in Java on net. I have also seen... are the use of these classes? Is there example code for explaining the File Handling
java file handling
java file handling  enter string from keyboard and then read a file if this string is present in file then print message already exists else write this string to file
java file handling
java file handling  program to get values from textbox array and the write them to text files
java file handling
java file handling  Hi friends, My situation is i have file named "sample.conf" which contains more IP Adressess.Eg: the file contains "IP=255.255.255.255" at some lines in the file. what i need to do is. i need to search
file handling - Java Beginners
manner. now write a progam in java and create another file which is having the amount required to purchase each item and at last read the newly created file...file handling  assume two files are there- 1. with the quanity of he
File Handling - Java Beginners
File Handling  Thanks for replying me.. but i am sorry to say...; System.out.print("Enter the file name : "); name = br.readLine(); name = "d...{ File f; f= new File(name); if(f.exists()){ RandomAccessFile rand
File Handling - Java Beginners
File Handling  Q. Write a java prg which accepts a list of existing text files from command line args & concatinate contents of all files...(e.getMessage()); } } }   its prg of copying file Check code
File Handling - Java Beginners
File Handling  I have written one program by which we can create a file , store any information in that,and resume that file using a user name and password.now my problem is that whenever i write datas into the file the previous
how to use Excel Templet to write excel file using java.
how to use Excel Templet to write excel file using java.  how to use Excel Templet to write excel file using java
excel file using JDBC java.?
excel file using JDBC java.?  hey, i am a telecomm. engineer , and i am try to develop a tool which reads from a excel file and then appends the same excel file as required. i am writing my code on notepad and have successfully
File Handling In Java
File Handling in Java In this section we will discuss about the file handling in Java. This section describes you about how to perform file handling in Java. In this section you will read what is file handling in Java, how to perform
how to use Excel Template to write excel file using java
how to use Excel Template to write excel file using java  How to use Excel template to write data in that file using java
Excel file wriring using java
Excel file wriring using java  i have one predefined bill format in excel i want to append values in it using java apche POI or Jexcel i tried it but it overwrites file and lost previous contents what can i do
File Handling Classes in Java
In this section, you will get to know about file handling classes in java to handle the file input output operations
Java File Handling Test Results
Java File Handling Test Results   Is somebody able to help me with this Write a program to read each name from the file and prompt the user... FileNotFoundException { new File("team.html").delete(); Scanner scan = new
how to create an excel file using java
how to create an excel file using java  how to create an excel file using java
File Handling
File Handling  how we do file handling of parsed XML code bu DOM in java
connection of java file to excel file and put data from excel file into oracle db
connection of java file to excel file and put data from excel file into oracle db  how to create button on excel sheet using java swing and how we give action to that button excel sheet? how we connect excel file to java file
read excel file from Java - Java Beginners
read excel file from Java  How we read excel file data with the help of java?  Hi friend, For read more information on Java POI visit to : http://www.roseindia.net/java/poi/ Thanks
Java File Handling
File Handling in Java       File Handling in Java For file handling.... The File Class In spite of classes available to support file I/O, Java has a class
downloading excel file using Java and springs
downloading excel file using Java and springs  I need to find out how to download an excel file using the spring framework in java. Please help me out as I am new to springs and its urgent
To read & write a excel file using the core java
To read & write a excel file using the core java  Hai, I'm new to JavaProgram.But now i need java program to read & write a excel file so, can anyone help me to learn the above mentioned topic(link for the portion
File handling
File handling  how do i search for an occurance of a word in a file and replace with the user input in java?   Java Replace a word in a file import java.io.*; import java.util.*; class FileOperations{ public static
Convert CSV to excel File - Java Beginners
Convert CSV to excel File  Sir, i have CSV file as input. I need to convert that CSV file to excel file and format. Currently i am using HSSF POI api to format the excel file. Here i am unable to read the data from the CSV
How to export data from html file to excel sheet by using java
How to export data from html file to excel sheet by using java    How to export data from html file to excel sheet by using java
Java Program to insert a row in the same sheet of excel file
Java Program to insert a row in the same sheet of excel file  Java program to insert a row in the same sheet of excel file using poi package in java
Inserting data in Excel File - Java Beginners
Inserting data in Excel File  Dear Sir, How can we fill the excel with fetched data from the database like Oracle, DB2? Is it possible to create an excel with filled data? Kindly give me the solutions to questions
How to export data from html file to excel sheet by using java
How to export data from html file to excel sheet by using java   reading the data from Html file
How to convert excel file int xml format in java
How to convert excel file int xml format in java  How to read excel file(xls) which is stored in any directory(D://) and convert it into xml format in java and place in any directory(E://).Can any body provide the code
Advertisements
 

 

 

DMCA.com