jsp:
<form id="PMReviewForm" method="post" action="PMReviewInfoExcel.do">
<table cellpadding="5" >
<tr height= '35px'>
<td align="left" class="conditionText">Start Date:</td>
<td><input id="startdate" name="startdate" class="easyui-datebox" required editable="false" style="width:155px;" data-options="formatter:myformatter,parser:myparser"></input></td>
</tr>
<tr height= '35px'>
<td align="left" class="conditionText">End Date:</td>
<td><input id="enddate" name="enddate" class="easyui-datebox" required editable="false" style="width:155px;" data-options="formatter:myformatter,parser:myparser"></input></td>
</tr>
<tr height= '35px'>
<td align="left" class="conditionText">EQP Category:</td>
<td>
<select id="category" class="easyui-combobox" editable="false" name="category" style="width:155px;">
<option value="">...</option>
<option value="P">P</option>
<option value="M">M</option>
</select>
</td>
</tr>
<tr height= '35px'>
<td align="left" class="conditionText" >Review Type:</td>
<td>
<select id="review_type" class="easyui-combobox" editable="false" name="review_type" style="width:155px;">
<option value="Detail">Detail</option>
<option value="Summary">Summary</option>
</select>
</td>
</tr>
<tr height= '35px'>
<td align="left"></td>
<td>
<input type="radio" name="howPlay" value="Excel" >Excel Open
<input type="radio" name="howPlay" value="HTML" checked>HTML Open
</td>
</tr>
<tr height= '35px'>
<td>
<a class="ovalbutton" style="align:center;text-decoration:none;" οnclick="clearForm()"><span>Reset</span></a>
</td>
<td align="left">
<a class="ovalbutton" style="align:left;text-decoration:none;" οnclick="submitForm()"><span>Submit</span></a>
</td>
</tr>
</table>
</form>
提交表单:
$('#PMReviewForm').attr('action','PMReviewInfoExcel.do');
$('#PMReviewForm').submit();
后台Action(这里只是空的Excel)
/*
* Excel查看方式
*/
InputStream excelStream; // 这个输入流对应上面struts.xml中配置的那个excelStream,两者必须一致
String fileName; // 这个名称就是用来传给上面struts.xml中的${fileName}的
public InputStream getExcelStream() {
return excelStream;
}
public void setExcelStream(InputStream excelStream) {
this.excelStream = excelStream;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String PMReviewInfoExcel() throws Exception {
logger.info("=====PMReviewAction to excel starting");
System.out.println("=====PMReviewAction to excel starting");
long sysstart=System.currentTimeMillis();
long sysend;
Map<String, String> param = new HashMap<String, String>();
startdate=startdate.replaceAll("-", "");
enddate=enddate.replaceAll("-", "");
param.put("STARTDATE", startdate);
param.put("ENDDATE", enddate);
param.put("CATEGORY", category);
List<PMReviewInfoBean> list=null;
List<PMReviewSumInfoBean> listSum=null;
Integer count=null;
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
try{
if("Detail".equalsIgnoreCase(review_type)){
count=reportsService.getPMJobDetailCount(param);
param.put("START", String.valueOf(1));
param.put("END", String.valueOf(count));
list=reportsService.getPMJobDetailInfo(param);
//list to workbook
setFileName("TestFile");
}else if("Summary".equalsIgnoreCase(review_type)){
count=reportsService.getPMJobSummaryCount(param);
param.put("START", String.valueOf(1));
param.put("END", String.valueOf(count));
listSum=reportsService.getPMJobSummaryInfo(param);
//list to workbook
setFileName("TestFile");
}
this.workbook2InputStream(wb);
sysend=System.currentTimeMillis();
logger.info("=====PMReviewAction to excel ending. use time:"+Math.round((sysend - sysstart) / 1000)+"s");
System.out.println("=====PMReviewAction to excel ending. use time:"+Math.round((sysend - sysstart) / 1000)+"s");
}catch(Exception e){
e.printStackTrace();
}finally{
}
return SUCCESS;
}
// 将Workbook写入到InputStream
private void workbook2InputStream(HSSFWorkbook workbook) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos);
baos.flush();
byte[] aa = baos.toByteArray();
excelStream = new ByteArrayInputStream(aa, 0, aa.length);
baos.close();
}
private String startdate;
private String enddate;
private String category;
private String review_type;
private String howplay;
public String getStartdate() {
return startdate;
}
public void setStartdate(String startdate) {
this.startdate = startdate;
}
public String getEnddate() {
return enddate;
}
public void setEnddate(String enddate) {
this.enddate = enddate;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getReview_type() {
return review_type;
}
public void setReview_type(String review_type) {
this.review_type = review_type;
}
public String getHowplay() {
return howplay;
}
public void setHowplay(String howplay) {
this.howplay = howplay;
}
struts.xml配置
<action name="PMReviewInfoExcel" class="PMReviewAction" method="PMReviewInfoExcel">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="inputName">excelStream</param>
<param name="contentDisposition">attachment;filename="${fileName}.xls"</param>
<param name="bufferSize">1024</param>
</result>
</action>