Excel file reading project

Hi, I am new here and just finished AP Computer Science learning Java. I am now doing a little programming project for my brother. He is giving me a folder with spreadsheets. I should be able to detect if he has added new spreadsheets. I have figured out the logic of the program (except how to detect if he adds new spreadsheets). I found some sample code using Apache POI, but I wasn't able to use it because I kept getting compile-time errors saying the POI or HSSF libraries or whatever couldn't be found. So I went to the Apache site to download what ever I needed and had three issues:

1. I didn't know what to download

2. Even if I wanted to download something, they weren't all in one zip or rar file so i didnt't know what to download or how to do it

3. I don't know where to put these files so I would be able to run my program which I am crrently just compiling and executing from a .java file

I know this is a lot, but thank you very much in advance.

[999 byte] By [technogeeka] at [2007-11-27 10:46:43]
# 1

Goto to this site http://www.eng.lsu.edu/mirrors/apache/poi/release/bin/

and download this file if it is windows ,poi-bin-3.0.1-FINAL-20070705.zip anf if it is linux download this file , poi-bin-3.0.1-FINAL-20070705.tar.gz

extract those file u will be gettting three jar files .

if our application is webapplication then place all the jar file which u download from above in lib folder

if our applicaiton is standlone set to classpath variable

i think it work

seethasrinatha at 2007-7-28 20:20:31 > top of Java-index,Core,Core APIs...
# 2

seeing as i am a beginner, please tell me how to set the class path. I am writing a stand-alone program because I do not know HTML. I am forever grateful for the help you have provided me. I would also greatly appreciate if you could provide some sample code on how to extract a value from a cell. This is what I managed to put together.

import java.util.*;

import java.io.*;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.hssf.usermodel.HSSFRow;

class ExcelExtractor

{

public static void main (String args [])

{

InputStream input = POIExample.class.getResourceAsStream ("workbook.xls");

POIFSFileSystem fs = new POIFSFileSystem (input);

HSSFWorkbook wb = new HSSFWorkbook (fs);

HSSFSheet sheet = wb.getSheetAt (0);

HSSFRow row = sheet.getRow(1);

HSSFCell cell = row.getCell((short)3);

System.out.println (cell.getStringCellValue ());

}

}

technogeeka at 2007-7-28 20:20:31 > top of Java-index,Core,Core APIs...
# 3

this is sample code which creates an excel sheet and reads excel values from the cell and modify ,depending on u r requirement change u r application

import java.io.IOException;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import org.apache.poi.hssf.usermodel.*;

import org.apache.poi.hssf.util.*;

/**

* File for HSSF testing/examples

*

* THIS IS NOT THE MAIN HSSF FILE!! This is a util for testing functionality.

* It does contain sample API usage that may be educational to regular API users.

*

* @see #main

* @author Andrew Oliver (acoliver at apache dot org)

*/

public class modify

