PoiReadExcelFile类将'poi-test.xls'文件读入HSSFWorkbook对象

时间:2013-03-29 13:13:53

标签: java apache-poi

我已创建此代码,PoiReadExcelFile类将在'poi-test.xls'文件中读入HSSFWorkbook对象。然后将“POI工作表”读入HSSFWorksheet对象,然后读取A1,B1,C1和D1单元格中的值并将其显示为标准输出。

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;

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;

public class PoiReadExcelFile {
public static void main(String[] args) {
    try {
        FileInputStream fileInputStream = new FileInputStream("poi-    

      test.xls");
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
        HSSFSheet worksheet = workbook.getSheet("POI Worksheet");
        HSSFRow row1 = worksheet.getRow(0);
        HSSFCell cellA1 = row1.getCell((short) 0);
        String a1Val = cellA1.getStringCellValue();
        HSSFCell cellB1 = row1.getCell((short) 1);
        String b1Val = cellB1.getStringCellValue();
        HSSFCell cellC1 = row1.getCell((short) 2);
        boolean c1Val = cellC1.getBooleanCellValue();
        HSSFCell cellD1 = row1.getCell((short) 3);
        Date d1Val = cellD1.getDateCellValue();

        System.out.println("A1: " + a1Val);
        System.out.println("B1: " + b1Val);
        System.out.println("C1: " + c1Val);
        System.out.println("D1: " + d1Val);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 }

当我运行此代码时,我得到了这个: 用法:BiffDrawingToXml [options] inputWorkbook 选项:   -exclude-workbook排除工作簿级记录   -sheet-indexes具有指定索引的输出表   -sheet-namek具有指定名称的输出表

有什么问题?

1 个答案:

答案 0 :(得分:1)

请使用右键单击运行选项运行类文件。上面的消息是在唯一的JAR文件运行时出现的。