jsp导出word,java导入access,freemarker

  1. 1使用jsp+struts导出带有表格的word  
  2. 要点包括:1中文乱码;2以word形式保存文件;3struts标签的基本使用;4  
  3.   
  4.   
  5. <%@ page language="java" pageEncoding="utf-8" %>  
  6. <%@ taglib prefix="s" uri="/struts-tags"%>  
  7. <html>  
  8. <head>  
  9.     <%  
  10.         String name =request.getAttribute("name").toString();  
  11.         name = new String(name.getBytes("GBK"),"ISO8859-1")+"";  
  12.         response.setHeader("Content-disposition","attachment; filename="+name+".doc");  
  13.     %>  
  14. </head>  
  15. <body>  
  16. <s:if test="${baseType}==0">  
  17. <h2 >1.A基本信息</h2>  
  18. <table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0  style='width:400pt;font-size:10.0pt;border-collapse:collapse;mso-padding-alt:0cm 0cm 0cm 0cm'>  
  19.         <tr height=30 style='height:22.5pt'>  
  20.             <td style="width:60pt;">姓名</td>  
  21.             <td><s:property value="personInfo.XM"/></td>  
  22.             <td style="width:60pt;">性别</td>  
  23.             <td><s:property value="personInfo.NL"/></td>  
  24.         </tr>  
  25.     </table>  
  26.     <h3 >1.2 A扩展信息</h3>  
  27.     <table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0  style='width:400pt;font-size:10.0pt;border-collapse:collapse;mso-padding-alt:0cm 0cm 0cm 0cm'>  
  28.         <s:iterator value="personInfo.kzxxList" status="stat">  
  29.              <tr height=19 style='height:14.25pt'>  
  30.                   <td style="width:70pt;"><s:property value="typeName" escape="false"/></td>  
  31.                   <td><s:property value="beanValue" escape="false"/></td>  
  32.              </tr>  
  33.         </s:iterator>  
  34.     </table>  
  35.    </s:if>  
  36.    <s:elseif test="${baseType}==1">  
  37.     <h2 >1.B基本信息</h2>  
  38.    </s:elseif>  
  39.     
  40.      
  41.      
  42.      
  43.    <h2 >2.关联信息</h2>  
  44.    <s:if test="docList.size()>0">  
  45.     <h3>文档</h3>  
  46. </s:if>  
  47.           
  48. </body>  
  49. </html>  
  50.   
  51.   
  52. 2 Java将数据导入到Access数据库要点  
  53.   
  54.   
  55. /** 
  56.      * 【业务共享】将指定了Id(由ids决定)的业务数据(由beanType决定)的某些字段(由props决定)导出到access 
  57.      * @param ids 即将导出的数据的id集合。  
  58.      * @param props 导出数据的那些属性 
  59.      * @param beanName 业务表的bean名称 
  60.      * @return 
  61.      */  
  62.     private synchronized final int exportData(List<String> ids,String props,String beanType){  
  63.         if(T.isNullList(ids)){return 0;}else{T.print("一共有"+ids.size()+"条数据即将导出");}  
  64.         if(T.isNull(props)){return 0;}  
  65.         //TODO 基础变量  
  66.         String filePath = T.getRealyPath()+"template/bussShare.mdb";  
  67.         String tableName = Constant.BASEINFO_ZZ.equals(beanType)?"表B":Constant.BASEINFO_MZ.equals(beanType)?"表C":"表A";  
  68.           
  69.         try {  
  70.             //创建jdbc连接。  
  71.             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
  72.             Properties prop = new Properties();  
  73.             Connection conn = DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+filePath+";", prop);  
  74.             Statement st = conn.createStatement();  
  75.               
  76.             //TODO 删除旧表  
  77.             try{st.execute("drop table 表A");}catch(Exception e){ }  
  78.             try{st.execute("drop table 表B");}catch(Exception e){ }  
  79.             try{st.execute("drop table 表C");}catch(Exception e){ }  
  80.               
  81.             //TODO 创建新表。  
  82.             StringBuffer buffer=new StringBuffer(500);  
  83.             buffer.append("create table ").append(tableName).append("(id integer");//主键ID  
  84.             String[] colNames = T.getJSON(props, "before").split(",");  
  85.             for (int i = 0; i < colNames.length; i++) {  
  86.                 buffer.append(",").append(colNames[i]).append(" memo ");  
  87.             }  
  88.             buffer.append(" ) ");//TODO carate table最后一个括号。  
  89.             st.execute(buffer.toString());  
  90.             //TODO 创建新表。END  
  91.               
  92.               
  93.             //TODO 从gx读取数据,同时写入access数据表中。  
  94.             String[] colArr= T.getJSON(props, "after").split(",");  
  95.             int propSize = colArr.length;//有这么些字段。  
  96.               
  97.             //TODO 拼凑插入数据的sql模板。  
  98.             StringBuffer insertBaseBuffer=new StringBuffer(500);  
  99.             insertBaseBuffer.append("insert into "+tableName+" values( {id}");  
  100.             for (int i = 0; i < propSize; i++) {  
  101.                 insertBaseBuffer.append(",").append("{"+colArr[i].toLowerCase().trim()+"}");  
  102.             }  
  103.             insertBaseBuffer.append(")");  
  104.             String insertBase = insertBaseBuffer.toString();  
  105.             //TODO 拼凑插入数据的sql模板END  
  106.               
  107.             //根据模板,生成带有真实数据的sql插入数据  
  108.             String insertsql ;  
  109.             int len = ids.size();  
  110.             PersonInfoLogic personLogic =  (PersonInfoLogic) ApplicationContextHolder.getBean("personInfoLogic");  
  111.             Object obj = "";  
  112.             ElecaddressInfo e;  
  113.             OrgInfo o;  
  114.             PersonInfo p;  
  115.             Method[] ms;  
  116.             Method m;  
  117.             String _name;  
  118.             String oValue ;  
  119.             for ( int i=0; i < len; i++) {  
  120.                 insertsql =  insertBase.replace("{id}", (i+1)+"");  
  121.                 p = personLogic.get(ids.get(i));   
  122.                 personLogic.convertDic(p, "view");  
  123.                 obj  = p;  
  124.                 ms = obj.getClass().getMethods();//TODO 所有get方法和set方法的集合。  
  125.                 for(int j=0;j<ms.length;j++){  
  126.                     if(ms[j].getName().indexOf("get")>=0){//只要get方法。  
  127.                         m = ms[j];  
  128.                         _name = m.getName().replace("get", "").toLowerCase();  
  129.                         oValue =T.isNull((m.invoke(obj, null)+""),"").toString();  
  130.                         insertsql = insertsql.replace("{"+_name+"}", addChar(oValue));  
  131.                     }  
  132.                 }  
  133.                 st.execute(insertsql);  
  134.                 if(i%50==0){  
  135.                     T.print(i);  
  136.                 }  
  137.             }  
  138.             st.clearBatch();  
  139.             st.close();  
  140.         } catch (Exception e) {  
  141.             e.printStackTrace();  
  142.         }  
  143.         return 0;  
  144.     }  
  145.   
  146.   
  147. 3 使用freemarker导出数据  
  148.   
  149. /** 
  150.  * 使用freemark导出数据 
  151.  * @param id 数据ID 
  152.  * @param dataType 数据类型 
  153.  * @return 生成的物理文件的物理路径 
  154.  */  
  155.   
  156. public static String exportByFreeMark(String baseId,String dataType){  
  157. PersonInfoLogic personInfoLogic =  (PersonInfoLogic) ApplicationContextHolder.getBean("personInfoLogic");  
  158. try {  
  159. Configuration cfg=new Configuration();  
  160. String filePath = WebUtil.getBasePath()+"template";  
  161. cfg.setDirectoryForTemplateLoading(new File(filePath));  
  162. cfg.setObjectWrapper(new DefaultObjectWrapper());  
  163. Template temp=cfg.getTemplate("toword.xml","GBK");  
  164.   
  165. Map root=new HashMap();//根  
  166. List<Map> personlist = new ArrayList<Map>();//关联数据对象  
  167.   
  168. root.put("dataType", "person");//数据类型使用dataType做标识  
  169. PersonInfo instance = personInfoLogic.getViewObj(baseId, null);  
  170. Map map_person = FreeMarkTool.converPerson(instance);  
  171. root.put("person",map_person);//对象主题数据  
  172.   
  173. String imgPath = WebUtil.getBasePath()+"images/no_image.jpg";  
  174. root.put("image", T.getFileEncode(imgPath));//图片信息  
  175.   
  176. Date currDate=new Date();  
  177. String name=DateUtil.formatDateTime(currDate, "yyyy-MM-dd")+"_"+currDate.getTime();//文件名  
  178. String fileUrl = "upload/file/"+name+".doc";  
  179. File file = new File(T.getRealyPath()+fileUrl);  
  180. if(file.exists()){file.delete();}  
  181. FileOutputStream fos = new FileOutputStream(file);  
  182. Writer out=new OutputStreamWriter(fos,"GBK");  
  183. temp.process(root, out);  
  184. out.flush();  
  185. fos.close();//TODO 必须关闭文件输出流 否则不能删除临时文件。  
  186. return fileUrl;  
  187. } catch (Exception e) {  
  188.     e.printStackTrace();  
  189. }finally{  
  190.     //清空缓存。  
  191.     personInfoLogic.getPersonInfoDAO().getHibSession().clear();  
  192. }  
  193. return null;  
  194. }  
  195.   
  196. /** 
  197.  * 将一个数据对象转变成map对象 
  198.  * @param instance 数据对象 
  199.  * @return 使用map模拟的数据对象 
  200.  */  
  201. public static Map converPerson(PersonInfo instance){  
  202.     String json = "name:{0},age:{1}";  
  203.     Object[] params = new Object[50];  
  204.     params[0] = T.isNullHTML(instance.getName(),"--");  
  205.     params[1] = T.isNullHTML(instance.getAge(),"--");  
  206.     json = MessageFormat.format(json, params);  
  207.     Map map = T.getMapFromJSON(json);  
  208.     return map;  
  209. }  
  210.   
  211.   
  212. 3 freemarker标签  
  213. 备注:首先使用word另存为生成xml模板,然后替换内容。  
  214.   
  215. <#if dataType=="person">  
  216.     ${obj.Name}  
  217.     <#list kzxxList as kzxx_item>  
  218.         ${kzxx_item.typeName}  
  219.     </#list>  
  220. <#elseif dataType=="orginfo">  
  221.   
  222. <#else>  
  223.     <w:p><w:r><w:t>无</w:t></w:r></w:p>  
  224. </#if>  
  225.       
  226.       
  227. <#if personlist?size gt 0 >  
  228.   
  229. </#if> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值