本文介绍了Java的API POI:从*的.xlsx到的* .xls转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题。
想转换新的Excel文件(的.xlsx)到旧的(的的.xls)与Java中的POI API。

I have a small problem.Wanna convert the new excel files (.xlsx) into the old one (.xls) with the POI API on Java.

我认为这是一个心态问题,但我不知道这存在故障...

I think it is a mind problem, but I don't know which fault exist...

我在这里使用了这些code:

I used these code here:

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class xlsx2xls {
    private String outFn;
    private File inpFn;

    public xlsx2xls(File inpFn){
        this.outFn = inpFn + ".xls";
        this.inpFn = inpFn;
    }

    public void xlsx2xls_progress() throws InvalidFormatException,IOException {
        InputStream in = new FileInputStream(inpFn);
        try {
            XSSFWorkbook wbIn = new XSSFWorkbook(in);
            File outF = new File(outFn);
            if (outF.exists()) {
                outF.delete();
            }

            Workbook wbOut = new HSSFWorkbook();
            int sheetCnt = wbIn.getNumberOfSheets();
            for (int i = 0; i < sheetCnt; i++) {
                Sheet sIn = wbIn.getSheetAt(0);
                Sheet sOut = wbOut.createSheet(sIn.getSheetName());
                Iterator<Row> rowIt = sIn.rowIterator();
                while (rowIt.hasNext()) {
                    Row rowIn = rowIt.next();
                    Row rowOut = sOut.createRow(rowIn.getRowNum());

                    Iterator<Cell> cellIt = rowIn.cellIterator();
                    while (cellIt.hasNext()) {
                        Cell cellIn = cellIt.next();
                        Cell cellOut = rowOut.createCell(cellIn.getColumnIndex(), cellIn.getCellType());

                        switch (cellIn.getCellType()) {
                        case Cell.CELL_TYPE_BLANK: break;

                        case Cell.CELL_TYPE_BOOLEAN:
                            cellOut.setCellValue(cellIn.getBooleanCellValue());
                            break;

                        case Cell.CELL_TYPE_ERROR:
                            cellOut.setCellValue(cellIn.getErrorCellValue());
                            break;

                        case Cell.CELL_TYPE_FORMULA:
                            cellOut.setCellFormula(cellIn.getCellFormula());
                            break;

                        case Cell.CELL_TYPE_NUMERIC:
                            cellOut.setCellValue(cellIn.getNumericCellValue());
                            break;

                        case Cell.CELL_TYPE_STRING:
                            cellOut.setCellValue(cellIn.getStringCellValue());
                            break;
                        }

                        {
                            CellStyle styleIn = cellIn.getCellStyle();
                            CellStyle styleOut = cellOut.getCellStyle();
                            styleOut.setDataFormat(styleIn.getDataFormat());
                        }cellOut.setCellComment(cellIn.getCellComment());

                        }
                }
            }
            OutputStream out = new BufferedOutputStream(new FileOutputStream(outF));
            try {
                wbOut.write(out);
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }
    }
}

和Java的告诉我,在这里:

And Java tells me that here:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException
    at xlsx2xls.xlsx2xls_progress(xlsx2xls.java:35)
    at Workflow.main(Workflow.java:32)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 2 more

我测试这些类POI 3.9。和3.10,在两个相同的错误的呼叫。
Java的:JDK 7
操作系统:赢8.1 x64的

I test these class with POI 3.9. and 3.10, on both the same error calls.Java: JDK 7OS: Win 8.1 x64

我希望得到关于我的问题的足够信息。
感谢您的帮助。

I hope get enough information about my problem.Thanks for your helps.

问候

推荐答案

请注意,作为新XSSF Excel 2007中OOXML(.xlsx)格式文件基于XML的支持。

Please be aware that as the new XSSF supported Excel 2007 OOXML (.xlsx) files are XML based.

您需要添加额外的2罐,使上(.xlsx)格式的Excel文件POI的工作。

You need to add extra 2 jars to make POI work on (.xlsx) Excel file.

xmlbeans2.3.0.jar 的dom4j-1.6.jar 添加到您的类路径中。这2个罐是在POI库处理的.xlsx Excel文件的依赖罐子。

Please add xmlbeans2.3.0.jar and dom4j-1.6.jar to your classpath. These 2 jars are the dependency jars for handling .xlsx Excel file in POI Library.

如果你有下载POI源$ C ​​$ C,你可以找到以下文件夹下这2个罐:

If you have download POI source code, You can find these 2 jars under the following folder:

\\ POI彬3.9-20121203 \\ POI-3.9 \\ OOXML-LIB \\

\poi-bin-3.9-20121203\poi-3.9\ooxml-lib\

如果没有,你可以从以下站点下载它们:

If not, you can download them from the following site:

这篇关于Java的API POI:从*的.xlsx到的* .xls转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 12:51