-1

您好我正在尝试将 Excel 文件读入我的 android 应用程序。我尝试使用此代码:

try {
    POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    HSSFSheet sheet = wb.getSheetAt(0);
    HSSFRow row;
    HSSFCell cell;

    int rows; // No of rows
    rows = sheet.getPhysicalNumberOfRows();

    int cols = 0; // No of columns
    int tmp = 0;

    // This trick ensures that we get the data properly even if it doesn't start from first few rows
    for(int i = 0; i < 10 || i < rows; i++) {
        row = sheet.getRow(i);
        if(row != null) {
            tmp = sheet.getRow(i).getPhysicalNumberOfCells();
            if(tmp > cols) cols = tmp;
        }
    }

    for(int r = 0; r < rows; r++) {
        row = sheet.getRow(r);
        if(row != null) {
            for(int c = 0; c < cols; c++) {
                cell = row.getCell((short)c);
                if(cell != null) {
                    // Your code here
                }
            }
        }
    }
} catch(Exception ioe) {
    ioe.printStackTrace();
}

我想知道是否应该导入任何东西或任何库?

4

1 回答 1

0

如果您使用 Eclipse 作为您的 IDE(我假设您是因为这与 Android 相关),您可以按 ctrl + shift + O 并且它会自动导入您需要的任何内容,假设您的构建路径中包含库。但是,听起来您没有 Apache POI 库。首先,您需要下载库。下载它们后,您需要使用以下方法将它们添加到构建路径中:(再次假设您使用 Eclipse 作为 IDE)

  1. 在项目资源管理器中右键单击您的项目。
  2. 选择构建路径 -> 配置构建路径
  3. 选择库选项卡。
  4. 单击“添加 JAR...”
  5. 查找并选择您刚刚下载的 JAR 文件。(您可能需要将 JAR 复制到您的项目中,以便您能够选择它)

或者,您可以将库添加到您的项目文件夹之一(通常是“lib”文件夹),在 Eclipse 中右键单击它并选择“添加到构建路径”。

它现在应该添加到您的构建路径中。再次尝试按 ctrl + shift + O ,将为您进行必要的导入。

于 2012-12-03T22:53:54.780 回答