Ho un file excel di 111 righe. Devo omettere le prime due righe del foglio e poi leggere il file usando java e POI.Come leggere il file excel omettendo le prime due righe
risposta
si deve saltare prime due righe utilizzando rownum()
Qui è il codice di esempio
HSSFWorkbook workBook = new HSSFWorkbook (fileSystem);
HSSFSheet sheet = workBook.getSheetAt (0);
Iterator<HSSFRow> rows = sheet.rowIterator();
while (rows.hasNext())
{
HSSFRow row = rows.next();
// display row number in the console.
System.out.println ("Row No.: " + row.getRowNum());
if(row.getRowNum()==0 || row.getRowNum()==1){
continue; //just skip the rows if row number is 0 or 1
}
}
Qui è un esempio completo
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
public class POIExcelReader
{
/** Creates a new instance of POIExcelReader */
public POIExcelReader()
{}
@SuppressWarnings ("unchecked")
public void displayFromExcel (String xlsPath)
{
InputStream inputStream = null;
try
{
inputStream = new FileInputStream (xlsPath);
}
catch (FileNotFoundException e)
{
System.out.println ("File not found in the specified path.");
e.printStackTrace();
}
POIFSFileSystem fileSystem = null;
try
{
fileSystem = new POIFSFileSystem (inputStream);
HSSFWorkbook workBook = new HSSFWorkbook (fileSystem);
HSSFSheet sheet = workBook.getSheetAt (0);
Iterator<HSSFRow> rows = sheet.rowIterator();
while (rows.hasNext())
{
HSSFRow row = rows.next();
if(row.getRowNum()==0 || row.getRowNum()==1){
continue; //just skip the rows if row number is 0 or 1
}
// once get a row its time to iterate through cells.
Iterator<HSSFCell> cells = row.cellIterator();
while (cells.hasNext())
{
HSSFCell cell = cells.next();
System.out.println ("Cell No.: " + cell.getCellNum());
/*
* Now we will get the cell type and display the values
* accordingly.
*/
switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_NUMERIC :
{
// cell type numeric.
System.out.println ("Numeric value: " + cell.getNumericCellValue());
break;
}
case HSSFCell.CELL_TYPE_STRING :
{
// cell type string.
HSSFRichTextString richTextString = cell.getRichStringCellValue();
System.out.println ("String value: " + richTextString.getString());
break;
}
default :
{
// types other than String and Numeric.
System.out.println ("Type not supported.");
break;
}
}
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main (String[] args)
{
POIExcelReader poiExample = new POIExcelReader();
String xlsPath = "c://test//test.xls";
poiExample.displayFromExcel (xlsPath);
}
}
Apache POI fornisce due modi per accedere alle righe e le celle a un file Excel. Uno è un iteratore che ti dà tutte le voci, l'altro è di andare in loop per indice. (POI ti dirà anche le righe/colonne di inizio/fine). L'iteratore è spesso più semplice da utilizzare, ma entrambi sono altrettanto veloci.
Se si dispone di requisiti specifici per le righe da recuperare, suggerirei di utilizzare quest'ultimo. Il tuo codice dovrebbe essere qualcosa del tipo:
int FIRST_ROW_TO_GET = 2; // 0 based
Sheet s = wb.getSheetAt(0);
for (int i = FIRST_ROW_TO_GET; i < s.getLastRowNum(); i++) {
Row row = s.getRow(i);
if (row == null) {
// The whole row is blank
}
else {
for (int cn=row.getFirstCellNum(); cn<row.getLastCellNum(); cn++) {
Cell c = row.getCell(cn, Row.RETURN_BLANK_AS_NULL);
if (c == null) {
// The cell is empty
} else {
// Process the cell
}
}
}
}
ciao signore, ho una domanda "come leggere solo l'ultima riga della prima cella" per favore aiutatemi –
- 1. Elimina le prime quattro righe in Excel con PowerShell
- 2. Leggi le righe da un file di testo, ma salta le prime due righe
- 3. Legge solo le prime righe di testo da un file
- 4. Ottenere le prime due righe di testo da una cella wraped in excel
- 5. Erlang legge dal file le prime 5 righe
- 6. Come diffare le prime righe di due file senza file intermedio
- 7. Sintassi R per selezionare tutte le prime due righe tranne
- 8. Elimina le prime X righe da un file PHP
- 9. Come unire le prime n righe in un file
- 10. Come leggere il file Excel in Scala
- 11. Come cancellare le prime due righe e le ultime quattro righe da un file di testo con bash?
- 12. Modo di leggere le prime righe per il dataframe di panda
- 13. Elenca le prime righe di ogni file in una directory
- 14. Pyodbc - stampa le prime 10 righe (python)
- 15. Ottieni le prime righe di Wikipedia Articolo
- 16. Come selezionare le prime N righe di ogni gruppo?
- 17. Come leggere da XLSX (Excel)?
- 18. SQL: selezionare solo le prime 10 righe?
- 19. Come rimuovere tutte le righe da una tabella tranne le prime due e l'ultima?
- 20. Selezionare le prime 2 righe in Hive
- 21. leggere il file excel utilizzando Apache POI
- 22. Qual è il contrario della testa? Voglio tutte le prime N righe di un file
- 23. Come restituire le prime 100 righe per un valore di colonna e quindi randomizzare le prime 100 righe?
- 24. Come leggere le ultime righe "n" del file di registro
- 25. Come leggere le righe dal file mmap in python?
- 26. Script di shell - leggere due righe di file e leggere una riga di un altro file
- 27. Come randomizzare le righe di Excel
- 28. Come selezionare le prime 100 righe in Oracle?
- 29. Come leggere n righe da un file in clojure
- 30. Elimina tutte le righe duplicate Excel vba
Ho bisogno di leggere il contenuto del file .xls e .xlsx, che ha 9 colonne e 100 righe. I contenuti letti devono essere aggiunti a un elenco e inseriti in un dl sql come inserto batch e il valore shud essere inserito nella colonna corrispondente. mi aiuti per favore !! – Priya