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

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("[email protected]");
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
File handling in Java  Hi, Can any one tell me the basics of File handling in Java? ThanksADS_TO_REPLACE_1   Hi, File handling... more at File Handling in Java. Examples of file handling in Java: Java
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  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  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  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
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
File Handling
File Handling  how we do file handling of parsed XML code bu DOM in java
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
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
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
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
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
how to create an excel file using java
how to create an excel file using java  how to create an excel file using 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
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
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
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
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
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
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
FILE HANDLING CONCEPT
FILE HANDLING CONCEPT  Write a program to read and write the details of an employee like year of joining, Department code, salary using some byte stream classes
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
FILE HANDLING CONCEPT
FILE HANDLING CONCEPT  Prompt the user to enter the name of a text file to search for. If the name does not end in a .txt extension, display an error message. Search for this file in the current directory. If the file does
FILE HANDLING CONCEPT
FILE HANDLING CONCEPT   Write a program that reads input from the console in the form of a string. When the user enters a word called end, the program should stop reading from the console and print out the values that have been
FILE HANDLING CONCEPT
FILE HANDLING CONCEPT   Write a program that reads input from the console in the form of a string. When the user enters a word called end, the program should stop reading from the console and print out the values that have been
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
Reading an excel file into array
Reading an excel file into array  Hi, I'm trying to read in an excel file, search a column for containing key words (entered by a user) and then displaying the matching rows in a table. I'm fairly new to JavaScript. Can anyone
sir, how to convert excel file to csv file using java? please send me sample code.
sir, how to convert excel file to csv file using java? please send me sample code.  please send me sample code for converting excel file into csv file uisng java
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?  write a program in java to read a text file and write the output to an excel file using filereader and filewriter
importing excel file
importing excel file  Hi All I am developing a java application whre we upload a excel file in to database and all excel value should import... it is not showing real excel sheet value. please suggest good way to solve this problem
How to link a excel file with a application software using java swing
How to link a excel file with a application software using java swing  I have to link an excel file with a application software which I am developing.The excel file will contain questionnaire for surveys.I have this code which
How to link a excel file with a application software using java swing
How to link a excel file with a application software using java swing  I have to link an excel file with a application software which I am developing.The excel file will contain questionnaire for surveys.I have this code which
How to link an excel file with the application software using java swing
How to link an excel file with the application software using java swing  I have to link an excel file with a application software which I am developing.The excel file will contain questionnaire for surveys.I have this code which
How to Retrieve Data from the database and write into excel file using Java
How to Retrieve Data from the database and write into excel file using Java  Hi, I am trying to develop an small application where i trying to retrieve Data from the database and store the details in excel file. Please can
exception handling code for file not found error..
exception handling code for file not found error..  How to do exception handling for file not found error
importing excel file and drawing multiple graphs from one excel file
importing excel file and drawing multiple graphs from one excel file  thanks a lot sir for replying with code for importing excel file... time from one excel file using different columns..and instead of passing column
Reading and querying an Excel file in JavaScript
Reading and querying an Excel file in JavaScript  Hi, I'm trying to read in an excel file, search a column for containing key words (entered by a user) and then displaying the matching rows in a table. I'm fairly new
write excel file into the oracle database
write excel file into the oracle database  dear sir, i need the jsp code that reads the excel file and stores it into the oracle database table..and also i need the code to connect oracle database? thank u in advance
unicode handling in java
unicode handling in java  reading a unicode text file in java
Create and Save Excel File in JSP
Create and Save Excel File in JSP  ... and saving Excel file from JSP application. In this example we are going.... Java program allows to you to create any number of excel sheets in an excel
Insert excel file data into database Using Java Programming
Insert excel file data into database in Java Program In this PHP tutorial section, you will learn how to insert excel file data into the database. We have... excel file data into Database Using java programming: import java.io.

Ads