jsp 형식 으로 doc 파일 내 보 내기 실례 상세 설명

6275 단어 jspdoc 파일
jsp 형식 으로 doc 파일 내 보 내기 실례 상세 설명
원리:doc 파일 은 사실 xml 파일 로 저장 할 수 있 습 니 다.이 xml 파일 은 문자열 로 doc 파일 의 표현 형식 을 표시 합 니 다.우 리 는 자바 로 작성 할 내용 을 교체 하고 고객 에 게 다운로드 하면 됩 니 다.
1.우선 문서 에 따라 데 이 터 를 작성 하 세 요.
2.문 서 를 xml 파일 로 저장 한 다음 xml 파일 을 편집 하고 작성 한 내용 을 어떤 형식 으로 바 꿉 니 다.예 를 들 어 이름 을 3 장 3 으로${name}으로 바 꿉 니 다.
3.파일 을 읽 고 파일 의${name}을 실제 이름 으로 바 꿉 니 다.
4.다운로드.
다음 코드 보기:
우선 그 전환 류.

package com.my.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;

public class DowloadDocByTemplate {

  public InputStream dowload(String templatePath,Map map) throws IOException{
    
    File file = new File(templatePath);
    StringBuffer sb = new StringBuffer();
      BufferedReader br;
        br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
        String s;
        while ((s = br.readLine()) != null) {
          sb.append(s);
          
        }
        br.close();
         s = sb.toString();
          Iterator<String> keys = map.keySet().iterator();
          while(keys.hasNext()) {
            String key = (String) keys.next();
            s=s.replace(key, (String) map.get(key));
         }

          return new ByteArrayInputStream(s.getBytes("UTF-8"));
  }
}
  

그리고 action:

/**
   *       
   * @return
   */
  public String downLoadcheck(){
    
    return SUCCESS;
  }
  
  public String getTargetFileName(){
    
    return "zengguoqg.doc";
  }
  public InputStream getInputStream(){
    //ExportToWordUtil etw = new ExportToWordUtil();
    StringBuffer carnumber = new StringBuffer("<w:tr w:rsidR='00000000'> <w:trPr> <w:trHeight w:hRule='exact' w:val='482'/> </w:trPr>");
    String s1 = "<w:tc><w:tcPr><w:tcW w:w=\"236\" w:type=\"dxa\"/></w:tcPr><w:p w:rsidR=\"00000000\" w:rsidRDefault=\"009F38EE\">" +
        "<w:pPr><w:rPr><w:rFonts w:hint=\"eastAsia\"/></w:rPr></w:pPr><w:r>" +
            "<w:rPr><w:rFonts w:hint=\"eastAsia\"/></w:rPr><w:t>";
    String number = "450981199202073518";
    char[] ch = number.toCharArray();
    for(int i =0;i<ch.length;i++){
      carnumber.append(s1).append(ch[i]).append("</w:t></w:r></w:p></w:tc>");
      
    }
    carnumber.append("</w:tr>");
    Map<String,String> map = new HashMap();
    map.put("${name}","  ");
    map.put("${sex}"," ");
    map.put("${writeDate}", "2014.1.2");
    map.put("${cardNumber}", "1100106");
    map.put("${personNumber}", carnumber.toString());
    map.put("${birthday}", "2014.1.2");
    map.put("${Education}", "  ");
    map.put("${marride}", "  ");
    map.put("${comeFrom}", "  ");
    map.put("${nation}", "  ");
    map.put("${ResidenceAddress}", "  ");
    map.put("${PostalCode}", "543002");
    map.put("${QQ}", "1104566175");
    map.put("${job}", "     ");
    map.put("${politicalStatus}", "    ");
    map.put("${GraduateSchool}", "  ");
    map.put("${professional}", "    ");
    map.put("${beginWorkTime}", "2014.1.2");
    map.put("${bankCarNumber}", "622709");
    map.put("${emergencyContactPerson}", "  ");
    map.put("${relationship}", "  ");
    map.put("${ummoveTelephone}", "0774133");
    map.put("${mobilePhone}", "1350335");
    map.put("${adreess}", "  ");
    map.put("${mainExperience}", "   ");
    map.put("${favoriteAndGoodAt}", "     ");
    
    
    String path = ServletActionContext.getServletContext().getRealPath("/fileTemplate/renyuandengjibiao2.xml");
    System.out.println(path);
    
    //return etw.export(path, "test.xml", map, "out.doc");
    try {
      return new DowloadDocByTemplate().dowload(path, map);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return null;
    }
  }
  
}
마지막 으로 Strut 설정 다운로드: 

<package name="dowload" extends="struts-default">
    <action name="dowload" class="zhaopingAction" method="downLoadcheck">
    <result name="success" type="stream">
     <param name="contentType">application/ZIP</param><!--    text/plain-->    
     <param name="inputName">inputStream</param>  
     <param name="contentDisposition">filename="${targetFileName}"</param>    
     <param name="bufferSize">4096</param><!--           -->    
    </result>
    </action>
끝났어,이게 다 야.xml 파일 은 WebRoot/fileTemplate/renyuandengjibiao 2.xml 에 놓 겠 습 니 다.
궁금 한 점 이 있 으 시 면 메 시 지 를 남기 거나 본 사이트 지역사회 에 가서 토론 을 하 세 요.읽 어 주 셔 서 감사합니다. 도움 이 되 었 으 면 좋 겠 습 니 다.본 사이트 에 대한 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기