473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

How to Read and Write Data to excel sheet in java

Expand|Select|Wrap|Line Numbers
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9. import java.util.Iterator;
  10. import java.util.List;
  11. import java.util.Scanner;
  12.  
  13. import org.apache.poi.hssf.usermodel.HSSFCell;
  14. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  15. import org.apache.poi.hssf.usermodel.HSSFRow;
  16. import org.apache.poi.hssf.usermodel.HSSFSheet;
  17. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  18. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  19. import org.apache.poi.ss.usermodel.Cell;
  20. import org.apache.poi.ss.usermodel.Row;
  21. import org.apache.poi.ss.usermodel.Sheet;
  22. import org.apache.poi.ss.usermodel.Workbook;
  23. import org.apache.poi.ss.usermodel.WorkbookFactory;
  24.  
  25.  
  26. public class ReadWriteXL
  27. {
  28. public static void main(String[] args) throws InvalidFormatException, IOException{
  29.     ArrayList arr=new ArrayList();
  30. File f=new File("D:\\temp\\TEXT\\email.txt");
  31. Scanner in=new Scanner(f);
  32. System.out.println("Read Data From The Txt file ");
  33. while(in.hasNext())
  34. {    
  35. arr.add(in.next());
  36. }
  37. System.out.println("Data From ArrayList");
  38. System.out.println(arr);
  39.  
  40.  
  41. System.out.println("Write data to an Excel Sheet");
  42. FileOutputStream fos=new FileOutputStream("D:/temp/1.xls");
  43. HSSFWorkbook workBook = new HSSFWorkbook();
  44. HSSFSheet spreadSheet = workBook.createSheet("email");
  45. HSSFRow row;
  46. HSSFCell cell;
  47. for(int i=0;i<arr.size();i++){
  48.     row = spreadSheet.createRow((short) i);
  49. cell = row.createCell(i);
  50. System.out.println(arr.get(i));
  51. cell.setCellValue(arr.get(i).toString());
  52. }
  53. System.out.println("Done");
  54. workBook.write(fos);
  55. arr.clear();
  56. System.out.println("ReadIng From Excel Sheet");
  57.  
  58. FileInputStream fis = null;
  59.     fis = new FileInputStream("D:/temp/1.xls");
  60.  
  61.     HSSFWorkbook workbook = new HSSFWorkbook(fis);
  62.     HSSFSheet sheet = workbook.getSheetAt(0);
  63.     Iterator rows = sheet.rowIterator();
  64.  
  65.     while (rows.hasNext()) {
  66.         HSSFRow row1 = (HSSFRow) rows.next();
  67.         Iterator cells = row1.cellIterator();
  68.         while (cells.hasNext()) {
  69.             HSSFCell cell1 = (HSSFCell) cells.next();
  70.             arr.add(cell1);
  71.         }
  72. }
  73. System.out.println(arr);
  74.  
  75.  
  76.  
  77.  
  78.  
  79. }}
  80.  
  81.  
  82.  
May 31 '13 #1
3 29121
Oralloy
988 Expert 512MB
lokanath60,

Nice bit of code - does it work, or is there a problem we can help you with?

I needed this about three years ago, and fell on my nose, so I'm glad to see your work.

Are you thinking of turning this into an article of some sort?

Cheers,
Oralloy
May 31 '13 #2
It is working fine ..I have posted it only because any one who is searing for "How to Read and Write Data to excel sheet in java" he can find this answer
Jun 2 '13 #3
Oralloy
988 Expert 512MB
lokanath60,

Have you considered writing an article on creating and manipulating spreadsheets using Java?

I had a problem about three years ago where I wanted to generate custom spreadsheets based on the content of the Atlassian Crucible tool's database.

Basically, the extension that I put into Crucible was supposed to update one page of a complex spreadsheet based on the comments in the tool.

Since I couldn't reliable manipulate the spreadsheet directly from Java, I ended up building a VBA form in the spreadsheet that interrogated Crucible through its REST interface. The resulting data were then pushed into the spreadsheet using VBA.

Oralloy
Jun 2 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: zino | last post by:
I have an excel sheet that have one of its cells as a hyperlink I need to read this cell through Vb.net and get the value of the hyperlink (the whole path ex: \\c:\document\..... ..... ... .. ,...
2
by: carlos | last post by:
How can we read / write numbers in a excel data sheet.
0
by: kalichakradhar | last post by:
hi, I am new to VB programming.. I am writing a automated Tool in Vb which could read/write cell value present in the MPP (project plan 2003).I am successful in opening the project but now i dont...
2
by: shankar2 | last post by:
Hello All, This is my first try at posting a query in this forum, your patience is appreciated. App Details: Access 2000 on Win 2000, Slightly better than a Novice, comfortable with various...
0
by: nallsindian | last post by:
Hi Good morning to all of you, i have a doubt in visual basic and excel connectivity, how to read or write the values in excel, Regards Nallasamy.VP
1
by: pvenu | last post by:
Hi, I know basic perl (regular expressions, pattern matching, string manipulation, reading writing into text files). Yet, my requirement is to read an input text file -> process this input file...
8
by: AccessHunter | last post by:
Hi, Below is the VBA code I am using to accept Age and Year from a form and then execute a Make Table Query, QryGetAllCases to create the table FormSelectCases. Everything is working fine. ...
5
by: DaveBinM | last post by:
Hi, I've got an Excel file that calculates various engineering formulas, and uses VBA to perform certain tasks essential to the calculations, and I now need to make an ASP.Net page that allows...
7
by: TG | last post by:
hi! I am trying to create a sql server table from an excel sheet. Here is the code I have: 'This procedure the xlsx file and dumps it to a table in SQL Server
8
blazedaces
by: blazedaces | last post by:
So I have a program below which writes an excel file with multiple sheets based on inputs of sheet names, data, cell types, etc. It uses Apache POI, which is currently the only thing I found...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.