一.Excel数据的读取
1.java代码
package com.immo.framework.utils;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.easy.excel.ExcelContext;
import org.easy.excel.result.ExcelImportResult;
import com.immo.framework.pojo.ygyy.TMonitorDrugList;
public class ExcelUtils {
public static void main(String[] args) {
String path = "D:/监控药物数据.xls";
List<TMonitorDrugList> list = new ExcelUtils().getExcelList(path);
for(TMonitorDrugList tMonitorDrugList : list){
System.out.println("tMonitorDrugList----"+tMonitorDrugList);
}
}
//Excel的读取
public List<TMonitorDrugList> getExcelList(String path){
List<TMonitorDrugList> notEmptyList = null;
try {
InputStream excelIn = new FileInputStream(path);
ExcelContext excelContext = new ExcelContext("excel-config.xml");
ExcelImportResult result = excelContext.readExcel("tmonitorDrug", 0, excelIn);
List<TMonitorDrugList> list= result.getListBean();
notEmptyList = new ArrayList<TMonitorDrugList>();
for(TMonitorDrugList tMonitorDrugList : list){
if(tMonitorDrugList.isEmpty()){
notEmptyList.add(tMonitorDrugList);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return notEmptyList;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<excels>
<!-- excel标签参看:ExcelDefinition,Field标签参看:FieldValue -->
<!-- 测试使用 ,学生类-->
<excel id="student" class="org.easy.excel.test.model.StudentModel"
sheetname="测试学生列表" enableStyle="true" defaultAlign="center" sheetIndex="0">
<field name="id" align="center" titleBgColor="dark_blue" uniformStyle="true"
columnWidth="3000" titleFountColor="white" title="ID"/>
<field name="name" title="学生姓名" uniformStyle="true"/>
<field name="age" title="年龄" align="center" titleFountColor="red" titleBgColor="dark_blue" isNull="false" regex="^[1-9]\d*$" regexErrMsg="必须是数字"/>
<field name="studentNo" title="学号" titleFountColor="blue" isNull="false" />
<field name="createTime" columnWidth="4000" title="创建时间" pattern="yyyy-MM-dd"/>
<field name="status" title="状态" titleBgColor="green" format="1:正常,0:禁用,-1:无效" />
<!-- 创建人,可能需要查询数据库校验,这里使用自定义解析器 -->
<field name="createUser" title="创建人"
cellValueConverter="org.easy.excel.test.converter.CreateUserCellValueConverter" otherConfig="Test动态传递的一个变量"/>
<!-- 复杂对象 -->
<field name="book.bookName" title="图书名称" columnWidth="6000"/>
<field name="book.price" title="图书价格" columnWidth="6000" decimalFormatPattern="###,##0.00" roundingMode="up" defaultValue="0.00"/>
<field name="book.author.authorName" title="作者名称" columnWidth="6000"/>
</excel>
<excel id="tmonitorDrug" class="com.immo.framework.pojo.ygyy.TMonitorDrugList">
<field name="startDate" title="启用时间"/>
<field name="pzCode" title="品种代码"/>
<field name="commonName" title="通用名"/>
<field name="ypfl" title="药品分类"/>
<field name="typeYl" title="药理/功效大类" />
<field name="subTypeYl" title="药理/功效子类"/>
<field name="typeAdminRoute" title="给药途径分类"/>
</excel>
</excels>
1.javaBean
public class User {
private int userId;
private String name;
private String sex;
private String address;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
/**
* 导出Excel
*/
public void exportUserInfoToExcel() throws IOException{
OutputStream out = response.getOutputStream();
response.setContentType("application/x-download");
//设置导出文件名称
response.setHeader("Content-Disposition", "attachment;filename="+DateUtil.getNow("yyyyMMddHHmmssSSS")+".xls");
Excel excel = new Excel();
//查询内容list,userService.getUserList方法自己实现
List<User> list = userService.getUserList();
//表头数组
String[] header = {"用户ID","用户名","性别","地址"};
//表单title
String title = "用户信息列表";
int headerLen = header.length;
//单元格边框颜色
Color borderColor = Color.GREY_50_PERCENT;
//表头字体
IFontEditor headFont = new IFontEditor(){
public void updateFont(Font font){
font.fontHeightInPoints(14)
.boldweight(BoldWeight.BOLD)
.color(Color.WHITE);
}
};
//标题字体
IFontEditor titleFont = new IFontEditor(){
public void updateFont(Font font){
font.fontHeightInPoints(30)
.boldweight(BoldWeight.BOLD)
.color(Color.DARK_BLUE);
}
};
//设置表单信息
excel.setWorkingSheet(0)//设置第1个工作表为工作状态
.sheetName(title+"1")
.fitToPage(true)
.horizontallyCenter(true)
.printGridlines(false)
.displayGridlines(true)
.autobreaks(true)
.printSetup(new IPrintSetup(){
public void setup(HSSFPrintSetup printSetup) {
printSetup.setLandscape(true);//是否打印背景
printSetup.setFitHeight((short)1);//调整缩放
printSetup.setFitWidth((short)1);//调整缩放
}
});
//设置标题内容,标题行列合并,标题样式
excel.row(0).height(60);
excel.cell(0, 0).value(title)
.align(Align.CENTER)
.font(titleFont);
excel.region(0, 0, 0, headerLen-1).merge();//合并标题的单元格
//设置表头及样式
for(int i=0;i<headerLen;i++){
excel.column(i).width(5000);
excel.cell(1, i).value(header[i])
.align(Align.CENTER)
.bgColor(Color.DARK_BLUE)
.font(headFont);
}
//添加内容
for(int i=0,len=list.size();i<len;i++){
excel.row(i+2).height(30);//设置行高度
for(int j=0;j<headerLen;j++){
excel.cell(i+2, j).align(Align.CENTER)//设置区域内单元格水平对齐方式
.vAlign(VAlign.CENTER)//设置垂直对齐方式
.border(BorderStyle.THIN, borderColor);
}
User grid = list.get(i);
excel.cell(i+2, 0).value(grid.getUserId());
excel.cell(i+2, 1).value(grid.getName());
excel.cell(i+2, 2).value(grid.getSex());
excel.cell(i+2, 3).value(grid.getAddress());
}
excel.saveExcel(out);
}
<a href="#" id="exportXlsButton" target="exportTarget" style="display:none"></a>
<iframe name="exportTarget" id="exportTarget" style="display:none;"></iframe>
js添加内容:
jConfirm('确定要导出excel文件?', '消息', function(rs) {
if (rs == true) {
var exportXlsButton = document.getElementById("exportXlsButton");
exportXlsButton.href = url; //url地址
exportXlsButton.click();
}
});
//url地址指向上面的方法即可。