Java 将Excel转为SVG的方法
程序员文章站
2022-03-03 09:41:53
1. 程序运行环境如下:编译工具:ideajdk版本:1.8.0excel测试文档:.xlsx 2013excel工具jar包:free spire.xls.jar 3.9.12.关于如何导入jar包...
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。
以上就是java 将excel转为svg的方法的详细内容,更多关于java 将excel转为svg的资料请关注其它相关文章!
上一篇: 浅谈IDEA实用的Servlet模板
下一篇: Unity中 mesh生成斜坡的示例代码