1.定義jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk" isELIgnored="false"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>報表</title>
<SCRIPT type=text/javascript src="<%=path%>/script/My97DatePicker/WdatePicker.js"></SCRIPT>
<SCRIPT type=text/javascript src="<%=path%>/script/jquery-1.3.2.min.js"></SCRIPT>
</head>
<script type="text/javascript">
function gotosearch(){
if($("#beginDate").val()=="" || $("#endDate").val()==""){
alert("請選擇開始時間和結束時間,不能爲空!");
return false;
}
document.forms[0].submit();
}
function exportToExcel(){
if($("#beginDate").val()=="" || $("#endDate").val()==""){
alert("請選擇開始時間和結束時間,不能爲空!");
return false;
}
document.forms[0].action="acvsExpensesReport_exportToExcel.action";
document.forms[0].submit();
}
</script>
<body scroll="yes">
<h2 align="center">費用化輔料報表</h2>
<div id="main">
<form id="searchForm" name="searchForm" method="post" action="acvsExpensesReport_searchData.action">
<table cellspacing="1">
<tr>
<td width="10%">流程名稱:
</td>
<td >
<select name="flowname">
<option value ="WGG-SC 生產用費用化輔料管理流程">WGG-SC 生產用費用化輔料管理流程</option>
<option value ="MB1A Y01輔料投成本中心不產生庫存二期優化流程">MB1A Y01輔料投成本中心不產生庫存二期優化流程</option>
<option value="IWMS Y01費用化輔料流程">IWMS Y01費用化輔料流程</option>
<option value="Y01費用化輔料(發貨)">Y01費用化輔料(發貨)</option>
<option value="Y01 費用化輔料(預算)">Y01 費用化輔料(預算)</option>
</select>
</td>
</tr>
<tr>
<td >開始時間:</td>
<td>
<input name="beginDate" id="beginDate" type="text"
οnclick="WdatePicker({skin:'ext',isShowClear:false,readOnly:true})" >
</td>
</tr>
<tr>
<td >結束時間:</td>
<td>
<input name="endDate" id="endDate" type="text"
οnclick="WdatePicker({skin:'ext',isShowClear:false,readOnly:true})" >
</td>
</tr>
<tr>
<!-- <td><input type="button" style="color:blue; text-align:center ;width: 100px;height: 30px" name="search" value="查詢" οnclick="gotosearch()"/>
</td>
-->
<td><input type="button" style="color:blue; text-align:center ;width: 100px;height: 30px" name="search" value="導出" οnclick="exportToExcel()"/>
</td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" border="0" width="100%" align="center">
<tr>
<td class="ta_01" align="center" bgcolor="#f5fafe">
<table cellspacing="0" cellpadding="1" rules="all" bordercolor="gray" border="1" id="DataGrid1">
<tr style="FONT-WEIGHT:bold;HEIGHT:15px;BACKGROUND-COLOR:#afd1f3">
<td>流程實例編號</td>
<td>提報人工號</td>
<td>提報人姓名</td>
<td>SAP返回的內容</td>
<td>工廠</td>
<td align="center">評估類型</td>
<td align="center">發貨庫存地點</td>
<td align="center">物料號</td>
<td align="center">基本計量單位</td>
<td align="center">數量</td>
<td align="center">成本中心</td>
<td align="center">供應商代碼</td>
<td align="center">特殊庫存標示</td>
<td align="center">物料描述</td>
<td align="center">移動類型</td>
<td align="center">憑證擡頭文本</td>
<td align="center">線體</td>
<td align="center">小微代碼</td>
</tr>
<s:if test="#request.formDatas!=null">
<s:iterator value="#request.formDatas" var="formData">
<tr οnmοuseοver="this.style.backgroundColor = 'white'" οnmοuseοut="this.style.backgroundColor = '#F5FAFE';">
<td align="center"><s:property value="#formData.flowid"/></td>
<td align="center"><s:property value="#formData.userno"/></td>
<td align="center">
<s:property value="#formData.username"/>
</td>
<td align="center" >
<s:property value="#formData.message"/> </td>
<td align="center" width="8%">
<s:property value="#formData.werks"/>
</td>
<td align="center" width="10%">
<s:property value="#formData.charg"/>
</td>
<td align="center" width="10%">
<s:property value="#formData.lgort"/>
</td>
<td align="center" width="9%" style="HEIGHT: 22px">
<s:property value="#formData.matnr"/>
</td>
<td align="center" width="9%" style="HEIGHT: 22px">
<s:property value="#formData.erfme"/>
</td>
<td align="center" width="9%" style="HEIGHT: 22px">
<s:property value="#formData.erfmg"/>
</td>
<td align="center" width="9%" style="HEIGHT: 22px">
<s:property value="#formData.kostl"/>
</td>
<td align="center" width="9%" style="HEIGHT: 22px">
<s:property value="#formData.lifnr"/>
</td>
<td align="center" width="9%" style="HEIGHT: 22px">
<s:property value="#formData.sobkz"/>
</td>
<td align="center" width="9%" style="HEIGHT: 22px">
<s:property value="#formData.maktx"/>
</td>
<td align="center" width="9%" style="HEIGHT: 22px">
<s:property value="#formData.bwartwa"/>
</td>
<td align="center" width="9%" style="HEIGHT: 22px">
<s:property value="#formData.bktxt"/>
</td>
<td align="center" width="9%" style="HEIGHT: 22px">
<s:property value="#formData.line"/>
</td>
<td align="center" width="9%" style="HEIGHT: 22px">
<s:property value="#formData.sybdm"/>
</td>
</tr>
</s:iterator>
</s:if>
</table>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
2.配置struts2配置文件
<action name="acvsExpensesReport_*" class="expensesReport.action.AcvsExpensensReportSearch" method="{1}">
<result name="home">/Reports/ACVS/acvsExpenseReport.jsp</result>
<result name="SUCCESS">/Reports/ACVS/acvsExpenseReport.jsp</result>
<result name="excel" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<!--需要action當中返回一個輸入流-->
<param name="inputName">excelStream</param>
<!--爲excel設置標題 需要通過ognl從action當中獲取 當然在這寫死也是沒關係的-->
<param name="contentDisposition">filename="${excelFileName}"</param>
<param name="bufferSize">1024</param>
</result>
</action>
3.編寫action。。。
/**
* 點擊導出後 導出到excel當中
* @return
*/
public String exportToExcel(){
//從頁面中獲取參數
flowname=request.getParameter("flowname");
beginDate=request.getParameter("beginDate");
endDate=request.getParameter("endDate");
String tableName=iac.findTableNameByFlowName(flowname);
//構造excel當中的數據和標頭
ArrayList fieldName=iac.getExcelTitleByflowName(flowname);
ArrayList fieldData=iac.searchExpenseValueByDateAndFlowName(tableName,flowname,beginDate,endDate);
try {
//標題名
String excelName=flowname+"("+beginDate+"到"+endDate+")數據";
excelFileName=new String(excelName.getBytes("gb2312"), "iso8859-1")+".xls";
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//構造輸入流
excelStream=iac.getExcelInputStreamExpensesReport(fieldName,fieldData);
return "excel";
}
4.iac調用的service層的方法代碼
/**
* 獲取文件輸入流來將數據寫入到excel當中
*/
public InputStream getExcelInputStreamExpensesReport(ArrayList fieldName,
ArrayList fieldData) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
HSSFWorkbook workBook = new HSSFWorkbook();
int rows = fieldData.size();
int sheetNum=0;
/*
* 2016/9/9修改 當數據集合沒有數據的時候也會將表頭導出 下載模板功能也用到此過程
* begin
*/
if (rows!=0 && rows % SPLIT_COUNT == 0) {
sheetNum = rows / SPLIT_COUNT;
} else if(rows!=0 && rows % SPLIT_COUNT != 0){
sheetNum = rows / SPLIT_COUNT + 1;
}else{
sheetNum=1;
}
//end
for (int i = 1; i <= sheetNum; i++) {
HSSFSheet sheet = workBook.createSheet("Page " + i);
HSSFRow headRow = sheet.createRow((short) 0);
for (int j = 0; j < fieldName.size(); j++) {
HSSFCell cell = headRow.createCell((short) j);
//添加樣式
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
//設置所有單元格的寬度
sheet.setColumnWidth((short)j, (short)6000);
//創建樣式
HSSFCellStyle cellStyle=workBook.createCellStyle();
//創建字體樣式
HSSFFont font=workBook.createFont();
//將字體加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//設置字體顏色
font.setColor(HSSFColor.RED.index);
//將新設置的字體屬性放置到樣式當中
cellStyle.setFont(font);
if(fieldName.get(j) != null){
cell.setCellStyle(cellStyle);
cell.setCellValue((String) fieldName.get(j));
}else{
cell.setCellStyle(cellStyle);
cell.setCellValue("-");
}
}
for (int k = 0; k < (rows < SPLIT_COUNT ? rows : SPLIT_COUNT); k++) {
HSSFRow row = sheet.createRow((short) (k + 1));
//將數據內容放入excel單元格
ArrayList rowList = (ArrayList) fieldData.get((i - 1)
* SPLIT_COUNT + k);
for (int n = 0; n < rowList.size(); n++) {
HSSFCell cell = row.createCell((short) n);
if(rowList.get(n) != null){
cell.setCellValue((String) rowList.get(n).toString());
}else{
cell.setCellValue("");
}
}
}
}
try {
workBook.write(out);
} catch (Exception e) {
e.printStackTrace();
}
return new ByteArrayInputStream(out.toByteArray());
}
完成excel的導出 相關 poi jar包自己導入 這只是個範本。。一般的excel導出都是通過這種方式實現的。。。