有位网友说导出到word文档时原来的表格样式没有了,我认为是你在导出到word时没有设置word样式,之前我没有导出到word的太多经验,Excel导入导出是做了些,呵呵^_^
下面给个导出到word的例子,能设置表格,我也是刚看的,希望对需要的朋友有帮助.
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.spsoft.basic.etfproj.service.EtfprojService;
import com.spsoft.basic.etmproj.service.EtmprojService;
import com.spsoft.construct.projmomerabili.domain.Projmemorabilia;
import com.spsoft.construct.projmomerabili.service.ProjmemorabiliaService;
import com.spsoft.framework.struts.BasePerformAction;
import com.spsoft.global.service.Services;
public class ExportToWordAction extends BasePerformAction {
public ActionForward perform(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String projId = request.getParameter("projId");
String projType = request.getParameter("projType");
String projName = "";
if (projType.equals("1")) {
EtfprojService service = (EtfprojService)Services.GetService(EtfprojService.SERVICE_NAME);
projName = service.getEtfproj(Long.valueOf(projId)).getEtfprojname();
}
else if (projType.equals("2")) {
EtmprojService service = (EtmprojService)Services.GetService(EtmprojService.SERVICE_NAME);
projName = service.getEtmproj(Long.valueOf(projId)).getEtmprojname();
}
try {
String fileName = projName + "-工程大事记";
response.setHeader("content-disposition","attachment; filename="
+ new String(fileName.getBytes("GBK"),"ISO-8859-1")+".doc");
response.setContentType("application/vnd.ms-word;charset=UTF-8");
ProjmemorabiliaService service=(ProjmemorabiliaService)
Services.GetService(ProjmemorabiliaService.NAME);
//获得需要显示的信息列表
Collection _list = service.listProjmemorabiliaByProjidAndProjtype(projId, projType);
StringBuffer sb = new StringBuffer();
sb.append("<html>").append("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>")
.append("<body>")
.append("<table width='100%' border='0' cellpadding='0' cellspacing='1' bgcolor='#cccccc'>")
.append("<tr>")
.append("<td align='center' colspan='2'>")
.append("<B>" + fileName + "</B>")
.append("</td>")
.append("</tr>");
sb.append("<tr>")
.append("<td>")
.append("时间")
.append("</td>")
.append("<td>")
.append("内容")
.append("</td>")
.append("</tr>");
for (Iterator it = _list.iterator(); it.hasNext();) {
Projmemorabilia pm = (Projmemorabilia) it.next();
sb.append("<tr bgcolor='#ffffff'>")
.append("<td>")
.append(new SimpleDateFormat("yyyy年MM月dd日").format(pm.getFilldate()))
.append("</td>")
.append("<td>")
.append(pm.getMemorabiliacontent())
.append("</td>")
.append("</tr>");
}
sb.append("</table></body></html>");
response.getWriter().write(sb.toString());
}
catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
出来效果,有表格,希望对需要的朋友有帮助: