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
}
}
現在、実行中に「メイン クラスが見つかりません。プログラムは終了します」というポップアップ エラー メッセージが表示されます。どこで間違いを犯すことができますか。私を助けてください。前もって感謝します...