我是 Apache Poi 和 Eclipse IDE 的新成员。我需要使用 Excel 文件,我必须在其中生成新的电子表格并需要阅读 Excel 工作表的所有内容。所以我已经用一个可以生成excel表的示例代码进行了说明。根据要求,我已将 poi-2.5.1.jar 作为外部 .jar 文件添加到项目中。这是代码,
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import java.io.*;
import java.util.*;
public class Ex1 {
//public Ex1() { }
public static void main(String[] a)
{
//public Ex1() { }
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) {
} catch (IOException e) {
e.printStackTrace();
}
// TODO Auto-generated constructor stub
}
}
现在我收到“找不到主类。程序将退出”在尝试运行时弹出错误消息。我在哪里可以犯错误。请帮我。提前致谢...