javaweb項目,將數據寫入word文檔中,轉爲PDF格式,並在線預覽PDF

ok,大家看到標題就能感受到這個功能的強大,不僅把從數據庫中查出來的數據寫入到了word文檔中,還將word轉爲了pdf,並且有pdf在線預覽的方法,順便說一下,在線預覽頁面可是有打印功能,並且手機和各種瀏覽器全部支持的哦!
廢話不多說了,看代碼。
第一步:將數據庫查出的數據存入到word中。

在這裏我們使用一個wordUtil的工具類,源碼如下:

 package com.fh.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

public class WordUtil {
	
	/**
      * 將源文件拷貝一份
	      * @param source
	      * @param target
	      */
	     public static void nioTransferCopy(File source, File target) {  
	         FileChannel in = null;  
	         FileChannel out = null;  
	         FileInputStream inStream = null;  
	         FileOutputStream outStream = null;  
	         try {  
	             inStream = new FileInputStream(source);  
	             outStream = new FileOutputStream(target);  
	             in = inStream.getChannel();  
	             out = outStream.getChannel();  
	             in.transferTo(0, in.size(), out);  
	         } catch (IOException e) {  
	             e.printStackTrace();  
	        }  
	    } 
	
	
	/**
	 * 讀取word文件的內容
	 * @param fs
	 * @return
	 * @throws IOException
	 */
	public static String doc2String(FileInputStream fs) throws IOException {
		StringBuilder result = new StringBuilder();
		WordExtractor re = new WordExtractor(fs);
		result.append(re.getText());
		re.close();
		return result.toString();
	}
	
	/**
	 * 寫入word文件的內容
	 * @param path
	 * @param data
	 */
	public static void writeDataDoc(String path,String data){
		OutputStream ostream=null;
      try {
          ostream = new FileOutputStream(path);
          ostream.write(data.getBytes());
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }finally{
          if(ostream != null){
              try {
                  ostream.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  }
	
	 public static void writeDoc(String path,Map<String, String> map) {  
		          try {  
		              FileInputStream in = new FileInputStream(path);  
		             HWPFDocument hdt = new HWPFDocument(in);  
		             Range range = hdt.getRange();  
		              //讀取word文本內容  
		              System.out.println(range.text());  
		            //替換文本內容  
		              for (Map.Entry<String,String> entry:map.entrySet()) {  
		                  range.replaceText(entry.getKey(),entry.getValue());  
		             }  
		
		              ByteArrayOutputStream ostream = new ByteArrayOutputStream();  
		             FileOutputStream out = new FileOutputStream(path);  
		              hdt.write(ostream);  
		             //輸出字節流  
		             out.write(ostream.toByteArray());  
		              ostream.close();  
		          } catch (IOException e) {  
		              e.printStackTrace();  
		         } catch (Exception e) {  
		              e.printStackTrace();  
		         }  
		     }
	
	 
	 /**
	  * 添加word信息
	  * 
	  * @sourcePath 原文檔路徑
	  * @targetPath 存文檔路徑
	  * @maps 需要替換的信息map
	  */
	 public static void addWordInfo(String sourcePath,String targetPath,Map<String,String> maps) throws FileNotFoundException, IOException{
		
		// 創建新的文檔
		File sourceFile = new File(sourcePath);
		File targetFile = new File(targetPath);
		WordUtil.nioTransferCopy(sourceFile,targetFile);
		// 替換某些字段
		WordUtil.writeDoc(targetPath,maps);
		
	 }
	
	public static void main(String[] args) throws FileNotFoundException, IOException {
		
		
		WordUtil.nioTransferCopy(new File("C:\\javaWork\\apache-tomcat-8.0.15\\webapps\\TCGL\\static\\ceshi\\ceshi.doc"),new File("E:\\ceshi.doc"));
		
		// 讀取文件的內容
		File oldFile = new File("E:\\009.doc");
		String datas =WordUtil.doc2String(new FileInputStream(oldFile));
		// 替換某些字段
		Map <String,String> maps = new HashMap<String, String>();
		maps.put("tihuanziduan1", "焦糖沙琪瑪");
		maps.put("tihuanziduan2, "焦糖橙子");
		WordUtil.writeDoc("E:\\009.doc",maps);
	
	}
	
}

你可以直接複製進去,放到你的工具類包中,改一下包路徑即可。
jar包在這裏:
https://pan.baidu.com/s/1e1R3ASuhQBiXQY00qJ_KUg
提取碼: kan7
第二步:將數據轉爲word
拿到工具類以後就可以進行數據轉word的操作了,這裏需要注意的是你要先在“\webapps\你的項目名\static\在建一個文件夾\”的項目路徑下,先放置一個word模板。就是這一段代碼。new File是生成以後的doc路徑。注意:兩個路徑下都要放,模板是一模一樣的。就是下面這段代碼。

WordUtil.nioTransferCopy(new File("C:\\javaWork\\apache-tomcat-8.0.15\\webapps\\CSXM\\static\\ceshi\\ceshi.doc"),new File("E:\\ceshi.doc"));

在word模板中需要寫入你想要的模板格式,以及需要替換的字段。
以我工具類測試方法爲例,他就應該是這樣的(沒有加表格或者什麼樣式,單純的寫了兩個字段。):
在這裏插入圖片描述
當程序運行的時候,就會將字段替換成 焦糖沙琪瑪,焦糖橙子。
要查看的路徑呢,就是E:\ceshi.doc 這個文檔。
好,就寫到這裏,下一篇發佈word轉pdf並在線預覽。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章