小编给大家分享一下Java如何将Excel转为SVG,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
编译工具:IDEA
JDK版本:1.8.0
Excel测试文档:.xlsx 2013
Excel工具jar包:free spire.xls.jar 3.9.1
方法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”下载导入。
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(); } } }
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”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。