使用HSSF来读取excel文件 [英] using HSSF to read an excel file

查看:200
本文介绍了使用HSSF来读取excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Emp ID名称薪资

Emp ID Name Salary

1.0 john 2000000.0

1.0 john 2000000.0

2.0 dean 4200000.0

2.0 dean 4200000.0

3.0 sam 2800000.0

3.0 sam 2800000.0

4.0 cass 600000.0

4.0 cass 600000.0

我创建了此代码:

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
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;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class sample2
 {
    public static void main(String[] args) {
    new sample2().sample2();
}
 }


FileInputStream file = new FileInputStream(new File("C:\\test.xls"));
//Get the workbook instance for XLS file 
HSSFWorkbook workbook = new HSSFWorkbook(test);

//Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);

//Get iterator to all the rows in current sheet
Iterator<Row> rowIterator = sheet.iterator();

//Get iterator to all cells of current row
Iterator<Cell> cellIterator = row.cellIterator();


try {

  FileInputStream file = new FileInputStream(new File("C:\\test.xls"));

  //Get the workbook instance for XLS file 
  HSSFWorkbook workbook = new HSSFWorkbook(file);

  //Get first sheet from the workbook
  HSSFSheet sheet = workbook.getSheetAt(0);

  //Iterate through each rows from first sheet
   Iterator<Row> rowIterator = sheet.iterator();
   while(rowIterator.hasNext()) {
     Row row = rowIterator.next();

     //For each row, iterate through each columns
    Iterator<Cell> cellIterator = row.cellIterator();
    while(cellIterator.hasNext()) {

        Cell cell = cellIterator.next();

        switch(cell.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                System.out.print(cell.getBooleanCellValue() + "\t\t");
                break;
            case Cell.CELL_TYPE_NUMERIC:
                System.out.print(cell.getNumericCellValue() + "\t\t");
                break;
            case Cell.CELL_TYPE_STRING:
                System.out.print(cell.getStringCellValue() + "\t\t");
                break;
        }
    }
    System.out.println("");
   }
   file.close();
   FileOutputStream out = 
     new FileOutputStream(new File("C:\\test.xls"));
  workbook.write(out);
  out.close();

} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();

}

这个excel文件使用POI库。我的编辑器是Eclipse。但是当我运行程序时,我采取了这一点:异常在线程mainjava.lang.Error:未解决的编译问题:
方法sample2()未定义类型sample2

for reading the content from this excel file using POI library. My editor is Eclipse. But when i run the program I took this: Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method sample2() is undefined for the type sample2

at sample2.main(sample2.java:17)

任何帮助?
谢谢你提前!

Any help? Thank u in advance!

推荐答案

public class sample2
 {
    public static void main(String[] args) {
    new sample2().sample2(); // This is wrong too.
    }
 }

之后的所有代码是无意义的。您的课程基本上以第二个} 结束。

All your code after this is pointless. Your class has basically ended with the second }.

您可能希望将所有内容移至 main()方法。

You might want to move all that within your main() method.

另外,这段代码在 main()方法 new sample2()。sample2(); 是错误的。

Also, this piece of code in the main() method new sample2().sample2(); is wrong.

应该是这样的

sample2 s = new sample2();

这篇关于使用HSSF来读取excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