将数据写入CSV文件。

huangapple go评论79阅读模式
英文:

Writing data into a csv file

问题

I want to write data into a csv file, but that won't work. I already inserted the apache.poi dependency. Every time when I try to run this, I only get the error message "Caused by: java.lang.RuntimeException: Uncompilable source code - package org.apache.poi.ss.usermodel does not exist at main.WritingCSV.(WritingCSV.java:4)"

My code:

package main;
import java.io.*;
import java.util.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class WritingCSV {
    
    public void writing()
    {
        //Create blank workbook
      XSSFWorkbook workbook = new XSSFWorkbook();
      
      //Create a blank sheet
      XSSFSheet spreadsheet = workbook.createSheet( " Employee Info ");

      //Create row object
      XSSFRow row;

      //This data needs to be written (Object[])
      Map < String, Object[] > empinfo = new TreeMap < String, Object[] >();
      empinfo.put( "1", new Object[] {
         "EMP ID", "EMP NAME", "DESIGNATION" });
      

      //Iterate over data and write to sheet
      Set < String > keyid = empinfo.keySet();
      int rowid = 0;
      
      for (String key : keyid) {
         row = spreadsheet.createRow(rowid++);
         Object [] objectArr = empinfo.get(key);
         int cellid = 0;
         
         for (Object obj : objectArr){
            Cell cell = row.createCell(cellid++);
            cell.setCellValue((String)obj);
         }
      }
      //Write the workbook in file system
      FileOutputStream out = new FileOutputStream(
         new File("C:/poiexcel/Writesheet.xlsx"));
      
      workbook.write(out);
      out.close();
      System.out.println("Writesheet.xlsx written successfully");
    }
}

My pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>Migration</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
    </dependency>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>12</maven.compiler.source>
        <maven.compiler.target>12</maven.compiler.target>
    </properties>
</project>
英文:

I want to write data into a csv file, but that won't work. I already inserted the apache.poi dependency. Everytime when I try to run this, I only get the error message "Caused by: java.lang.RuntimeException: Uncompilable source code - package org.apache.poi.ss.usermodel does not exist at main.WritingCSV.<clinit>(WritingCSV.java:4)"

My code:

package main;
import java.io.*;
import java.util.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class WritingCSV {
    
    public void writing()
    {
        //Create blank workbook
      XSSFWorkbook workbook = new XSSFWorkbook();
      
      //Create a blank sheet
      XSSFSheet spreadsheet = workbook.createSheet( &quot; Employee Info &quot;);

      //Create row object
      XSSFRow row;

      //This data needs to be written (Object[])
      Map &lt; String, Object[] &gt; empinfo = new TreeMap &lt; String, Object[] &gt;();
      empinfo.put( &quot;1&quot;, new Object[] {
         &quot;EMP ID&quot;, &quot;EMP NAME&quot;, &quot;DESIGNATION&quot; });
      

      //Iterate over data and write to sheet
      Set &lt; String &gt; keyid = empinfo.keySet();
      int rowid = 0;
      
      for (String key : keyid) {
         row = spreadsheet.createRow(rowid++);
         Object [] objectArr = empinfo.get(key);
         int cellid = 0;
         
         for (Object obj : objectArr){
            Cell cell = row.createCell(cellid++);
            cell.setCellValue((String)obj);
         }
      }
      //Write the workbook in file system
      FileOutputStream out = new FileOutputStream(
         new File(&quot;C:/poiexcel/Writesheet.xlsx&quot;));
      
      workbook.write(out);
      out.close();
      System.out.println(&quot;Writesheet.xlsx written successfully&quot;);
    }
}

My pom

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;com.mycompany&lt;/groupId&gt;
    &lt;artifactId&gt;Migration&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;
    &lt;dependency&gt;
    &lt;groupId&gt;org.apache.poi&lt;/groupId&gt;
    &lt;artifactId&gt;poi-ooxml&lt;/artifactId&gt;
    &lt;version&gt;4.1.2&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;properties&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
        &lt;maven.compiler.source&gt;12&lt;/maven.compiler.source&gt;
        &lt;maven.compiler.target&gt;12&lt;/maven.compiler.target&gt;
    &lt;/properties&gt;
&lt;/project&gt;

答案1

得分: 1

尝试将dependency标签包裹在dependencies标签中,就像这样:

&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.apache.poi&lt;/groupId&gt;
        &lt;artifactId&gt;poi-ooxml&lt;/artifactId&gt;
        &lt;version&gt;4.1.2&lt;/version&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;
英文:

try to wrap the dependency tag in dependencies tag, like this:

&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.apache.poi&lt;/groupId&gt;
        &lt;artifactId&gt;poi-ooxml&lt;/artifactId&gt;
        &lt;version&gt;4.1.2&lt;/version&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;

huangapple
  • 本文由 发表于 2020年8月7日 15:37:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63297318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定