【问题标题】:How to import import org.apache.poi.ss.usermodel.Row error?如何导入 import org.apache.poi.ss.usermodel.Row 错误?
【发布时间】:2018-02-27 20:39:36
【问题描述】:

我正在尝试使用 Apache 将数据写入 Excel

我参考这个链接:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/

单元格和行未导入

这是我的 java 类代码:

我下载Apache jar file here

下面是我的java代码:

 try {
            String FILE_NAME = "/tmp/MyFirstExcel.xlsx";
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Datatypes in Java");
            Object[][] datatypes = {
                                  {"Datatype", "Type", "Size(in bytes)"},
                                  {"int", "Primitive", 2},
                                  {"float", "Primitive", 4},
                                  {"double", "Primitive", 8},
                                  {"char", "Primitive", 1},
                                  {"String", "Non-Primitive", "No fixesize"}
                          };
                int rowNum = 0;
                System.out.println("Creating excel");

                          for (Object[] datatype : datatypes) {
                              Row row = sheet.createRow(rowNum++);
                              int colNum = 0;
                              for (Object field : datatype) {
                                  Cell cell = row.createCell(colNum++);
                                  if (field instanceof String) {
                                      cell.setCellValue((String) field);
                                  } else if (field instanceof Integer) {
                                      cell.setCellValue((Integer) field);
                                  }
                              }
                          }
                          FileOutputStream outputStream = new FileOutputStream(FILE_NAME);
                          workbook.write(outputStream);
                          workbook.close();


                      } catch (IOException e) {
                          e.printStackTrace();
                      }
}

Build.grid

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    androidTestCompile 'com.android.support:support-annotations:26.+'
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:26.+'
    compile 'com.android.support:support-v4:26.+'
    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'
    compile 'com.android.support:cardview-v7:25.2.0'

    testCompile 'junit:junit:4.12'
}

【问题讨论】:

标签: java android excel apache maven-jar-plugin


【解决方案1】:

简单你需要添加jar文件

https://mvnrepository.com/artifact/org.apache.poi/poi/3.9

转到此链接下载 jar 文件 将其添加到您的 lib 文件夹中。

【讨论】:

    【解决方案2】:

    试试下面的代码

    String filepath = "/tmp/MyFirstExcel.xlsx";
    
        FileInputStream fis;
    
        fis = new FileInputStream(filepath);
    
        XSSFWorkbook workbook = new XSSFWorkbook(fis);
        XSSFSheet sheet = workbook.getSheetAt(0);
    
        Iterator<org.apache.poi.ss.usermodel.Row> rowIterator = sheet.iterator();
    
        while (rowIterator.hasNext()) {
    
            Row row = rowIterator.next();
    
            row.getCell(0).getStringCellValue();
    
        }
    

    【讨论】:

      【解决方案3】:

      试试这个:

      在上面的代码中,使用 XSSFRow 代替 Row:

      XSSFRow = sheet.createRow(rowNum++);
      

      【讨论】:

      • 请给出更详细的答复。你应该适当地编写代码。
      猜你喜欢
      • 1970-01-01
      • 2012-10-10
      • 2017-12-30
      • 1970-01-01
      • 1970-01-01
      • 2023-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多