{

Stringfilename =null;

HSSFWorkbook hssfworkbook=null;

/**

* Constructor HSSF - creates an HSSFStream from an InputStream. The HSSFStream

* reads in the records allowing modification.

*

*

* @param filename

*

* @exception IOException

*

*/

public modify(String filename)

throws IOException

{

this.filename = filename;

POIFSFileSystem fs =

new POIFSFileSystem(new FileInputStream(filename));

hssfworkbook = new HSSFWorkbook(fs);

// records = RecordFactory.createRecords(stream);

}

/**

* Constructor HSSF - given a filename this outputs a sample sheet with just

* a set of rows/cells.

*

*

* @param filename

* @param write

*

* @exception IOException

*

*/

public modify(String filename, boolean write)

throws IOException

{

shortrownum = 0;

FileOutputStream out= new FileOutputStream(filename);

HSSFWorkbookwb= new HSSFWorkbook();

HSSFSheets= wb.createSheet();

HSSFRow r= null;

HSSFCell c= null;

/*/**

* Constructor HSSF - takes in file - attempts to read it then reconstruct it

*

*

* @param infile

* @param outfile

* @param write

*

* @exception IOException

*

*/

public modify(String infile, String outfile, boolean write)

throws IOException

{

this.filename = infile;

POIFSFileSystem fs =

new POIFSFileSystem(new FileInputStream(filename));

hssfworkbook = new HSSFWorkbook(fs);

// HSSFWorkbook book = hssfstream.getWorkbook();

}

/**

* Method main

*

* Given 1 argument takes that as the filename, inputs it and dumps the

* cell values/types out to sys.out

*

* given 2 arguments where the second argument is the word "write" and the

* first is the filename - writes out a sample (test) spreadsheet (see

* public HSSF(String filename, boolean write)).

*

* given 2 arguments where the first is an input filename and the second

* an output filename (not write), attempts to fully read in the

* spreadsheet and fully write it out.

*

* given 3 arguments where the first is an input filename and the second an

* output filename (not write) and the third is "modify1", attempts to read in the

* spreadsheet, deletes rows 0-24, 74-99. Changes cell at row 39, col 3 to

* "MODIFIED CELL" then writes it out. Hence this is "modify test 1". If you

* take the output from the write test, you'll have a valid scenario.

*

* @param args

*

*/

public static void main(String args[])

{

if (args.length < 2)

{

try

{

modify hssf = new modify(args[ 0 ]);

System.out.println("Data dump:\n");

HSSFWorkbook wb = hssf.hssfworkbook;

for (int k = 0; k < wb.getNumberOfSheets(); k++)

{

System.out.println("Sheet " + k);

HSSFSheet sheet = wb.getSheetAt(k);

introws = sheet.getPhysicalNumberOfRows();

for (int r = 0; r < rows; r++)

{

HSSFRow row= sheet.getPhysicalRowAt(r);

intcells = row.getPhysicalNumberOfCells();

System.out.println("ROW " + row.getRowNum());

for (int c = 0; c < cells; c++)

{

HSSFCell cell = row.getPhysicalCellAt(c);

Stringvalue = null;

switch (cell.getCellType())

{

case HSSFCell.CELL_TYPE_FORMULA :

value = "FORMULA ";

break;

case HSSFCell.CELL_TYPE_NUMERIC :

value = "NUMERIC value="

+ cell.getNumericCellValue();

break;

case HSSFCell.CELL_TYPE_STRING :

value = "STRING value="

+ cell.getStringCellValue();

break;

default :

}

System.out.println("CELL col="

+ cell.getCellNum()

+ " VALUE=" + value);

}

}

}

}

catch (Exception e)

{

e.printStackTrace();

}

}

else if (args.length == 2)

{

if (args[ 1 ].toLowerCase().equals("write"))

{

System.out.println("Write mode");

try

{

long time = System.currentTimeMillis();

modify hssf = new modify(args[ 0 ], true);

System.out

.println("" + (System.currentTimeMillis() - time)

+ " ms generation time");

}

catch (Exception e)

{

e.printStackTrace();

}

}

else

{

System.out.println("readwrite test");

try

{

modify hssf= new modify(args[ 0 ]);

// HSSFStreamhssfstream = hssf.hssfstream;

HSSFWorkbookwb= hssf.hssfworkbook;

FileOutputStream stream = new FileOutputStream(args[ 1 ]);

// HSSFCell cell = new HSSFCell();

// cell.setCellNum((short)3);

// cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);

// cell.setCellValue(-8009.999);

// hssfstream.modifyCell(cell,0,(short)6);

wb.write(stream);

stream.close();

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

/*else if ((args.length == 3)

&& args[ 2 ].toLowerCase().equals("modify1"))

{

try// delete row 0-24, row 74 - 99 && change cell 3 on row 39 to string "MODIFIED CELL!!"

{

HSSF hssf= new HSSF(args[ 0 ]);

// HSSFStreamhssfstream = hssf.hssfstream;

HSSFWorkbookwb= hssf.hssfworkbook;

FileOutputStream stream = new FileOutputStream(args[ 1 ]);

HSSFSheetsheet = wb.getSheetAt(0);

for (int k = 0; k < 25; k++)

{

HSSFRow row = sheet.getRow(k);

sheet.removeRow(row);

}

for (int k = 74; k < 100; k++)

{

HSSFRow row = sheet.getRow(k);

sheet.removeRow(row);

}

HSSFRow row = sheet.getRow(39);

HSSFCell cell = row.getCell(( short ) 3);

cell.setCellType(HSSFCell.CELL_TYPE_STRING);

cell.setCellValue("MODIFIED CELL!!!!!");

// HSSFCell cell = new HSSFCell();

// cell.setCellNum((short)3);

// cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);

// cell.setCellValue(-8009.999);

// hssfstream.modifyCell(cell,0,(short)6);

wb.write(stream);

stream.close();

}

catch (Exception e)

{

e.printStackTrace();

}

}*/

}

}

SETTING CLASSPATH :

Right click on mycomputer ->properites->advanced->environment variables->

in that create a user variable with poi and vlaue should be the path were u r there jar file are placed

seethasrinatha at 2007-7-28 20:20:31 > top of Java-index,Core,Core APIs...
# 4

thank you for the help. I wrote a progam using the import statements you gave. the program is below. I will be passing in the name of the file ("workbook.xls"). the cell method will then read the value in cell B2. when i compile the program, i am getting errors saying that the package does not exist and that the class symbols cannot be found. Please help.

import java.io.IOException;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import org.apache.poi.hssf.usermodel.*;

import org.apache.poi.hssf.util.*;

class Extractor

{

String fn;

HSSFWorkbook wb;

public Extractor (String file) throws IOException

{

fn = file;

cell ();

}

private void cell ()

{

POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fn));

wb = new HSSFWorkbook (fs);

HSSFSheet s = wb.getSheetAt (0);

HSSFRow row = s.getPhysicalRowAt(1);

HSSFCell cell = row.getPhysicalCellAt(1);

System.out.println (cell.getStringCellValue());

}

}

technogeeka at 2007-7-28 20:20:31 > top of Java-index,Core,Core APIs...