加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Excel转Html

发布时间:2020-12-14 06:31:51 所属栏目:Java 来源:网络整理
导读:项目结构: 这是一个maven项目,主函数在Client类里面 当运行程序的后,控制台情况: 当我们刷新了test.html文件后,用浏览器打开效果: 说一下这个过程的设计思路: 1.读取excel文件 2.利用velocity模板工具把读取的内容渲染到html里面 整个过程就两个步骤

项目结构:

这是一个maven项目,主函数在Client类里面

当运行程序的后,控制台情况:

当我们刷新了test.html文件后,用浏览器打开效果:

说一下这个过程的设计思路:

1.读取excel文件

2.利用velocity模板工具把读取的内容渲染到html里面

整个过程就两个步骤,是不是非常简单。

当我们在把这两个过程再细化一下,思路就更加清晰明了了。

1.1.怎样读取或者写入Excel文件呢?

2.1.怎样使用velocity模板工具呢?

有了上面1.1和2.1的基础,现在我们要做的工作,就是把他们串起来,就实现了Excel转Html

为了自己以后一看源码就知道怎样做,我习惯贴源码出来。 当然还会有源码下载的(在文章末尾)。

===============================================

源码部分:

===============================================

/excel2html/src/main/java/com/b510/excel/client/Client.java

main(String[] args) String excel2010 = List list1 = (list1 != && list1.size() > 0 System.out.println("No. : " + student.getNo() + ",name : " + student.getName() + ",age : " + student.getAge() + ",score : " + System.out.println("begin to write into html file" }

/excel2html/src/main/java/com/b510/excel/common/Common.java

String OFFICE_EXCEL_2010_POSTFIX = "xlsx" String EMPTY = "" String POINT = "." String STUDENT_INFO_XLSX_PATH = "/student_info" + POINT + String NOT_EXCEL_FILE = " : Not the Excel file!" String PROCESSING = "Processing..." String HTML_FILE = "test.html" String TEST_HTML_FILE = "./test.html" }

/excel2html/src/main/java/com/b510/excel/reader/ReadExcel.java

List readExcel(String path) (path == || } String postfix = (! } System.out.println(path + @SuppressWarnings("resource" List readXlsx(String path) System.out.println(Common.PROCESSING + InputStream is = XSSFWorkbook xssfWorkbook = Student student = List list = ArrayList ( numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++ XSSFSheet xssfSheet = (xssfSheet == ( rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++ XSSFRow xssfRow = (xssfRow != student = XSSFCell no = xssfRow.getCell(0 XSSFCell name = xssfRow.getCell(1 XSSFCell age = xssfRow.getCell(2 XSSFCell score = xssfRow.getCell(3 @SuppressWarnings("resource" List readXls(String path) System.out.println(Common.PROCESSING + InputStream is = HSSFWorkbook hssfWorkbook = Student student = List list = ArrayList ( numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++ HSSFSheet hssfSheet = (hssfSheet == ( rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++ HSSFRow hssfRow = (hssfRow != student = HSSFCell no = hssfRow.getCell(0 HSSFCell name = hssfRow.getCell(1 HSSFCell age = hssfRow.getCell(2 HSSFCell score = hssfRow.getCell(3 @SuppressWarnings("static-access" (xssfRow.getCellType() == } (xssfRow.getCellType() == } @SuppressWarnings("static-access" (hssfCell.getCellType() == } (hssfCell.getCellType() == } }

/excel2html/src/main/java/com/b510/excel/util/Util.java

(path == || path.substring(path.lastIndexOf(Common.POINT) + 1 }

/excel2html/src/main/java/com/b510/excel/vm/student.vm

HTML Tutorial S/N ID Name Age Score $count $student.no $student.name $student.age $student.score

/excel2html/src/main/java/com/b510/excel/vo/Student.java

.id = .no = .name = .age = setScore( .score = }

/excel2html/src/main/java/com/b510/excel/writer/WriteHtml.java

String createCode(String fileVMPath,List students) VelocityEngine velocityEngine = velocityEngine.setProperty("input.encoding","UTF-8" velocityEngine.setProperty("output.encoding","UTF-8" Template template = VelocityContext velocityContext = velocityContext.put("students" StringWriter stringWriter = write(List students) System.out.println("write begin" File file = FileWriter fw = fw.write(createCode("./src/main/java/com/b510/excel/vm/student.vm" System.out.println("write end. Refresh the project before seeing the excel2html/" + }

/excel2html/pom.xml

4.0.0 hongten.exl2html excel2html 0.0.1 excel2html http://maven.apache.org UTF-8 junit junit 3.8.1 test org.apache.poi poi 3.12 org.apache.poi poi-ooxml 3.12 org.apache.poi poi-ooxml-schemas 3.12 velocity velocity 1.5 dom4j dom4j 1.5 org.apache.xmlbeans xmlbeans 2.6.0

源码下载:

========================================================

More reading,and english is important.

I'm Hongten

大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。Hongten博客排名在100名以内。粉丝过千。Hongten出品,必是精品。

E | hongtenzone@foxmail.com ?B |?

========================================================

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读