java - .xls 使用 java 和 POI APACHE 转换为 xlsx

标签 java apache-poi xls xlsx

我正在尝试使用 POI.APACHE 在 java 中编辑 excel 文件。我必须将 .xls 转换为 .xlsx,因为我需要将文件发送到共享点。这就是为什么它不能用不同的扩展名重命名的原因。我该怎么做?我在他们的网站上找不到任何示例。谢谢

最佳答案

import java.io.BufferedInputStream;
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;

/**
 * Partial copy of one of style and data..
 * 
 * @author jcalfee
 */
public class xls2xlsx {

    /**
     * @param args
     * @throws InvalidFormatException
     * @throws IOException
     */
    public static void main(String[] args) throws InvalidFormatException,
            IOException {

        String inpFn = args[0];
        String outFn = args[1];

        InputStream in = new BufferedInputStream(new FileInputStream(inpFn));
        try {
            Workbook wbIn = new HSSFWorkbook(in);
            File outF = new File(outFn);
            if (outF.exists())
                outF.delete();

            Workbook wbOut = new XSSFWorkbook();
            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());

                        // HSSFCellStyle cannot be cast to XSSFCellStyle
                        // cellOut.setCellStyle(cellIn.getCellStyle());
                    }
                }
            }
            OutputStream out = new BufferedOutputStream(new FileOutputStream(
                    outF));
            try {
                wbOut.write(out);
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }
    }
}

关于java - .xls 使用 java 和 POI APACHE 转换为 xlsx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14533544/

相关文章:

java - 删除重复行后无法使用 Apache POI 编写新的 Excel

java - 两个 ArrayList 之间的同步字符串

java - 异步任务停止工作,在使用断点调试时工作 - Android

Java小数精度问题

java 。 thymeleaf 。 CRUD 操作后,页面上的所有 .CSS 样式都会消失

c# - VS 代码 C# - System.NotSupportedException : No data is available for encoding 1252

java - 写入后 Excel 文件损坏

java - Groovy/POI 在不同系统上返回不同迭代器

powershell - 根据一列的值分割一个csv文件

csv - 使用 VBScript 将 xls 转换为 csv 并用分号分隔