gpt4 book ai didi

java - 使用HSSF读取excel文件

转载 作者:行者123 更新时间:2023-12-01 14:43:58 24 4
gpt4 key购买 nike

员工 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中的异常: Unresolved 编译问题: 类型sample2 的方法sample2() 未定义

at sample2.main(sample2.java:17)

有什么帮助吗?预先感谢您!

最佳答案

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

此后的所有代码都毫无意义。您的类(class)基本上以第二个 } 结束。

您可能想在 main() 方法中移动所有内容。

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

应该是这样的

sample2 s = new sample2();

关于java - 使用HSSF读取excel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15654338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com