亲宝软件园·资讯

展开

Java 将Excel转为SVG Java 将Excel转为SVG的方法

E-iceblue 人气:0
想了解Java 将Excel转为SVG的方法的相关内容吗,E-iceblue在本文为您仔细讲解Java 将Excel转为SVG的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java,excel转svg,下面大家一起来学习吧。

1. 程序运行环境如下:

编译工具:IDEA

JDK版本:1.8.0

Excel测试文档:.xlsx 2013

Excel工具jar包:free spire.xls.jar 3.9.1

2.关于如何导入jar包

方法1:手动下载jar包。解压,将文件路径:D:\...\Spire.Xls-FE_3.9.1\lib\Spire.Xls.jar中的文件导入Java程序(即本文中使用的方法)。

方法2:Maven仓库下载导入。先在maven程序中配置pom.xml文件,如下内容:

<repositories>

        <repository>

            <id>com.e-iceblue</id>

            <url>http://repo.e-iceblue.cn/repository/maven-public/</url>

        </repository>

    </repositories>

<dependencies>

    <dependency>

        <groupId> e-iceblue </groupId>

        <artifactId>spire.xls.free</artifactId>

        <version>3.9.1</version>

    </dependency>

</dependencies>

完成配置后,点击“Import”下载导入。

3. Java代码

import com.spire.xls.*;

import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;


public class ExcelToSVG {
    public static void main(String[] args) throws Exception{
        //加载Excel测试文档
        Workbook workbook = new Workbook();
        workbook.loadFromFile("sample.xlsx");

        int count = 1;

        //获取分页信息
        List<Map<Integer, PageColRow>> pageInfoList = workbook.getSplitPageInfo();

        //遍历工作表
        for (int i = 0; i < workbook.getWorksheets().getCount(); i++)
        {
            Worksheet sheet = workbook.getWorksheets().get(i);

            FileOutputStream stream;
            Map integerPageColRowMap = pageInfoList.get(i);//获取每页sheet的分页信息
            for (Object object : integerPageColRowMap.values())
            {
                PageColRow colRow = (PageColRow) object;

                stream = new FileOutputStream( (count++) + "-ExcelToSVG.svg");//指定文件名输出格式

                //保存为SVG
                sheet.toSVGStream(stream, colRow.StartRow, colRow.StartCol, colRow.EndRow, colRow.EndCol);
                stream.flush();
                stream.close();
            }
        }

    }
}

转换结果可在IDEA项目文件夹下查看,如下图:

另一种方法

同样使用Free-Spire-XLS 

依赖引入同上

转换工具类

import com.spire.xls.Worksheet;
import org.apache.poi.ss.usermodel.Workbook;
import sun.misc.BASE64Encoder;
 
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
 
 
/**
 * Excel转换处理工具类:转为图片、svg文件等
 *
 * @author yangfeng
 * @date 2019-12-26
 */
public class ExcelTransformUtil {
 
    /**
     * workbook 转为输入流
     *
     * @param workbook
     * @return
     * @throws Exception
     */
    public static InputStream convert2Stream(Workbook workbook) throws Exception {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        workbook.write(bos);
        byte[] barray = bos.toByteArray();
        return new ByteArrayInputStream(barray);
    }
 
    /**
     * excel 转为图片
     *
     * @param inputStream
     */
    public static void excel2Img(InputStream inputStream) {
        com.spire.xls.Workbook book;
        try {
            book = new com.spire.xls.Workbook();
            book.loadFromStream(inputStream);
            Worksheet sheet = book.getWorksheets().get(0);
            sheet.saveToImage("d://toImg.png");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * excel 转为SVG
     *
     * @param inputStream
     */
    public static void excel2SVG(InputStream inputStream, HttpServletResponse response) {
        com.spire.xls.Workbook book;
        try {
            book = new com.spire.xls.Workbook();
            book.loadFromStream(inputStream);
            Worksheet sheet = book.getWorksheets().get(0);
            sheet.toSVGStream(response.getOutputStream(), sheet.getFirstRow(), sheet.getFirstColumn(),
                    sheet.getLastRow(), sheet.getLastColumn());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

生成svg服务

 public void generateSVG(ReportRequest request, HttpServletResponse response) throws Exception {
        ServiceResult<List<OperationReportVO>> result = getOperationReport(request);
        if (result != null && result.isSucceed() && !CollectionUtils.isEmpty(result.getData())) {
            //转为map
            List<Map<String, Object>> list = MapBeanUtil.objectsToMaps(result.getData());
            Workbook workbook = ReportExportUtil.generateExcelReport("运行情况统计表",
                    getDateAndWeek(request.getPlanDate()), list, OperationReportVO.class);
            ExcelTransformUtil.excel2SVG(ExcelTransformUtil.convert2Stream(workbook), response);
        }
    }

    此处的Workbook引自包:org.apache.poi.ss.usermodel.Workbook 。生成excel的workbook。然后将workbook转为输入流,com.spire.xls.Workbook读取输入流,转成svg。

加载全部内容

相关教程
猜你喜欢
用户评论