简体   繁体   English

我应该导入什么才能正确使用以下代码?

[英]What should I import to be able to use the following code correctly?

Hi Im trying to read an Excel file into my android application.嗨,我正在尝试将 Excel 文件读入我的 android 应用程序。 I tried using this code:我尝试使用此代码:

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();
}

I was wondering if I should import anything or any library?我想知道我是否应该导入任何东西或任何库?

If you are using Eclipse as your IDE (I assume you are since this is Android related) you can press ctrl + shift + O and it will automatically import anything you need, assuming you have the libraries included in your build path.如果你使用 Eclipse 作为你的 IDE(我假设你是因为这是 Android 相关的)你可以按 ctrl + shift + O 并且它会自动导入你需要的任何东西,假设你的构建路径中包含了库。 However, it sounds like you don't have the Apache POI libraries.但是,听起来您没有 Apache POI 库。 First you need to download the libraries.首先,您需要下载库。 After they are downloaded you need to add them to your build path using the following method: (Again assuming that you are using Eclipse as your IDE)下载它们后,您需要使用以下方法将它们添加到构建路径中:(再次假设您使用 Eclipse 作为 IDE)

  1. Right click your project in the project explorer.在项目资源管理器中右键单击您的项目。
  2. Choose Build Path -> Configure Build Path选择构建路径 -> 配置构建路径
  3. Choose the library tab.选择库选项卡。
  4. Click "Add JARs..."单击“添加 JAR...”
  5. Find and select the JAR file that you just downloaded.查找并选择您刚刚下载的 JAR 文件。 (You might need to copy the JAR into your project so that you are able to select it) (您可能需要将 JAR 复制到您的项目中,以便您能够选择它)

Alternatively, you can add the library to one of your project folders (typically a "lib" folder), right click it in Eclipse and select "Add to Build Path".或者,您可以将库添加到您的项目文件夹之一(通常是“lib”文件夹),在 Eclipse 中右键单击它并选择“添加到构建路径”。

It should now be added to your build path.现在应该将它添加到您的构建路径中。 Again try hitting ctrl + shift + O and the necessary imports will be made for you.再次尝试按 ctrl + shift + O 并为您制作必要的导入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM