POI Excel API究竟需要什么?

时间:2013-07-23 09:02:09

标签: java apache-poi jexcelapi

我正致力于通过POI API 3.9制作Excel数据的程序。

但目录中有许多Jar文件如下。

poi-3.9-20121203 
poi-excelant-3.9-20121203  
poi-ooxml-3.9-20121203 
poi-ooxml-schemas-3.9-20121203 
poi-scratchpad-3.9-20121203 
ooxml-lib/xmlbeans-2.3.0  
lib/commons-logging-1.1 
..etc.

我只需要创建,阅读和编​​写excel文件。我很困惑什么jar文件需要。

请告诉我我要导入的文件。我在网站上看过很多节目。但我可以找到它。

谢谢

8 个答案:

答案 0 :(得分:3)

在您的情况下,不要过分担心您的依赖关系。只需使用MavenGradle等工具,然后添加POI dependency

<强>的Maven

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.9</version>
</dependency>

<强>摇篮

'org.apache.poi:poi:3.9'

这将为您提供正确的依赖关系。

答案 1 :(得分:3)

您可能需要导入所有这些内容。与许多框架一样,Apache POI在不同的库中被分割。 Apache POI还使用XML Bean等外部库。

Apache POI website上,您会找到组件列表以及所需的目标,请查看Component Map部分。

通常,如果您只处理旧的Excel文件,则不需要poi-ooxml依赖项。

请注意,您的外部依赖项列表列在“组件映射”部分的第二个表的先决条件列中,如此处所述,您还需要其他库:commons-logging,{{ 1}},commons-codec

最后,为了避免使用依赖关系管理带来的麻烦,您可以使用像Maven这样适合您的工具。

答案 2 :(得分:2)

您提到的任务只需要5个罐子

例如,如果我们使用apache 3.9

dom4j-1.6.1.jar
poi-3.9-20121203.jar
poi-ooxml-3.9-0121203.jar
poi-ooxml-schemas-3.9-0121203.jar
xmlbeans-2.3.0.jar

答案 3 :(得分:1)

如果您使用Maven进行项目构建,那么您只需在项目中添加以下两个依赖项 -

<!-- Apache POI -->
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>3.9</version>
</dependency>
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-ooxml</artifactId>
  <version>3.9</version>
</dependency>

这将为您下载所有必需的罐子。

Here是我开发的一个演示项目,展示了excel阅读方法。以上几行取自我的项目pom。这种方法对我来说很好。

答案 4 :(得分:1)

(1)您好,我在这里发布一个链接,您将获得与您的问题相关的所有信息。

http://selenium-testng.com/test-data-through-excel/

(2)您可以从此URL下载所有必需的罐子

http://poi.apache.org/download.html

(3)提取URL并包含classPath中的所有jar

(4)对于MS Excel示例,您可以检查以下站点。它有所有必需的例子。

http://poi.apache.org/spreadsheet/quick-guide.html

答案 5 :(得分:0)

对于独立项目,您需要以下罐子。

共享记录-1.1.jar

DOM4J-1.6.1.jar

jsr173_1.0_api.jar

的log4j-1.2.13.jar

POI-3.8-20120326.jar

POI-OOXML-3.8-20120326.jar

POI-OOXML-架构 - 3.8-20120326.jar

resolver.jar

xbean_xpath.jar

xbean.jar

的xmlbeans-qname.jar

xmlpublic.jar

答案 6 :(得分:0)

您可以使用ver 3.9或ver 3.8代替。但我发现poi ver.3.9删除了一些类(例如org.apache.poi.xssf.usermodel.XSSFWorkbook),直到提供poi-3.8-20120326.jar。因此,请验证您的项目中是否使用了这些类。

希望这有帮助!

答案 7 :(得分:0)

感谢以下链接

http://www.mysamplecode.com/2011/10/android-read-write-excel-file-using.html

为我工作:)

编辑编辑代码:

public static boolean isExternalStorageReadOnly() {
    String extStorageState = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
        return true;
    }
    return false;
}

public static boolean isExternalStorageAvailable() {
    String extStorageState = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
        return true;
    }
    return false;
}
private static boolean saveExcelFile(MainActivity mainActivity, String fileName) {

        // check if available and not read only
        if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
            Log.w("FileUtils", "Storage not available or read only");
            return false;
        }

        boolean success = false;

        //New Workbook
        Workbook wb = new HSSFWorkbook();

        Cell c = null;

        //Cell style for header row
        CellStyle cs = wb.createCellStyle();
        cs.setFillForegroundColor(HSSFColor.LIME.index);
        cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

        //New Sheet
        Sheet sheet1 = null;
        sheet1 = wb.createSheet("Sheet1");

        // Generate column headings
        Row row = sheet1.createRow(0);

        c = row.createCell(0);
        c.setCellValue(DBHelper.INVENTORY_PROJECT_CODE);
        c.setCellStyle(cs);

        c = row.createCell(1);
        c.setCellValue(DBHelper.INVENTORY_PROJECT_UPC_BAR_CODE);
        c.setCellStyle(cs);

        c = row.createCell(2);
        c.setCellValue(DBHelper.INVENTORY_PROJECT_NAME);
        c.setCellStyle(cs);

        c = row.createCell(3);
        c.setCellValue(DBHelper.INVENTORY_PROJECT_QUANTITY);
        c.setCellStyle(cs);

        c = row.createCell(4);
        c.setCellValue(DBHelper.INVENTORY_PROJECT_PRICE);
        c.setCellStyle(cs);


        ArrayList<String> arrayList1 = dbHelper.getAllRecords();

        int rowno = 1;
    int columnno=0;
            for(String red : arrayList1){
                columnno = 0;
                Row row1 = sheet1.createRow(rowno);
                String[] parts = red.split(Pattern.quote(","));
                for(String str : parts){
                    c = row1.createCell(columnno);
                    c.setCellValue(str);
                    c.setCellStyle(cs);
                    columnno++;
                }
                rowno++;
            }

        sheet1.setColumnWidth(0, (15 * 500));
        sheet1.setColumnWidth(1, (15 * 500));
        sheet1.setColumnWidth(2, (15 * 500));
        sheet1.setColumnWidth(3, (15 * 500));
        sheet1.setColumnWidth(4, (15 * 500));


        // Create a path where we will place our List of objects on external storage
        File file = new File(mainActivity.getExternalFilesDir(null), fileName);
        FileOutputStream os = null;

        try {
            os = new FileOutputStream(file);
            wb.write(os);
            Log.w("FileUtils", "Writing file" + file);
            success = true;
        } catch (IOException e) {
            Log.w("FileUtils", "Error writing " + file, e);
        } catch (Exception e) {
            Log.w("FileUtils", "Failed to save file", e);
        } finally {
            try {
                if (null != os)
                    os.close();
            } catch (Exception ex) {
            }
        }

        return success;


}