嗨,这是我使用XSSFWorkbook创建电子表格的代码:
import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
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 Writesheet
{
public static void main(String[] args) throws Exception
{
//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" });
empinfo.put( "2", new Object[] {
"tp01", "Gopal", "Technical Manager" });
empinfo.put( "3", new Object[] {
"tp02", "Manisha", "Proof Reader" });
empinfo.put( "4", new Object[] {
"tp03", "Masthan", "Technical Writer" });
empinfo.put( "5", new Object[] {
"tp04", "Satish", "Technical Writer" });
empinfo.put( "6", new Object[] {
"tp05", "Krishna", "Technical Writer" });
//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("Writesheet.xlsx"));
workbook.write(out);
out.close();
System.out.println(
"Writesheet.xlsx written successfully" );
}
}
但是当我尝试运行它时,我得到以下ClassNotFound异常:
线程“main”中的异常java.lang.NoClassDefFoundError: org / apache / xmlbeans / XmlException at plotgz.XSSFParser.main(XSSFParser.java:17)引起: java.lang.ClassNotFoundException:org.apache.xmlbeans.XmlException at java.net.URLClassLoader $ 1.run(URLClassLoader.java:366)at java.net.URLClassLoader $ 1.run(URLClassLoader.java:355)at java.security.AccessController.doPrivileged(Native Method)at java.net.URLClassLoader.findClass(URLClassLoader.java:354)at java.lang.ClassLoader.loadClass(ClassLoader.java:425)at sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:308)at at java.lang.ClassLoader.loadClass(ClassLoader.java:358)... 1更多
我在类路径中添加了以下jar:
POI-OOXML-3.5 beta5.jar
我无法弄清楚导致此异常的原因。任何帮助,将不胜感激。感谢。