Java Aspose.cells Excel(.xls,.xlsx)文件转成csv文件和html文件

利用Aspose组件处理office文件,转换的性能比较好,转换的比较快,格式兼容性也挺好。另外服务器或PC上不用安装ms office软件就可以转换。下面分享一下Java中调用Aspose组件将Excel文件转成csv文件和html文件。

1、Aspose组件下载

Aspose下载地址:https://products.aspose.com/cells/java

破解版下载地址:https://download.csdn.net/download/phl657880020/10489382

官方文档地址:https://docs.aspose.com/display/cellsjava/Home

官方Demo代码:https://github.com/aspose-cells/Aspose.Cells-for-Java

2、Excel转csv和html代码

package com.demo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import com.aspose.cells.License;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;

/**
 * 
 * 由于ASPOSE比较吃内存,操作大一点的文件就会堆溢出,所以请先设置好java虚拟机参数:-Xms512m -Xmx512m(参考值)<br>
 * 
 *
 */
public class Test {

    /**
     * 获取license
     * 
     * @return
     */
    public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = Test.class.getClassLoader().getResourceAsStream("\\license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 支持DOC, DOCX, OOXML, RTF, HTML, OpenDocument, PDF, EPUB, XPS, SWF等相互转换<br>
     * 
     * @param args
     */
    public static void main(String[] args) {
        // 验证License
        if (!getLicense()) {
            return;
        }
        try {
            long old = System.currentTimeMillis();
            ExcelConvertToCSV("/home/11.xlsx", "/home/csv.csv");
            ExcelConvertToHtml("/home/11.xlsx", "/home/excel/11.html");
            long now = System.currentTimeMillis();
            System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void ExcelConvertToCSV(String sourceFilePath, String csvFilePath)
			throws Exception {
		com.aspose.cells.Workbook excel = null;
		excel = new com.aspose.cells.Workbook(sourceFilePath);
		excel.save(csvFilePath, com.aspose.cells.SaveFormat.CSV);
	}
       public static void ExcelConvertToHtml(String sourceFilePath, String htmlFilePath)
			throws Exception {
		com.aspose.cells.LoadOptions loadOption = null;
		com.aspose.cells.Workbook excel = null;
		if (sourceFilePath != null
				&& !sourceFilePath.isEmpty()
				&& sourceFilePath
						.substring(sourceFilePath.lastIndexOf("."))
						.toLowerCase() == ".csv") {
			loadOption = new com.aspose.cells.TxtLoadOptions(
					com.aspose.cells.LoadFormat.AUTO);
		}
		if (loadOption != null) {
			excel = new com.aspose.cells.Workbook(sourceFilePath, loadOption);
		} else {
			excel = new com.aspose.cells.Workbook(sourceFilePath);
		}
		excel.save(htmlFilePath, com.aspose.cells.SaveFormat.HTML);
	}
}

3、本文项目代码下载

下载地址:https://www.cjavapy.com/download/5c1620d6dc72d915fc31068a/

推荐阅读
cjavapy编程之路首页