eclipse 使用Apache POI和eclipse作为编辑器读取excel文件的java程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15700512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:17:37  来源:igfitidea点击:

java program for read an excel file using Apache POI and eclipse as editor

javaeclipseapache-poi

提问by userefoikon

I have created this code to read an excel file which contain boolean, string and numeric values using APACHE POI.

我创建了这段代码来使用 APACHE POI 读取包含布尔值、字符串和数字值的 excel 文件。

import java.io.*;
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 ExcelRead {

public static void main(String[] args) throws Exception {
    File excel = new File ("C:\Users\...\Documents\test.xls");
    FileInputStream fis = new FileInputStream(excel);

    HSSFWorkbook wb = new HSSFWorkbook(fis);
    HSSFSheet ws = wb.getSheet("Input");

    int rowNum = ws.getLastRowNum()+1;
    int colNum = ws.getRow(0).getLastCellNum();
    String[][] data = new String[rowNum][colNum];


    for (int i=0; i<rowNum; i++){
        HSSFRow row = ws.getRow(i);
            for (int j=0; j<colNum; j++){
                HSSFCell cell = row.getCell(j);
                String value = cellToString(cell);
                data[i][j] = value;
                System.out.println("The value is" + value);

            }
       }
    }

public static String cellToString (HSSFCell cell){

int type;
Object result;
type = cell.getCellType();

     switch(type) {


    case 0://numeric value in excel
        result = cell.getNumericCellValue();
        break;
    case 1: //string value in excel
        result = cell.getStringCellValue();
        break;
    case 2: //boolean value in excel
        result = cell.getBooleanCellValue ();
        break;
    default:
        throw new RunTimeException("There are no support for this type of 
                                                cell");                      
}

return result.toString();


}

When I run it using eclipse I get this warning:

当我使用 eclipse 运行它时,我收到此警告:

Usage: AddDimensionedImage imageFile outputFile

用法:AddDimensionedImage imageFile outputFile

Can anyone help me?

谁能帮我?

回答by Serkan Ar?ku?u

You are trying to run a different project/class named "AddDimensionedImage".

您正在尝试运行名为“AddDimensionedImage”的不同项目/类。

Right click on your ExcelRead file on Eclipse and choose Run As -> Java Application

右键单击 Eclipse 上的 ExcelRead 文件,然后选择 Run As -> Java Application