码迷,mamicode.com
首页 > 其他好文 > 详细

word或excel导出

时间:2014-09-22 21:40:53      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:word或excel导出

说明:实现的功能是checkBox选择多个订单对象,然后由wordexcel导出

 

Jsp页面

<a href="javascript:exportOrders(‘excel‘)">批量导出订单(excel)</a>

 

<a href="javascript:exportOrders(‘word‘)">批量导出订单(word)</a>

 

--------------------------------------------------------------

 

Js文件

/*

 * 批量导出订单

 *将要导出数据id进行拼接

 */

function exportOrders(model) {

//messageIdcheckboxid 

var messageIds = document.getElementsByName("messageId");

var orderIds = "";

var checkedCount = 0;

for ( var i = 0; i < messageIds.length; i++) {

if (messageIds[i].checked) {

checkedCount++;

}

}

if (checkedCount <= 0) {

alert("请至少选择一条数据!");

return;

else {

for ( var i = 0; i < messageIds.length; i = i + 1) {

if (messageIds[i].checked) {

orderIds += ("‘"+messageIds[i].value + "‘;");

}

}

}

document.getElementById("orderIds").value = orderIds;

document.getElementById("model").value = model;

var action = document.myForm.action;// 记录之前的Action,用完之后,恢复原状

document.myForm.action = "order/exportOrders/";

document.myForm.submit();

document.myForm.action = action;

}

 

--------------------------------------------------------------

 

Struts.xml文件

<!-- 批量导出订单信息(excel/word)-->

<actionname="exportOrders"method="exportOrders" class="com.cloudsoar3c.action.OrderAction">

<result name="excel">/WEB-INF/order/exportOrdersExcel.jsp

</result>

<result name="word">/WEB-INF/order/exportOrdersWord.jsp

</result>

<interceptor-ref name="securityStack"></interceptor-ref>

</action>

 

--------------------------------------------------------------

 

Action文件

/**

 * 批量导出订单信息(excel/word)

 * 

 * @return

 */

public String exportOrders(){

String stringOrderIds = this.request.getParameter("orderIds");

String model = this.request.getParameter("model");

String orderIds = Util.charReplacement(stringOrderIds, ";"",");

Util.log(orderIds);

List<TOrder> orderList = this.orderBiz.getOrderListByIds(orderIds);

Util.log("导出的订单集合大小:"+orderList.size());

super.request.setAttribute("orderList", orderList);

return "excel".equals(model.trim()) ? "excel" : "word";

}

 

--------------------------------------------------------------

 

/**

 * 拆分字符串(将"A#B#C#D#E#F#"格式的字符串转换为"A,B,C,D,E,F"),并输出 */

public static String charReplacement(String sourceStr, String sourceChar,

String targetChar) {

String result = "";

 

// 将需要处理的字符串中含有的sourceChar,全部替换为targetChar

String temp = sourceStr.replaceAll(sourceChar, targetChar);

 

// 截取字符串中最后一个字符;

int len = temp.length();

result = temp.substring(0, len - 1);

 

return result;

}

 

-------------------------------------------------------------------

 

导出Excel的返回页面 exportOrdersExcel.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@ page contentType="text/html; charset=GBK" %>

<%@taglib uri="/struts-tags" prefix="s"%>

<%@page import="java.util.*"%>

<%@page import="java.text.SimpleDateFormat"%>

<% response.setContentType("application/vnd.ms-excel;charset=GBK"); %>

<HTML>

<meta http-equiv="Content-Type" content="text/html; charset=GBK">

<head><title>Test</title></head>

<body>

<%

request.setCharacterEncoding("GBK");

  SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss");

 response.setHeader("Content-Disposition""attachment;filename=order_"+sf.format(new Date())+".xls");

%>

<center>订单列表</center>

<table border="1" align="center" width="60%">

  <tr align="center">

    <td>序号</td>

<td>订单号</td>

<td>单价(元)</td>

<td>数量</td>

                       <td>总金额(元)</td>

<td>优惠价格(元)</td>

<td>实际需付金额(元)</td>

<td>下单时间</td>

<td>订单完成时间</td>

<td>客户账号</td>

<td>主机名称</td>

<td>订单状态</td>

  </tr>

  <s:iterator value="#request.orderList" var="order" status="st">

  <tr align="center">

    <td style="mso-number-format:‘\@‘;">${st.count }</td>

    <td style="mso-number-format:‘\@‘;"> <s:property value="#order.orderId"/> 

</td>

    <td style="mso-number-format:‘\@‘;"> <s:property value="#order.price"/> </td>

    <td style="mso-number-format:‘\@‘;"> <s:property value="#order.buyCount"/> </td>

<td style="mso-number-format:‘\@‘;"> <s:property value="%{@com.cloudsoar3c.tools.Util@getTotalPriceByOrder(#order.totalPrice)}"/> </td>

<td style="mso-number-format:‘\@‘;"> <s:property value="#order.preferentialPrice"/></td>

<td style="mso-number-format:‘\@‘;"> <s:property value="%{@com.cloudsoar3c.tools.Util@getActualPayByOrder(#order.totalPrice,#order.preferentialPrice)}"/> </td>

<td style="mso-number-format:‘\@‘;"> <s:date name="#order.orderTime" format="yyyy-MM-dd HH:mm:dd"/> </td>

    <td style="mso-number-format:‘\@‘;"> <s:date name="#order.completeTime" format="yyyy-MM-dd HH:mm:dd"/> </td>

    <td style="mso-number-format:‘\@‘;"> <s:property value="#order.TClient.email"/> </td>

    <td style="mso-number-format:‘\@‘;"> <s:property value="#order.TServerHostLicense.hostName"/> </td>

<td style="mso-number-format:‘\@‘;"> <s:property value="%{#order.TStatusTypeContent.statusTypeContent}"/> </td>

  </tr>

  </s:iterator>

</table>

</body>

</HTML>

-------------------------------------------------------------------

导出word的返回页面 exportOrdersWord.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@ page contentType="text/html; charset=GBK"%>

<%@taglib uri="/struts-tags" prefix="s"%>

<%@page import="java.util.*"%>

<%@page import="java.text.SimpleDateFormat"%>

<%

response.setContentType("application/vnd.ms-word;charset=GBK");

%>

<HTML>

<meta http-equiv="Content-Type" content="text/html; charset=GBK">

<head>

<title>Test</title>

</head>

<body>

<%

request.setCharacterEncoding("GBK");

SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss");

response.setHeader("Content-Disposition",

"attachment;filename=order_" + sf.format(new Date())

".doc");

%>

<center>订单列表</center>

<table border="1" align="center" width="2000px">

<tr align="center">

<td>序号</td>

<td>订单号</td>

<td>单价(元)</td>

<td>数量</td>

<td>总金额(元)</td>

<td>优惠价格(元)</td>

<td>实际需付金额(元)</td>

<td>下单时间</td>

<td>订单完成时间</td>

<td>客户账号</td>

<td>主机名称</td>

<td>订单状态</td>

</tr>

<s:iterator value="#request.orderList" var="order" status="st">

<tr align="center">

<td style="mso-number-format:‘\@‘;">${st.count }</td>

<td style="mso-number-format:‘\@‘;"> <s:property

value="#order.orderId" /> 

</td>

<td style="mso-number-format:‘\@‘;"> <s:property

value="#order.price" /> 

</td>

<td style="mso-number-format:‘\@‘;"> <s:property

value="#order.buyCount" /> 

</td>

<td style="mso-number-format:‘\@‘;"> <s:property

value="%{@com.cloudsoar3c.tools.Util@getTotalPriceByOrder(#order.totalPrice)}" /> 

</td>

<td style="mso-number-format:‘\@‘;"> <s:property

value="#order.preferentialPrice" /></td>

<td style="mso-number-format:‘\@‘;"> <s:property

value="%{@com.cloudsoar3c.tools.Util@getActualPayByOrder(#order.totalPrice,#order.preferentialPrice)}" /> 

</td>

<td style="mso-number-format:‘\@‘;"> <s:date

name="#order.orderTime" format="yyyy-MM-dd HH:mm:dd" /> 

</td>

<td style="mso-number-format:‘\@‘;"> <s:date

name="#order.completeTime" format="yyyy-MM-dd HH:mm:dd" /> 

</td>

<td style="mso-number-format:‘\@‘;"> 

                       <s:property value="#order.TClient.email" /> 

</td>

<td style="mso-number-format:‘\@‘;"> <s:property

value="#order.TServerHostLicense.hostName" /> </td>

<td style="mso-number-format:‘\@‘;"> <s:property

value="%{#order.TStatusTypeContent.statusTypeContent}" /> 

</td>

</tr>

</s:iterator>

</table>

</body>

</HTML>

 

 

 

 

 

 

=================================================================================

------------------------------------------------------------------------------------------------------------------------------------------

                       Txt导出  与上部分完全没关系

------------------------------------------------------------------------------------------------------------------------------------------=================================================================================

 

 

Isp页面值

<div class="h2">

<a href="javascript:exportCustomers(‘telphone‘)">导出客户手机号码(txt)</a>  

<a href="javascript:exportCustomers(‘email‘)">导出客户Email(txt)</a>  

</div>

 

------------------------------------------------------------------------------------------------------------------------------------------

Js文件

/*

 * 批量导出电话或Email

 */

function exportCustomers(model) {

//messageId是checkbox的id 

var messageIds = document.getElementsByName("messageId");

var customerIds = "";

var checkedCount = 0;

for ( var i = 0; i < messageIds.length; i++) {

if (messageIds[i].checked) {

checkedCount++;

}

}

if (checkedCount <= 0) {

alert("请至少选择一条数据!");

return;

else {

//将要导出数据id进行拼接

for ( var i = 0; i < messageIds.length; i = i + 1) {

if (messageIds[i].checked) {

customerIds += (""+messageIds[i].value + ";");

}

}

}

document.getElementById("customerIds").value = customerIds;

document.getElementById("model").value = model;

var action = document.main.action;// 记录之前的Action,用完之后,恢复原状

document.main.action = "customer/exportCustomer/";

document.main.submit();

document.main.action = action;

}

------------------------------------------------------------------------------------------------------------------------------------------

Struts文件 与原来一样 

------------------------------------------------------------------------------------------------------------------------------------------

Action文件

//导出电话 和 email

public String exportCustomer(){

//保存选择的id

String stringCustomerIds = this.request.getParameter("customerIds");

//保存导出类型 tell还是email

String model = this.request.getParameter("model");

String customerIds = Util.charReplacement(stringCustomerIds, ";"",");

String[] ids=customerIds.split(",");

try {

HttpServletResponse response = ServletActionContext.getResponse();

response.setContentType("application/octet-stream;charset=UTF-8");

response.setHeader("Content-Disposition","attachment;filename=\"data.txt\"");

PrintWriter writer = new PrintWriter(response.getOutputStream());

if(model.equals("email")){

for (int i = 0; i < ids.length; i++) {

TCustomerInfo c=customerInfoBiz.findById(Integer.parseInt(ids[i]));

if(StringUtils.hasText(c.getEmail())){

 writer.write(""+c.getEmail()+";\r\r\n");

}

}

}else{//telphone

for (int i = 0; i < ids.length; i++) {

TCustomerInfo c=customerInfoBiz.findById(Integer.parseInt(ids[i]));

if(StringUtils.hasText(c.getTelephone())){

 writer.write(""+c.getTelephone()+";\r\r\n");

}

}

}

        writer.close();

catch (IOException e) {

e.printStackTrace();

}

return null;

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

word或excel导出

标签:word或excel导出

原文地址:http://blog.csdn.net/nevergiveuplzl/article/details/39480901

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!