导出word格式

wangyuchuan520 2012-11-05
java 从页面文本框导出word文本格式和图片,大哥大姐谁知道,回复下小弟,急......
guiyaxiang 2013-07-18
你好,可以使用freemarker模板实现,有疑问可以联系我:QQ:522366204
u013810758 2014-07-23

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

Jsp页面
<a href="javascript:exportOrders('excel')">批量导出订单(excel)</a>

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

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

Js文件
/*
* 批量导出订单
*将要导出数据id进行拼接
*/
function exportOrders(model) {
//messageId是checkbox的id
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:'\@';">&nbsp;<s:property value="#order.orderId"/>&nbsp;
</td>
    <td style="mso-number-format:'\@';">&nbsp;<s:property value="#order.price"/>&nbsp;</td>
    <td style="mso-number-format:'\@';">&nbsp;<s:property value="#order.buyCount"/>&nbsp;</td>
<td style="mso-number-format:'\@';">&nbsp;<s:property value="%{@com.cloudsoar3c.tools.Util@getTotalPriceByOrder(#order.totalPrice)}"/>&nbsp;</td>
<td style="mso-number-format:'\@';">&nbsp;<s:property value="#order.preferentialPrice"/></td>
<td style="mso-number-format:'\@';">&nbsp;<s:property value="%{@com.cloudsoar3c.tools.Util@getActualPayByOrder(#order.totalPrice,#order.preferentialPrice)}"/>&nbsp;</td>
<td style="mso-number-format:'\@';">&nbsp;<s:date name="#order.orderTime" format="yyyy-MM-dd HH:mm:dd"/>&nbsp;</td>
    <td style="mso-number-format:'\@';">&nbsp;<s:date name="#order.completeTime" format="yyyy-MM-dd HH:mm:dd"/>&nbsp;</td>
    <td style="mso-number-format:'\@';">&nbsp;<s:property value="#order.TClient.email"/>&nbsp;</td>
    <td style="mso-number-format:'\@';">&nbsp;<s:property value="#order.TServerHostLicense.hostName"/>&nbsp;</td>
<td style="mso-number-format:'\@';">&nbsp;<s:property value="%{#order.TStatusTypeContent.statusTypeContent}"/>&nbsp;</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:'\@';">&nbsp;<s:property
value="#order.orderId" />&nbsp;
</td>
<td style="mso-number-format:'\@';">&nbsp;<s:property
value="#order.price" />&nbsp;
</td>
<td style="mso-number-format:'\@';">&nbsp;<s:property
value="#order.buyCount" />&nbsp;
</td>
<td style="mso-number-format:'\@';">&nbsp;<s:property
value="%{@com.cloudsoar3c.tools.Util@getTotalPriceByOrder(#order.totalPrice)}" />&nbsp;
</td>
<td style="mso-number-format:'\@';">&nbsp;<s:property
value="#order.preferentialPrice" /></td>
<td style="mso-number-format:'\@';">&nbsp;<s:property
value="%{@com.cloudsoar3c.tools.Util@getActualPayByOrder(#order.totalPrice,#order.preferentialPrice)}" />&nbsp;
</td>
<td style="mso-number-format:'\@';">&nbsp;<s:date
name="#order.orderTime" format="yyyy-MM-dd HH:mm:dd" />&nbsp;
</td>
<td style="mso-number-format:'\@';">&nbsp;<s:date
name="#order.completeTime" format="yyyy-MM-dd HH:mm:dd" />&nbsp;
</td>
<td style="mso-number-format:'\@';">&nbsp;
                       <s:property value="#order.TClient.email" />&nbsp;
</td>
<td style="mso-number-format:'\@';">&nbsp;<s:property
value="#order.TServerHostLicense.hostName" />&nbsp;</td>
<td style="mso-number-format:'\@';">&nbsp;<s:property
value="%{#order.TStatusTypeContent.statusTypeContent}" />&nbsp;
</td>
</tr>
</s:iterator>
</table>
</body>
</HTML>






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


Isp页面值
<div class="h2">
<a href="javascript:exportCustomers('telphone')">导出客户手机号码(txt)</a>&nbsp;&nbsp;
<a href="javascript:exportCustomers('email')">导出客户Email(txt)</a>&nbsp;&nbsp;
</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;
}















Global site tag (gtag.js) - Google Analytics