一个导出Excel的方法会重复执行两次,求解!
页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>fineplat</title>
<link href="<%=request.getContextPath() %>/web/css/style.css" rel="stylesheet" type="text/css" />
<link href="<%=request.getContextPath() %>/web/css/reset.css" rel="stylesheet" type="text/css" />
<link href="<%=request.getContextPath() %>/web/css/invalid.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/web/js/jquery/themes/gray/easyui.css">
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/web/js/jquery/themes/icon.css">
<script type="text/javascript" src="<%=request.getContextPath() %>/web/js/jquery/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/web/js/jquery/jquery.easyui.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/web/js/public.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/web/js/button.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/web/js/My97DatePicker/WdatePicker.js"></script>
<script type="text/JavaScript" language="javascript">
function query()
{
document.form1.action="feelog_monthlyfeeSummary.action";
document.form1.submit();
}
function exportExcelFile()
{
document.form1.action="feelog_exportExcelFile.action";
document.form1.submit();
}
</script>
</head>
<body>
<div id="body-wrapper"> <!-- Wrapper for the radial gradient background -->
<div id="main-content">
<div class="bread">当前位置: 短信管理 > 包月计费统计</div>
<div class="clear"></div> <!-- End .clear -->
<div class="content-box"><!-- Start Content Box -->
<div class="content-box-content">
<div class="tab-content default-tab" id="tab1"> <!-- This is the target div. id must match the href of this div's tab -->
<table>
<tfoot>
<tr>
<td style="border-bottom: 1px solid #ddd;">
<form name="form1" method="post" redirect="true"/>
<div class="bulk-actions align-left">
业务:<select name="monthlyfeeSummaryQueryDTO.serviceid" id="monthlyfeeSummaryQueryDTO.serviceid" >
<option value="" selected="selected" >不限</option>
<s:iterator value="serviceList">
<option value="${sid}" <s:if test="sid==monthlyfeeSummaryQueryDTO.serviceid">selected="selected"</s:if>>${servicename}</option>
</s:iterator>
</select>
时间:<input id="monthlyfeeSummaryQueryDTO.startTime" value="${monthlyfeeSummaryQueryDTO.startTime }" name="monthlyfeeSummaryQueryDTO.startTime" class="Wdate" onclick="WdatePicker({maxDate:'#F{$dp.$D(\'monthlyfeeSummaryQueryDTO.endTime\')||\'%y-%M-%d\'}'})" readonly="readonly" />
-至-
<input id="monthlyfeeSummaryQueryDTO.endTime" value="${monthlyfeeSummaryQueryDTO.endTime }" name="monthlyfeeSummaryQueryDTO.endTime" class="Wdate" onclick="WdatePicker({minDate:'#F{$dp.$D(\'monthlyfeeSummaryQueryDTO.startTime\')}',maxDate:'%y-%M-%d'})" readonly="readonly" />
<a id="message.feelog.query" class="button" href="#" onclick="query();"> 查 询 </a>
<a id="" class="button" href="#" onclick="exportExcelFile();"> 导 出 </a>
</div>
</form>
<!-- End .pagination -->
<div class="clear"></div>
</td>
</tr>
</tfoot>
</table>
<table>
<tbody>
<img title="包月计费统计" src="<%=request.getContextPath() %>/${url }" />
</tbody>
</table>
</div> <!-- End #tab1 -->
</div> <!-- End .content-box-content -->
</div> <!-- End .content-box -->
<div class="clear"></div>
</div> <!-- End #main-content -->
</div>
</body>
</html>
struts.xml配置:
<package name="message" namespace="/message" extends="fineplat"><action name="feelog_*" class="com.box.fineplat.www.action.message.FeelogAction" method="{1}">
<result name="page">/web/jsp/message/monthlyfeeSummarylist.jsp</result>
<result name="summary">/web/jsp/message/monthlyfeeSummarylist.jsp</result>
</action>
</package>
action方法:
public String exportExcelFile() throws IOException, ParseException {
try {
getAllServiceList();
List mfeeloglist = this.feelogService.getMonthlyfeeSummary(this.getMonthlyfeeSummaryQueryDTO());
List arealist = new ArrayList<String>();
List contentlist = new ArrayList<Integer>();
for (int i = 0; i < mfeeloglist.size(); i++) {
MonthlyfeeSummaryDTO model = (MonthlyfeeSummaryDTO) mfeeloglist.get(i);
String area = super.changeAreaText(model.getAreacode());
arealist.add(area);
contentlist.add(model.getTotal());
}
String title = "地区,费用(元)";
super.printExcel(exportExcel("包月计费统计", title, arealist, contentlist));
} catch (ServiceException e) {
log.error("异常", e);
}
return "page";
}
继承的相关父类方法:
public void printExcel(HSSFWorkbook workbook) throws IOException {
HttpServletResponse response = ServletActionContext.getResponse();
response.setHeader("Content-disposition", "attachment; filename=export.xls");
response.setContentType("application/msexcel;charset=UTF-8");
OutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
}
public HSSFWorkbook exportExcel(String sheetname, String strTitles, List arealist, List contentlist) throws IOException, ParseException {
HSSFWorkbook workbook = null;
// 创建工作簿实例
workbook = new HSSFWorkbook();
// 创建工作表实例
HSSFSheet sheet = workbook.createSheet(sheetname);
// 设置列宽
this.setSheetColumnWidth(sheet);
// 获取样式
HSSFCellStyle style = this.createTitleStyle(workbook);
if (arealist != null && arealist.size() > 0) {
// 创建第一行标题,标题名字的本地信息通过resources从资源文件中获取
HSSFRow row = sheet.createRow((short) 0);// 建立新行
for (int i = 0; i < strTitles.split(",").length; i++) {
this.createCell(row, i, style, HSSFCell.CELL_TYPE_STRING, this.getText(strTitles.split(",")[i]));
}
}
String textTitle = "";
for (int i = 0; i < 11; i++) {
HSSFRow row1 = sheet.createRow((short) (i + 1));// 建立新行
this.createCell(row1, 0, style, HSSFCell.CELL_TYPE_STRING, arealist.get(i));
this.createCell(row1, 1, style, HSSFCell.CELL_TYPE_NUMERIC, contentlist.get(i));
}
return workbook;
}
//设置Excel单元格列宽
private void setSheetColumnWidth(HSSFSheet sheet) {
// 根据你数据里面的记录有多少列,就设置多少列
sheet.setColumnWidth((short) 0, (short) 3000);
sheet.setColumnWidth((short) 1, (short) 3200);
}
// 设置excel的title样式
private HSSFCellStyle createTitleStyle(HSSFWorkbook wb) {
HSSFFont fontStyle = null;
HSSFCellStyle cellStyle = null;
fontStyle = wb.createFont();
fontStyle.setFontName("宋体");
fontStyle.setFontHeightInPoints((short)14);
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle style = wb.createCellStyle();
style.setFont(fontStyle);
return style;
}
// 创建Excel单元格
private void createCell(HSSFRow row, int column, HSSFCellStyle style, int cellType, Object value) {
HSSFCell cell = row.createCell((short) column);
if (style != null) {
cell.setCellStyle(style);
}
switch (cellType) {
case HSSFCell.CELL_TYPE_BLANK: {
}
break;
case HSSFCell.CELL_TYPE_STRING: {
cell.setCellValue(value.toString());
}
break;
case HSSFCell.CELL_TYPE_NUMERIC: {
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(Double.parseDouble(value.toString()));
}
break;
default:
break;
}
}
实在不知为何,求各位高手指点,感激不尽!
[最优解释]
不知道是不是你的页面会提交两次、在你的提交js方法中alert()一下、看看是不是弹出两次!
[其他解释]
导出时怎么会两次跳到Action里的方法里呢?
这样导出当然是两次了!!!
[其他解释]
<form name="form1" method="post" redirect="true"/>
和上面的 redirect 有关系么?
[其他解释]
请楼主仔细检查一下js代码,极有可能是提交了2次的缘故
[其他解释]
该回复于2011-05-05 09:24:13被版主删除
[其他解释]
页面没有提交两次,页面上是有参数传到action方法里的,第一次到这个方法的时候参数是有的,但是第二次跳到这个方法里参数就没了
[其他解释]
现在就是想知道它为什么会执行两次那个方法呀?!
[其他解释]
额 看来不是我一个人碰到这个问题~~
不过当时时间紧 没有追溯问题~~ 只是简单的处理了下 是否有提交请求file对象这个参数 没有就return了
[其他解释]
很头疼,断点调试,exportExcelFile方法执行一次之后就到了.class文件里,释放断点之后就又到这个方法里了,不知道怎么控制
[其他解释]
很奇怪的问题 细节上的
[其他解释]
function exportExcelFile()
{
document.form1.action="feelog_exportExcelFile.action";
document.form1.submit();
}
document.form1.action="feelog_exportExcelFile.action";
document.form1.submit();
这两句能不能换成
别的。我看是在这里执行了两次!
[其他解释]
和redirect 没有关系,只是看到别人有这样的问题说是加上这个,我这个加不加没区别,没起作用的
[其他解释]
以前和lz遇到同样的问题!!很la一起求解!!
[其他解释]
提交方式没有问题,没有两次提交
[其他解释]
我也遇到这个问题,怎么解决啊
[其他解释]
原来做jasper 产生报表时也产生了类似的问题,但是由于当时不影响系统的运行 就没有解决,所以一直记忆犹新
[其他解释]
http://bbs.csdn.net/topics/390301139 楼主解决了吗 我也遇到这样的问题 尼玛坑爹的导表