如何读取省略前两行的excel文件

如何读取省略前两行的excel文件,excel,apache-poi,Excel,Apache Poi,我有一个111行的excel文件。我需要省略工作表的前两行,然后使用java和POI读取文件。您必须使用rownum()跳过前两行。下面是示例代码 HSSFWorkbook workBook = new HSSFWorkbook (fileSystem); HSSFSheet sheet = workBook.getSheetAt (0); Iterator<HSSFRow> rows = sheet.rowIterator (); whil

我有一个111行的excel文件。我需要省略工作表的前两行,然后使用java和POI读取文件。

您必须使用
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
  }
}
HSSFWorkbook工作簿=新的HSSFWorkbook(文件系统);
HSSFSheet sheet=workBook.getSheetAt(0);
迭代器行=sheet.rowditerator();
while(rows.hasNext())
{
HSSFRow row=rows.next();
//在控制台中显示行编号。
System.out.println(“行编号:”+Row.getRowNum());
if(row.getRowNum()==0 | | row.getRowNum()==1){
continue;//如果行号为0或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);
}
}
import org.apache.poi.hssf.usermodel.HSSFCell;
导入org.apache.poi.hssf.usermodel.HSSFRichTextString;
导入org.apache.poi.hssf.usermodel.HSSFRow;
导入org.apache.poi.hssf.usermodel.HSSFSheet;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.poifs.filesystem.poifsfsystem;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.Iterator;
公共类XcelReader
{
/**创建一个新的XcelReader实例*/
公共XcelReader()
{}
@抑制警告(“未选中”)
公共void displayFromExcel(字符串xlsPath)
{
InputStream InputStream=null;
尝试
{
inputStream=新文件inputStream(xlsPath);
}
catch(filenotfounde异常)
{
System.out.println(“在指定路径中找不到文件”);
e、 printStackTrace();
}
POIFSFileSystem=null;
尝试
{
fileSystem=新的POIFSFfilesystem(inputStream);
HSSF工作簿=新的HSSF工作簿(文件系统);
HSSFSheet sheet=workBook.getSheetAt(0);
迭代器行=sheet.rowditerator();
while(rows.hasNext())
{
HSSFRow row=rows.next();
if(row.getRowNum()==0 | | row.getRowNum()==1){
continue;//如果行号为0或1,则跳过行
}
//一旦得到一行,就需要遍历单元格。
迭代器单元格=row.cellIterator();
while(cells.hasNext())
{
HSSFCell cell=cells.next();
System.out.println(“单元格编号:”+Cell.getCellNum());
/*
*现在我们将获取单元格类型并显示值
*因此。
*/
开关(cell.getCellType())
{
案例HSSFCell.CELL\u类型\u数字:
{
//单元格类型为数字。
System.out.println(“数值:+cell.getNumericCellValue());
打破
}
案例HSSFCell.CELL\u类型\u字符串:
{
//单元格类型字符串。
HSSFRichTextString richTextString=cell.getRichStringCellValue();
System.out.println(“字符串值:+richTextString.getString());
打破
}
违约:
{
//字符串和数字以外的类型。
System.out.println(“不支持类型”);
打破
}
}
}
}
}
捕获(IOE异常)
{
e、 printStackTrace();
}
}
公共静态void main(字符串[]args)
{
POIExcelReader-poiExample=新的POIExcelReader();
字符串xlsPath=“c://test//test.xls”;
从Excel(xlsPath)显示示例;
}
}

ApachePOI提供了两种访问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
         }
      }
   }
}
int FIRST_ROW_TO_GET=2;//基于0
表s=wb.getSheetAt(0);
for(int i=FIRST_ROW_TO_GET;i对于(int cn=row.getFirstCellNum();cnI需要读取.xls和.xlsx文件的内容,该文件有9列和100行。读取的内容必须添加到列表中,并作为批插入插入插入到sql db中,值应该插入到相应的列中。请帮助我!!您好,先生,我有一个查询“如何仅读取最后一行第一个单元格”请帮助我