博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java-----Excel转HTML
阅读量:6229 次
发布时间:2019-06-21

本文共 3960 字,大约阅读时间需要 13 分钟。

尽管是转别人的(忘了哪转过来的了),但此处标为原创不是为了提高訪问量,也不是为了其它。仅仅是纯粹的认为有实际用途。希望能给很多其它有此需求的人看到并能帮到他们就足够了

所需jar包:jxl.jar

package cn.com.utils;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.util.HashMap;import java.util.Map;import jxl.Cell;import jxl.Range;import jxl.Sheet;import jxl.Workbook;import jxl.format.Alignment;import jxl.format.CellFormat;import jxl.format.Colour;import jxl.format.VerticalAlignment;public class ExcelTransformHtml {	/**	 * 获取Excel文件转换为HTML后的字符串,仅仅支持后缀为xls的Excel(主调方法)	 * @param sourcefile  Excel文件	 * @return 返回Excel转换后的HTML字符串	 * @throws Exception	 */	public static String getExcelInfo(File sourcefile) throws Exception {		StringBuffer sb = new StringBuffer();		InputStream is = new FileInputStream(sourcefile);		Workbook rwb = Workbook.getWorkbook(is);		Sheet sheet = rwb.getSheet(0);		int colnum = sheet.getColumns();		int rownum = sheet.getRows();		Map
map[] = getRowSpanColSpanMap(sheet); sb.append("
"); for (int row = 0; row < rownum; row++) { sb.append("
"); for (int col = 0; col < colnum; col++) { Cell cell = sheet.getCell(col, row); String content = cell.getContents(); CellFormat cellFormat = cell.getCellFormat(); if (map[0].containsKey(row + "," + col)) { String pointString = map[0].get(row + "," + col); map[0].remove(row + "," + col); int bottomeRow = Integer.valueOf(pointString.split(",")[0]); int bottomeCol = Integer.valueOf(pointString.split(",")[1]); int rowSpan = bottomeRow - row + 1; int colSpan = bottomeCol - col + 1; sb.append("
"); } sb.append("
"); } sb.append("
"); if (content == null || "".equals(content.trim())) { sb.append("   "); } else { sb.append(content); } sb.append("
"); rwb.close(); is.close(); return sb.toString(); } @SuppressWarnings("unchecked") private static Map
[] getRowSpanColSpanMap(Sheet sheet) { Map
map0 = new HashMap
(); Map
map1 = new HashMap
(); Range[] range = sheet.getMergedCells(); for (int i = 0; i < range.length; i++) { Cell topCell = range[i].getTopLeft(); Cell bottomCell = range[i].getBottomRight(); int topRow = topCell.getRow(); int topCol = topCell.getColumn(); int bottomRow = bottomCell.getRow(); int bottomCol = bottomCell.getColumn(); map0.put(topRow + "," + topCol, bottomRow + "," + bottomCol); int tempRow = topRow; while (tempRow <= bottomRow) { int tempCol = topCol; while (tempCol <= bottomCol) { map1.put(tempRow + "," + tempCol, ""); tempCol++; } tempRow++; } map1.remove(topRow + "," + topCol); } Map[] map = { map0, map1 }; return map; } private static String convertToHtmlGrammer(Alignment alignment) { String align = "left"; switch (alignment.getValue()) { case 1: align = "left"; break; case 2: align = "center"; break; case 3: align = "right"; break; case 5: align = "justify"; break; default: break; } return align; } private static String convertToHtmlGrammer(VerticalAlignment verticalAlignment) { String valign = "middle"; switch (verticalAlignment.getValue()) { case 1: valign = "middle"; break; case 2: valign = "bottom"; break; case 3: valign = "top"; break; default: break; } return valign; } /*private static String convertToHtmlGrammer(Colour colour) { StringBuffer sb = new StringBuffer(""); if (colour != null && !"default background".equalsIgnoreCase(colour.getDescription())) { sb.append("#"); sb.append(fillWithZero(Integer.toHexString(colour.getDefaultRGB().getRed()))); sb.append(fillWithZero(Integer.toHexString(colour.getDefaultRGB().getGreen()))); sb.append(fillWithZero(Integer.toHexString(colour.getDefaultRGB().getBlue()))); } return sb.toString(); }*/ private static String fillWithZero(String str) { if (str != null && str.length() < 2) { return "0" + str; } return str; }}


本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5204461.html,如需转载请自行联系原作者

你可能感兴趣的文章
固本清源
查看>>
Execution Plan 执行计划介绍
查看>>
聊聊连接池和线程
查看>>
Python——正則表達式(2)
查看>>
适合新人学习的iOS官方Demo
查看>>
拉开大变革序幕(下):分布式计算框架与大数据
查看>>
AndroidStudio 使用AIDL
查看>>
H.264 RTPpayload 格式------ H.264 视频 RTP 负载格式(包含AAC部分解析)
查看>>
poj 3468 A Simple Problem with Integers 【线段树-成段更新】
查看>>
CentOS---网络配置详解
查看>>
第1阶段——uboot分析之硬件初始化start.S(4)
查看>>
记dynamic的一个小坑 -- RuntimeBinderException:“object”未包括“xxx”的定义
查看>>
代写初中语文作文|代写初中语文作文技巧分享
查看>>
linux字符设备文件的打开操作
查看>>
Servlet介绍以及简单实例
查看>>
[js高手之路] 跟GhostWu一起封装一个字符串工具库-架构篇(1)
查看>>
Java.ftp上传下载
查看>>
【Node.js】4.从一个例子切入Node js的规范
查看>>
实施微服务架构的关键技术
查看>>
“流”的思维—Workflowy
查看>>