Hi,
wollte mal fragen ob jemand weiss, ob man mit Java auch was programmieren kann das ein Excel-file öffnen und anzeigen kann? Weiter sollte es möglich sein in das geöffnete Excel-file zu schreiben und Werte auszulesen!
Ja, hab ich gerade!
Aber in dem Verzeichnis liegen die ganzen files dann als *.java und da meckert der compiler! Die müssen ja als *.class drin liegen? Also HSSFWorkbook.class und nicht HSSFWorkbook.java ?
ich brauche das auch, hab's mir mal runtergeladen, krieg's aber nicht hin:
Ich habe das Beispielprogramm(den Code) kopiert und beim compilieren in dem Verzeichnis wo die jars liegen hagelts immer Fehlermeldungen das der die ganzen Sachen nicht finden kann:
Code:
/**
* This example shows how to use the event API for reading a file.
*/
public class EventExample
implements HSSFListener
{
private SSTRecord sstrec;
/**
* This method listens for incoming records and handles them as required.
* @param record The record that was found while reading.
*/
public void processRecord(Record record)
{
switch (record.getSid())
{
// the BOFRecord can represent either the beginning of a sheet or the workbook
case BOFRecord.sid:
BOFRecord bof = (BOFRecord) record;
if (bof.getType() == bof.TYPE_WORKBOOK)
{
System.out.println("Encountered workbook");
// assigned to the class level member
} else if (bof.getType() == bof.TYPE_WORKSHEET)
{
System.out.println("Encountered sheet reference");
}
break;
case BoundSheetRecord.sid:
BoundSheetRecord bsr = (BoundSheetRecord) record;
System.out.println("New sheet named: " + bsr.getSheetname());
break;
case RowRecord.sid:
RowRecord rowrec = (RowRecord) record;
System.out.println("Row found, first column at "
+ rowrec.getFirstCol() + " last column at " + rowrec.getLastCol());
break;
case NumberRecord.sid:
NumberRecord numrec = (NumberRecord) record;
System.out.println("Cell found with value " + numrec.getValue()
+ " at row " + numrec.getRow() + " and column " + numrec.getColumn());
break;
// SSTRecords store a array of unique strings used in Excel.
case SSTRecord.sid:
sstrec = (SSTRecord) record;
for (int k = 0; k < sstrec.getNumUniqueStrings(); k++)
{
System.out.println("String table value " + k + " = " + sstrec.getString(k));
}
break;
case LabelSSTRecord.sid:
LabelSSTRecord lrec = (LabelSSTRecord) record;
System.out.println("String cell found with value "
+ sstrec.getString(lrec.getSSTIndex()));
break;
}
}
/**
* Read an excel file and spit out what we find.
*
* @param args Expect one argument that is the file to read.
* @throws IOException When there is an error processing the file.
*/
public static void main(String[] args) throws IOException
{
// create a new file input stream with the input file specified
// at the command line
FileInputStream fin = new FileInputStream(args[0]);
// create a new org.apache.poi.poifs.filesystem.Filesystem
POIFSFileSystem poifs = new POIFSFileSystem(fin);
// get the Workbook (excel part) stream in a InputStream
InputStream din = poifs.createDocumentInputStream("Workbook");
// construct out HSSFRequest object
HSSFRequest req = new HSSFRequest();
// lazy listen for ALL records with the listener shown above
req.addListenerForAllRecords(new EventExample());
// create our event factory
HSSFEventFactory factory = new HSSFEventFactory();
// process our events based on the document input stream
factory.processEvents(req, din);
// once all the events are processed close our file input stream
fin.close();
// and our document input stream (don't want to leak these!)
din.close();
System.out.println("done.");
}
}
hier die Fehlermeldungen:
Code:
C:\Programme\Java\POI\EventExample.java:5: cannot find symbol
symbol: class HSSFListener
implements HSSFListener
^
C:\Programme\Java\POI\EventExample.java:7: cannot find symbol
symbol : class SSTRecord
location: class EventExample
private SSTRecord sstrec;
^
C:\Programme\Java\POI\EventExample.java:13: cannot find symbol
symbol : class Record
location: class EventExample
public void processRecord(Record record)
^
C:\Programme\Java\POI\EventExample.java:65: cannot find symbol
symbol : class IOException
location: class EventExample
public static void main(String[] args) throws IOException
^
C:\Programme\Java\POI\EventExample.java:18: cannot find symbol
symbol : variable BOFRecord
location: class EventExample
case BOFRecord.sid:
^
C:\Programme\Java\POI\EventExample.java:19: cannot find symbol
symbol : class BOFRecord
location: class EventExample
BOFRecord bof = (BOFRecord) record;
^
C:\Programme\Java\POI\EventExample.java:19: cannot find symbol
symbol : class BOFRecord
location: class EventExample
BOFRecord bof = (BOFRecord) record;
^
C:\Programme\Java\POI\EventExample.java:29: cannot find symbol
symbol : variable BoundSheetRecord
location: class EventExample
case BoundSheetRecord.sid:
^
C:\Programme\Java\POI\EventExample.java:30: cannot find symbol
symbol : class BoundSheetRecord
location: class EventExample
BoundSheetRecord bsr = (BoundSheetRecord) record;
^
C:\Programme\Java\POI\EventExample.java:30: cannot find symbol
symbol : class BoundSheetRecord
location: class EventExample
BoundSheetRecord bsr = (BoundSheetRecord) record;
^
C:\Programme\Java\POI\EventExample.java:33: cannot find symbol
symbol : variable RowRecord
location: class EventExample
case RowRecord.sid:
^
C:\Programme\Java\POI\EventExample.java:34: cannot find symbol
symbol : class RowRecord
location: class EventExample
RowRecord rowrec = (RowRecord) record;
^
C:\Programme\Java\POI\EventExample.java:34: cannot find symbol
symbol : class RowRecord
location: class EventExample
RowRecord rowrec = (RowRecord) record;
^
C:\Programme\Java\POI\EventExample.java:38: cannot find symbol
symbol : variable NumberRecord
location: class EventExample
case NumberRecord.sid:
^
C:\Programme\Java\POI\EventExample.java:39: cannot find symbol
symbol : class NumberRecord
location: class EventExample
NumberRecord numrec = (NumberRecord) record;
^
C:\Programme\Java\POI\EventExample.java:39: cannot find symbol
symbol : class NumberRecord
location: class EventExample
NumberRecord numrec = (NumberRecord) record;
^
C:\Programme\Java\POI\EventExample.java:44: cannot find symbol
symbol : variable SSTRecord
location: class EventExample
case SSTRecord.sid:
^
C:\Programme\Java\POI\EventExample.java:45: cannot find symbol
symbol : class SSTRecord
location: class EventExample
sstrec = (SSTRecord) record;
^
C:\Programme\Java\POI\EventExample.java:51: cannot find symbol
symbol : variable LabelSSTRecord
location: class EventExample
case LabelSSTRecord.sid:
^
C:\Programme\Java\POI\EventExample.java:52: cannot find symbol
symbol : class LabelSSTRecord
location: class EventExample
LabelSSTRecord lrec = (LabelSSTRecord) record;
^
C:\Programme\Java\POI\EventExample.java:52: cannot find symbol
symbol : class LabelSSTRecord
location: class EventExample
LabelSSTRecord lrec = (LabelSSTRecord) record;
^
C:\Programme\Java\POI\EventExample.java:69: cannot find symbol
symbol : class FileInputStream
location: class EventExample
FileInputStream fin = new FileInputStream(args[0]);
^
C:\Programme\Java\POI\EventExample.java:69: cannot find symbol
symbol : class FileInputStream
location: class EventExample
FileInputStream fin = new FileInputStream(args[0]);
^
C:\Programme\Java\POI\EventExample.java:71: cannot find symbol
symbol : class POIFSFileSystem
location: class EventExample
POIFSFileSystem poifs = new POIFSFileSystem(fin);
^
C:\Programme\Java\POI\EventExample.java:71: cannot find symbol
symbol : class POIFSFileSystem
location: class EventExample
POIFSFileSystem poifs = new POIFSFileSystem(fin);
^
C:\Programme\Java\POI\EventExample.java:73: cannot find symbol
symbol : class InputStream
location: class EventExample
InputStream din = poifs.createDocumentInputStream("Workbook");
^
C:\Programme\Java\POI\EventExample.java:75: cannot find symbol
symbol : class HSSFRequest
location: class EventExample
HSSFRequest req = new HSSFRequest();
^
C:\Programme\Java\POI\EventExample.java:75: cannot find symbol
symbol : class HSSFRequest
location: class EventExample
HSSFRequest req = new HSSFRequest();
^
C:\Programme\Java\POI\EventExample.java:79: cannot find symbol
symbol : class HSSFEventFactory
location: class EventExample
HSSFEventFactory factory = new HSSFEventFactory();
^
C:\Programme\Java\POI\EventExample.java:79: cannot find symbol
symbol : class HSSFEventFactory
location: class EventExample
HSSFEventFactory factory = new HSSFEventFactory();
^
30 errors
Prozess beendet mit Exit-Code 1
Ja warum, ich dachte das läuft über den implements HSSF Listener?
(Außerdem ist das ja ein Beispielprogramm, das muss ja laufen, ohne es zu veränder, oder?)
Ich habe mir die poi-bin-2.5.1-final-20040804 heruntergeladen, entpackt und die jar-Files in den Ordner lib\ext unterhalb des Java-Heimatverzeichnises reingepackt. Anschließend habe ich auf dieses Verzeichnis die PATH-Umgebungsvariable gesetzt und in die CLASSPATH aufgenommen.
Nun wollte ich folgendes Programm ausprobieren:
Code:
package javafuerwindows.poi;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelDemo1 {
public ExcelDemo1() {
/*
* 1. Schritt: Anlegen einer Arbeitsmappe
*/
HSSFWorkbook mappe = new HSSFWorkbook();
/*
* 2. Schritt: ein Arbeitsblatt wird angelegt
*/
HSSFSheet blatt = mappe.createSheet();
/*
* 3. Schritt: festlegen der Daten,
* die das Arbeitsblatt aufnehmen soll
*/
String [] weine = {"Würzburger Stein",
"Volkacher Ratsherr",
"Röthenbacher Steinlaus",
"Speyerer Kaisertröpfchen"};
int [] flaschen = {12, 6, 1, 7};
/*
* 4. Schritt: erzeugen der Zeilen
*/
HSSFRow [] zeilen = new HSSFRow[4];
for (int i = 0; i < 4; i++)
zeilen[i] = blatt.createRow(4 + i);
/*
* 5. Schritt: erzeugen der Zellen
* und füllen mit den Daten
*/
HSSFCell [] zellen = new HSSFCell[8];
for (int i = 0; i < 4; i++) {
zellen[i] = zeilen[i].createCell((short) 1);
zellen[i].setCellValue(weine[i]);
}
for (int i = 0; i < 4; i++) {
zellen[i + 4] = zeilen[i].createCell((short) 4);
zellen[i + 4].setCellValue(flaschen[i]);
}
/*
* 6. Schritt: speichern des Dokuments
*/
try {
FileOutputStream stream = new FileOutputStream("ExcelDemo1.xls");
mappe.write(stream);
} catch (Exception e) {
System.err.println(e);
}
}
public static void main(String [] args) {
new ExcelDemo1();
}
}
Nun kann der Compiler die ganzen importierten Package nicht finden, wie z. B. org.apache.poi.hssf.usermodel.HSSFCell.
Muss ich da noch was besonderes beachten? :bahnhof: