很早之前写过一个通用的读取模块,使用比较麻烦,当时只写了读取,没有写文件的功能。
原文章:Excel通用读取模块
这次的这个方法,不需要很麻烦的在类中写参数,但是需要配置一个xml文件。
程序根据xml文件来对List<Object>对象进行格式化输出。
程序中只需要调用一个方法即可完成。
方法:
使用该方法的方法:
String xmlPath = "E:/Workspaces/hbxnb/List2Excel/src/TestObj.xml";
String filePath = "E:/";
String fileName = "text";
ExpListObjExcelVS.expListObjFile(list, xmlPath, filePath, fileName);
通过上面的调用方法,应该发现,使用的时候挺简单的,写的稍微有点多的其实是xml文件。
xml文件格式如下:
<?xml version="1.0" encoding="UTF-8"?>
<excel>
<!--
必填项为:name,header,其他全部选填
title:excel表格中第一行合并单元格居中显示的内容
description:第二行显示的说明
author:xml作者,不会输出
sheet:导出excel的sheet名
columns:导出列的信息
column:一个列
name:列对应Object中的字段名,必须有标准的get方法
header:导出excel中显示的标题
width:列宽(单位像素)
-->
<title>人员信息导出</title>
<description>这里是描述信息</description>
<author>isea533</author>
<sheet>人员信息</sheet>
<columns>
<column>
<name>name</name>
<header>姓名</header>
<width>200</width>
</column>
<column>
<name>country</name>
<header>国籍</header>
<width>50</width>
</column>
<column>
<name>birthday</name>
<header>生日</header>
<width>160</width>
</column>
<column>
<name>age</name>
<header>年龄</header>
</column>
<column>
<name>str4</name>
<header>字符串4</header>
</column>
<column>
<name>str2</name>
<header>字符串2</header>
</column>
<column>
<name>str3</name>
<header>字符串3</header>
</column>
<column>
<name>str1</name>
<header>字符串1</header>
</column>
<column>
<name>str5</name>
<header>字符串5</header>
</column>
</columns>
</excel>
测试导出excel结果如下图: