1

Emp ID 姓名 薪水

1.0 约翰 2000000.0

2.0 院长 4200000.0

3.0 山姆 2800000.0

4.0 卡斯 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();

}

使用 POI 库从这个 excel 文件中读取内容。我的编辑器是 Eclipse。但是当我运行程序时,我采取了这个:线程“main”java.lang.Error中的异常:未解决的编译问题:方法sample2()未定义类型sample2

at sample2.main(sample2.java:17)

有什么帮助吗?提前谢谢你!

4

2 回答 2

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

在此之后的所有代码都是毫无意义的。你的课基本上以第二课结束了}

您可能希望在您的main()方法中移动所有这些。

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

应该是这样的

sample2 s = new sample2();
于 2013-03-27T08:20:56.253 回答
0

删除代码中的最后一个花括号:

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

} }

然后创建一个名为 test2() 的方法,如下所示:

public void sample2(){ //Put your code here }
于 2013-03-27T08:27:23.673 回答