我有一個111行的excel文件。我需要省略工作表的前兩行,然後使用java和POI讀取文件。如何讀取省略前兩行的excel文件
8
A
回答
26
你必須使用rownum()
。這裏是示例代碼
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
}
}
下面是完整的例子
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);
}
}
4
的Apache POI提供了兩種方式來訪問行和單元格,跳過前兩行一個Excel文件。一個是迭代器,它爲您提供所有條目,另一個是按索引循環。 (POI也會告訴你開始/結束行/列)。迭代器通常使用起來更簡單,但兩者同樣快。
如果您對要提取的行有特定要求,我建議您使用後者。你的代碼想要是這樣的:
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
}
}
}
}
+0
你好先生我有一個查詢「如何只讀最後一行第一個單元格」請幫助我 –
相關問題
- 1. 讀取文本文件並省略行
- 2. 從getResourceAsStream讀取文件省略換行
- 3. 如何用console.log省略文件/行號
- 4. Python - 從txt文件讀取前兩行
- 5. StreamReader讀取文件和StreamWriter寫入結果省略第一行?
- 6. 省略換行符在讀取文件與C++
- 7. 從文本文件中讀取行但跳過前兩行
- 8. C#讀取excel文件忽略第一行
- 9. 如何省略文件路徑壓縮?
- 10. 讀取Excel文件
- 11. 讀取Excel文件
- 12. 讀取excel文件
- 13. 嘗試讀取Excel文件中的行
- 14. 如何省略excel中的第一行導入到DataTable中
- 15. 如何痛飲省略向前聲明
- 16. 如何省略模塊前綴?
- 17. 如何使用AJAX讀取excel文件?
- 18. 如何從Excel文件讀取?
- 19. 如何從Excel文件讀取數據?
- 20. 如何從動態excel文件讀取?
- 21. 如何從excel文件中讀取
- 22. 如何用Perl讀取excel文件?
- 23. 如何在Perl中讀取Excel文件?
- 24. 如何捕捉兩個文件,但分別省略最後/第一行?
- 25. 的Java讀取Excel文件
- 26. (Python)CSV閱讀器省略一行
- 27. 如何只讀取Excel文件中使用過的行
- 28. 如何在使用spark讀取csv.file時省略標題?
- 29. CSS文本溢出前置省略號
- 30. NetBeans PHP運行配置:如何省略doc-root文件夾?
我需要讀取.xls和.xlsx文件,它有9列和100行的內容。讀取的內容必須添加到列表中,並作爲批量插入插入到sql db中,並將值shud插入到相應的列中。請幫幫我 !! – Priya