设备资源管理系统-poi报表
设备资源管理系统-poi报表
- POI报表的导出形式
- 部分代码
POI实现excel文件的导出:
1、导入使用poi的jar包。
2、添加类文件ExcelFileGenerator.java
3、修改userIndex.jsp,在Form2中添加:
* <td class="ta_01" align="right">
<input style="font-size:12px; color:black; height=20;width=80" id="BT_Export" type="button" value="导出EXCEL" name="BT_Export"
onclick="exportUser()">
</td>
* 添加一个js的方法:
function exportUser(){
var userName = document.getElementById("userName").value;
var path = "system/elecUserAction_export.do?userName="+userName;
openWindow(path,'700','400');
}
4、修改userList.jsp,在Form2中添加:
<td class="ta_01" align="right">
<input style="font-size:12px; color:black; height=20;width=80" id="BT_Export" type="button" value="导出EXCEL" name="BT_Export"
onclick="exportUser()">
</td>
5、配置struts.xml文件
<!-- 2011-12-12,添加excel的数据导出 -->
<result name="export">
/WEB-INF/page/system/userIndex.jsp
</result>
6、在ElecUserAction中添加:
try {
ArrayList<String> fieldName = elecUserService.getExportFieldName();
ArrayList fieldData = elecUserService.getExportFieldData(elecUserForm);
//获取输出流
OutputStream out = response.getOutputStream();
//重置输出流
response.reset();
//设置数据导出类型,使用excel的格式
response.setContentType("application/vnd.ms-excel");
//使用ExcelFileGenerator.java进行将数据信息导入到excel文件中
ExcelFileGenerator excelFileGenerator = new ExcelFileGenerator(fieldName,fieldData);
excelFileGenerator.expordExcel(out);
//设置输出形式
System.setOut(new PrintStream(out));
//刷新
out.flush();
//关闭
if(out!=null){
out.close();
}
} catch (IOException e) {
throw new RuntimeException("将数据导出EXCEL有误!");
}
return null;
7、在ElecUserServiceImpl中添加
public ArrayList<String> getExportFieldName() {
String [] arrayNames = {"登录名","用户姓名","性别","联系电话","是否在职"};
ArrayList<String> fieldName = new ArrayList<String>();
for(int i=0;i<arrayNames.length;i++){
String name = arrayNames[i];
fieldName.add(name);
}
return fieldName;
}
public ArrayList getExportFieldData(ElecUserForm elecUserForm) {
String hqlWhere = "";
ArrayList<String> paramsList = new ArrayList<String>();
if(StringUtils.isNotBlank(elecUserForm.getUserName())){
hqlWhere += " and o.userName like ?";
paramsList.add("%"+elecUserForm.getUserName()+"%");
}
Object [] params = paramsList.toArray();
LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
orderby.put(" o.onDutyDate", "desc");
List<ElecUser> list = elecUserDao.findCollectionByConditionNoPage(hqlWhere, params, orderby);
List<ElecUserForm> fromList = this.elecUserPOListToVOList(list);
//组织导出的数据格式使用ArrayList嵌套ArrayList的形式
ArrayList fieldData = new ArrayList();
for(int i=0;fromList!=null && i<fromList.size();i++){
ElecUserForm userform = fromList.get(i);
ArrayList<String> tempList = new ArrayList<String>();
tempList.add(userform.getLogonName());//登录名
tempList.add(userform.getUserName());//用户姓名
tempList.add(userform.getSexID()); //性别
tempList.add(userform.getContactTel());//联系电话
tempList.add(userform.getIsDuty()); //是否在职
fieldData.add(tempList);
}
return fieldData;
}