import java.io.BufferedInputStream; java
import java.io.BufferedOutputStream; dom
import java.io.File; ide
import java.io.FileInputStream; spa
import java.io.FileOutputStream; orm
import java.io.InputStream; 文檔
import java.io.OutputStream; get
import java.text.SimpleDateFormat; input
import java.util.ArrayList; it
import java.util.Date; io
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class WordWriter {
private WordOperator word;
public WordWriter(String filePath){
word=new WordOperator();
word.openDocument(filePath);
}
public WordWriter(InputStream input,String filePath,String fileName) throws Exception{
String path=saveAsDocFile(input,filePath,fileName);
word=new WordOperator();
word.openDocument(path);
}
/**
* 將word文檔輸入流保存爲本地獲得word文件
* @param input
* @param filePath
* @param fileName
* @throws Exception
*/
@SuppressWarnings("unused")
private String saveAsDocFile(InputStream input,String filePath,String fileName)throws Exception{
if(!StringUtils.isValidateString(filePath)||!StringUtils.isValidateString(fileName)){
throw new Exception("The filePath or fileName is error");
}
if(input==null){
throw new Exception("InputStream is null");
}
File file=new File(filePath);
if(!file.exists()){
throw new Exception(" The FilePath is null");
}
filePath = validateFilePath(filePath);
fileName = getRandomFileName(fileName);
InputStream in=null;
OutputStream out=null;
try{
in=new BufferedInputStream(input);
out=new BufferedOutputStream(new FileOutputStream(filePath+fileName));
byte[] b=new byte[1024];
for(int p=0; (p=in.read(b))!=-1;){
out.write(b);
out.flush();
}
}finally{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
return filePath+fileName;
}
/**
* 驗證Word文件路徑
* @param filePath
* @return
*/
private String validateFilePath(String filePath) {
if((filePath.lastIndexOf("\\\\")==-1)&&(filePath.lastIndexOf("/")==-1)){
filePath=filePath+"/";
}
return filePath;
}
/**
* 生成一個新的文件名(保證文件名不相同)
* @param fileName
* @return
*/
private String getRandomFileName(String fileName) {
fileName= fileName + "_"+ new SimpleDateFormat("yyyyMMddHHmmssZ").format(new Date())+".doc";
return fileName;
}
/**
* replaceText
* @param map
*/
public void replaceAllText(Map<String,String> map){
if(map==null){
return;
}
Set<String> keys=map.keySet();
Iterator<String> it=keys.iterator();
while(it.hasNext()){
String key=it.next();
word.replaceAllText(key, map.get(key));
}
}
/**
* add details
* @param values
*/
public void insertContextInRow(List<Map<String,String>> values,int tableIndex){
if(tableIndex<=1){
tableIndex=1;
}
if(values==null||values.size()<=0){
return;
}
int[] p=null;
Map<String,String> m=values.get(0);
Set<String> keys=m.keySet();
Iterator<String> it=keys.iterator();
while(it.hasNext()){
String str=it.next();
int[] a=word.getTableCellPostion(str, tableIndex);
if(a!=null&&a[0]!=0){
p=a;
}
}
if(p!=null&&p[0]!=0){
for(int i=1;i<values.size();i++){
word.addTableRow(tableIndex,p[0]);//在表格插入行數
}
}
Iterator<String> it2=keys.iterator();
while(it2.hasNext()){
int row=p[0];
int col=0;
String str=it2.next();
int[]a=word.getTableCellPostion(str, tableIndex);
if(a!=null){
col=a[1];
}
for(Map<String,String> map:values){
word.putTxtToCell(tableIndex, row, col, map.get(str));
row++;
}
}
}
/**
* close document
*/
public void close(){
word.closeDocument();
word.close();
}
/**
* 依據Word文件完整路徑刪除文件
* @param path
* @throws Exception
*/
public void deleteWordFile(String path) throws Exception{
File f=new File(path);
if(!f.exists()){
throw new Exception("The file is not exists");
}
f.delete();
}
/**
*
* @param args
* @throws Exception
*/
public static void main(String args[]) throws Exception{
InputStream in=new FileInputStream("d:\\aaa.doc");
String path="d:\\qq";
String fileName="aaa";
WordWriter writer=new WordWriter(in,path,fileName);
Map<String,String> map1=new HashMap<String,String>();
map1.put("p21", "上海商哲");
map1.put("p12", "1550");
writer.replaceAllText(map1);
List<Map<String,String>> values =new ArrayList<Map<String,String>>();
for(int i=0;i<10;i++){
Map<String,String> map=new HashMap<String,String>();
map.put("$1", "111111111111");
map.put("$2", "222222222222");
map.put("$3", "333333333333");
map.put("$4", "444444444444");
map.put("$5", "555555555555");
map.put("$6", "666666666666");
values.add(map);
}
writer.insertContextInRow(values, 1);
writer.close();
}
}