|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Jul 2007
Città: Cassano M.go (Va)
Messaggi: 631
|
[Java] Eseguire un'operazione su molti file
Salve gente, sto facendo un programmino in java che deve leggere molti file excel di una directory.
Ora riesco a leggerne uno solo, dato che gli passo la directory in cui si trova il file con l'istruzione Codice:
InputStream input = new FileInputStream("D:\\Documents and Settings\\nome.xls");
__________________
PC: Intel Core i5 4690K @ 3,5 Ghz | VGA Gigabyte GTX 970 G1 Gaming | RAM G Skill Ares 1866 Mhz (2x4GB) | HDD WD Caviar Blue 1TB | SSD Samsung 840 Evo 250GB | MoBo AsRock Z97 Extreme 4 Router: Netgear dg834g v5 Notebook: Asus x53sv: Intel i7 2630qm | Geforce gt630 | RAM 4GB | SSD 250GB Cell: Iphone 8 64GB Black Tablet: Ipad Air 16GB + 4G Grigio siderale |
|
|
|
|
|
#2 | |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Quote:
Codice:
File parent = new File("D:\\Documents and Settings\\");
Codice:
String[] files = parent.list(new FileNameFilter() {
@Override public void accept(File dir, String name) {
return dir.isFile() &&
name.toLowercase().endsWith(".xls");
}
});
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) Ultima modifica di banryu79 : 29-07-2011 alle 21:24. |
|
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Jul 2007
Città: Cassano M.go (Va)
Messaggi: 631
|
Domani testo la tua dritta e ti faccio sapere....
__________________
PC: Intel Core i5 4690K @ 3,5 Ghz | VGA Gigabyte GTX 970 G1 Gaming | RAM G Skill Ares 1866 Mhz (2x4GB) | HDD WD Caviar Blue 1TB | SSD Samsung 840 Evo 250GB | MoBo AsRock Z97 Extreme 4 Router: Netgear dg834g v5 Notebook: Asus x53sv: Intel i7 2630qm | Geforce gt630 | RAM 4GB | SSD 250GB Cell: Iphone 8 64GB Black Tablet: Ipad Air 16GB + 4G Grigio siderale |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Jul 2007
Città: Cassano M.go (Va)
Messaggi: 631
|
Non ho capito come dovrei fare, al momento ho scritto così:
Codice:
public static void main( String [] args ) {
try {
//Apriamo uno stream per recuperare il file
InputStream input = new FileInputStream("D:\\Documents and Settings\\....\\file.xls
//e lo rendiamo utilizzabile dalle librerie POI
POIFSFileSystem fs = new POIFSFileSystem(input);
//un file excel è un WorkBook contenente diversi fogli
HSSFWorkbook wb = new HSSFWorkbook(fs);
//di cui ne leggiamo il primo
HSSFSheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.rowIterator();
__________________
PC: Intel Core i5 4690K @ 3,5 Ghz | VGA Gigabyte GTX 970 G1 Gaming | RAM G Skill Ares 1866 Mhz (2x4GB) | HDD WD Caviar Blue 1TB | SSD Samsung 840 Evo 250GB | MoBo AsRock Z97 Extreme 4 Router: Netgear dg834g v5 Notebook: Asus x53sv: Intel i7 2630qm | Geforce gt630 | RAM 4GB | SSD 250GB Cell: Iphone 8 64GB Black Tablet: Ipad Air 16GB + 4G Grigio siderale |
|
|
|
|
|
#5 |
|
Member
Iscritto dal: Sep 2010
Messaggi: 102
|
non ho idea di cosa tu abbia fatto, ma non hai usato il list a quanto pare
metti un list tra un while has next element... e sei apposto |
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Jul 2007
Città: Cassano M.go (Va)
Messaggi: 631
|
Allo stato attuale il mio codice è il seguente:
Codice:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Collection;
import java.util.HashMap;
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;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
public class FromXLS {
//Oggetto Hash Map
HashMap hMap = new HashMap();
@SuppressWarnings("deprecation")
public static void main( String [] args ) {
try {
//Apriamo uno stream per recuperare il file
InputStream input = new FileInputStream("D:\\.....\\file.xls");
//e lo rendiamo utilizzabile dalle librerie POI
POIFSFileSystem fs = new POIFSFileSystem(input);
//un file excel è un WorkBook contenente diversi fogli
HSSFWorkbook wb = new HSSFWorkbook(fs);
//di cui ne leggiamo il primo
HSSFSheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.rowIterator();
//Iteriamo su tutte le righe del foglio
while( rows.hasNext() ) {
HSSFRow row = (HSSFRow) rows.next();
System.out.println( "N. Riga = " + row.getRowNum() );
Iterator cells = row.cellIterator();
while( cells.hasNext() ) {
//Per ogni riga scorriamo tutte le celle
HSSFCell cell = (HSSFCell) cells.next();
System.out.println( "N. Cella = "
+ cell.getCellNum() );
/*Per ogni cella verifichiamo la tipologia del dato
* che contiene e lo scriviamo nel canale di output */
switch ( cell.getCellType() ) {
//Tipo numerico
case HSSFCell.CELL_TYPE_NUMERIC:
if (cell.getNumericCellValue() != 0)
System.out.println((int)cell.getNumericCellValue() );
break;
//Tipo stringa
case HSSFCell.CELL_TYPE_STRING:
System.out.println(cell.getStringCellValue() );
break;
//Tipo formula
case HSSFCell.CELL_TYPE_FORMULA:
if (cell.getNumericCellValue() != 0)
System.out.println((int)cell.getNumericCellValue());
break;
//Altro
default:
System.out.println("unsuported cell type" );
break;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
Deve creare una Hash Map in cui il campo chiave sia una determinata colonna e il campo valore sia un numero che viene incrementato. Consigli su come operare?
__________________
PC: Intel Core i5 4690K @ 3,5 Ghz | VGA Gigabyte GTX 970 G1 Gaming | RAM G Skill Ares 1866 Mhz (2x4GB) | HDD WD Caviar Blue 1TB | SSD Samsung 840 Evo 250GB | MoBo AsRock Z97 Extreme 4 Router: Netgear dg834g v5 Notebook: Asus x53sv: Intel i7 2630qm | Geforce gt630 | RAM 4GB | SSD 250GB Cell: Iphone 8 64GB Black Tablet: Ipad Air 16GB + 4G Grigio siderale |
|
|
|
|
|
#7 |
|
Senior Member
Iscritto dal: Jul 2007
Città: Cassano M.go (Va)
Messaggi: 631
|
I file xls sono fatti così:
![]() Uploaded with ImageShack.us A me interessano solo le colonne KEY e TOT. Quindi ogni volta che si incontra la stessa KEY in un file xls seguente, bisogna sommare il valore di TOT vecchio con quello appena letto. In ogni file xls sono presenti le stesse KEY, quindi credo che basti memorizzarle una volta sola nell'Hash Map. Consigli su come operare?
__________________
PC: Intel Core i5 4690K @ 3,5 Ghz | VGA Gigabyte GTX 970 G1 Gaming | RAM G Skill Ares 1866 Mhz (2x4GB) | HDD WD Caviar Blue 1TB | SSD Samsung 840 Evo 250GB | MoBo AsRock Z97 Extreme 4 Router: Netgear dg834g v5 Notebook: Asus x53sv: Intel i7 2630qm | Geforce gt630 | RAM 4GB | SSD 250GB Cell: Iphone 8 64GB Black Tablet: Ipad Air 16GB + 4G Grigio siderale |
|
|
|
|
|
#8 |
|
Senior Member
Iscritto dal: Jul 2007
Città: Cassano M.go (Va)
Messaggi: 631
|
Sono andato avanti un pochino, ora sono "riuscito" a fargli prendere l'intera lista di file in una data directory.
Codice:
public class FromXLS {
private static FilenameFilter xls;
//Metodo che prende in input un file excel ed estrae il contenuto di righe e colonne
@SuppressWarnings("deprecation")
public static void elaboraFile(File excel){
try {
//Apriamo uno stream per recuperare il file
InputStream input = new FileInputStream(excel);
//e lo rendiamo utilizzabile dalle librerie POI
POIFSFileSystem fs = new POIFSFileSystem(input);
//un file excel è un WorkBook contenente diversi fogli
HSSFWorkbook wb = new HSSFWorkbook(fs);
//di cui ne leggiamo il primo
HSSFSheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.rowIterator();
//Iteriamo su tutte le righe del foglio
while( rows.hasNext() ) {
HSSFRow row = (HSSFRow) rows.next();
//System.out.println( "N. Riga = " + row.getRowNum() );
Iterator cells = row.cellIterator();
while( cells.hasNext() ) {
//Per ogni riga scorriamo tutte le celle
HSSFCell cell = (HSSFCell) cells.next();
/*Per ogni cella verifichiamo la tipologia del dato
* che contiene e lo scriviamo nel canale di output */
switch ( cell.getCellType() ) {
//Tipo numerico
case HSSFCell.CELL_TYPE_NUMERIC:
if (cell.getCellNum() == 2 || cell.getCellNum() == 5)
System.out.println((int)cell.getNumericCellValue() );
break;
//Tipo stringa
case HSSFCell.CELL_TYPE_STRING:
if (cell.getCellNum() == 2 || cell.getCellNum() == 5)
System.out.println(cell.getStringCellValue() );
break;
//Tipo formula
case HSSFCell.CELL_TYPE_FORMULA:
if (cell.getCellNum() == 2 || cell.getCellNum() == 5)
System.out.println((int)cell.getNumericCellValue());
break;
//Altro
default:
if (cell.getCellNum() == 2 || cell.getCellNum() == 5)
System.out.println("unsuported cell type" );
break;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
//Directory contenente tutti i file .xls da elaborare
File mainDir = new File("I://rep_ vorwerk//Luglio_2011");
//Filtro sui file .xls
File files[] = mainDir.listFiles(xls);
//Oggetto Hash Map
Map<String, Integer> map = new HashMap<String, Integer>();
//Per tutti i file della directory
for( File file : files ){
System.out.println("Nome file: " + file);
elaboraFile(file);
}
}
}
__________________
PC: Intel Core i5 4690K @ 3,5 Ghz | VGA Gigabyte GTX 970 G1 Gaming | RAM G Skill Ares 1866 Mhz (2x4GB) | HDD WD Caviar Blue 1TB | SSD Samsung 840 Evo 250GB | MoBo AsRock Z97 Extreme 4 Router: Netgear dg834g v5 Notebook: Asus x53sv: Intel i7 2630qm | Geforce gt630 | RAM 4GB | SSD 250GB Cell: Iphone 8 64GB Black Tablet: Ipad Air 16GB + 4G Grigio siderale |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 17:03.





















