Emp ID名称薪资
1.0约翰2000000.0
2.0院长4200000.0
3.0 sam 2800000.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();}
用于使用POI库从此excel文件中读取内容。我的编辑器是Eclipse。但是当我运行程序时,我遇到了这样的问题:线程"main“中的异常java.lang.Error:未解决的编译问题:未为类型sample2定义sample2()方法
at sample2.main(sample2.java:17)有什么帮助吗?提前谢谢你!
发布于 2013-03-27 16:20:56
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-27 16:27:24
删除代码中的最后一个大括号:
{
public static void main(String[] args) {
new sample2().sample2();}}
然后创建一个名为test2()的方法,如下所示:
public void sample2(){ //Put your code here }https://stackoverflow.com/questions/15654338
复制相似问题