package com.wordProcess;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import javax.servlet.ServletContext;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class wordTool
{
private String modelPath;
private String outPath;
private ArrayList bmList;
double picRate;
public wordTool()
{
modelPath = "";
bmList = new ArrayList();
picRate = 566.225;
Properties pro = new Properties();
try
{
String url = this.getClass().getClassLoader().getResource("wordTool.properties").toString().substring(6);
String empUrl = url.replace("%20", " ");//
//empUrl = "D:/jboss/server/default/deploy/qxgl.war/WEB-INF/classes/wordTool.properties";
//empUrl = "D:/jakarta-tomcat-5.0.30/webapps/WordTest/WEB-INF/classes/wordTool.properties";
System.out.println(empUrl);
InputStream ins = new BufferedInputStream(new FileInputStream(empUrl));
pro.load(ins);
modelPath = pro.getProperty("file.ModelPath");
outPath = pro.getProperty("file.OutPath");
ins.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private boolean initBookMark(String modelFile)
{
bmList.clear();
modelFile = modelFile.substring(0, modelFile.indexOf(".rtf"));
String xmlFile = modelPath + modelFile + ".xml";
try
{
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(xmlFile);
Element pEle = document.getRootElement();
List list = pEle.elements();
Iterator iter=list.iterator();
List eList;
int i;
Element bmElement, chElement;
while(iter.hasNext())
{
bmElement=(Element)iter.next();
eList = bmElement.elements();
for(i=0; i<eList.size(); i++)
{
chElement = (Element)(eList.get(i));
if(chElement.getName() != "Type")
continue;
setBookMark(bmElement, Integer.parseInt(chElement.getData().toString()));
}
}
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("书签信息初始化失败!");
return false;
}
return true;
}
private void setBookMark(Element eleBm, int nType)
{
int i, j;
Element eleCh,eleCol;
List eList = eleBm.elements();
List colList;
bookMark bm = new bookMark();
bm.nType = nType;
for(i=0; i<eList.size(); i++)
{
eleCh = (Element)(eList.get(i));
if(eleCh.getName() == "Name")
{
bm.strBookMark = eleCh.getData().toString();
continue;
}
switch(nType)
{
case 1:
if(eleCh.getName() == "Width")
bm.dWidth = Double.parseDouble(eleCh.getData().toString());
if(eleCh.getName() == "Height")
bm.dHeight = Double.parseDouble(eleCh.getData().toString());
break;
case 2:
colList = eleCh.elements();
for(j=0; j<colList.size(); j++)
{
eleCol = (Element)colList.get(j);
bm.colList.add(Integer.parseInt(eleCol.getData().toString()));
}
break;
}
}
bmList.add(bm);
}
public String generateWord(Hashtable hTable, String modelFile)
{
if(false == initBookMark(modelFile))
return null;
String outFile = modelFile;
modelFile = modelPath + modelFile;
InputStream ins;
int nRead;
String strModel = "";
try
{
ins = new FileInputStream(modelFile);
if(ins == null)
{
System.out.println("模板文件不存在!");
return null;
}
byte[] bList = new byte[1638400];
while((nRead = ins.read(bList,0,1638400)) != -1)
{
strModel += new String(bList, 0, nRead);
}
ins.close();
}
catch(Exception e)
{
e.printStackTrace();
}
bookMark bm;
for(int i=0; i<bmList.size(); i++)
{
bm = (bookMark)(bmList.get(i));
if(hTable.get(bm.strBookMark) == null)
continue;
strModel = strModel.replace(GetBookMark(bm.strBookMark), GetRtfStr(bm.nType,hTable.get(bm.strBookMark),bm));
}
Date current=new Date();
SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strTemp = sdf.format(current);
//targetname = sdf.format(current).substring(11,13) + "时";
//targetname += sdf.format(current).substring(14,16) + "分";
//targetname += sdf.format(current).substring(17,19) + "秒";
String targetname = strTemp.substring(0,10) + strTemp.substring(11,13) + strTemp.substring(14,16) + strTemp.substring(17,19);
targetname += "_"+outFile;
try
{
String savePath = getSavePath();
FileWriter fw = new FileWriter(savePath + "\\" + targetname,true);
PrintWriter out = new PrintWriter(fw);
out.println(strModel);
out.close();
fw.close();
System.out.println(savePath+" 该目录下生成文件" + targetname + " 成功");
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
return targetname;
}
private String GetBookMark(String bmName)
{
String bm;
bm = "{\\*\\bkmkstart "+bmName+"}{\\*\\bkmkend "+bmName+"}";
return bm;
}
private String GetRtfStr(int nType, Object pObject, bookMark bm)
{
String strRtf = "";
switch(nType)
{
case 0:
strRtf = strToRtf((String)pObject);
break;
case 1:
strRtf = GetPicRtf((String)pObject, bm);
break;
case 2:
strRtf = GetTbRtf((ArrayList)pObject, bm);
break;
default:
strRtf = (String)pObject;
break;
}
return strRtf;
}
private String strToRtf(String content)
{
if(content.equals(null))
return "";
StringBuffer sb = new StringBuffer("");
try
{
char[] digital = "0123456789ABCDEF".toCharArray();
byte[] bs = content.getBytes();
int inext = 0;
int bit;
for (int i = 0; i < bs.length; i++)
{
bit = (bs[i] & 0x0f0) >> 4;
if(bs[i]>0 && inext == 0)
{
sb.append("\\'");
}
else
{
if(inext == 0)
sb.append("\\dbch\\f31505");
sb.append("\\'");
if(inext == 0)
inext = 1;
else
inext = 2;
}
sb.append(digital[bit]);
bit = bs[i] & 0x0f;
sb.append(digital[bit]);
if(inext > 1)
{
sb.append("\\loch\\f31505");
inext=0;
}
}
}
catch(Exception e)
{
System.out.print(content);
return "";
}
return sb.toString();
}
private String strToRtf(String content, int nFlag)
{
if(content == null)
return "";
StringBuffer sb = new StringBuffer("");
try
{
char[] digital = "0123456789ABCDEF".toCharArray();
byte[] bs = content.getBytes();
int inext = 0;
int bit;
sb.append("{");
for (int i = 0; i < bs.length; i++)
{
bit = (bs[i] & 0x0f0) >> 4;
if(bs[i]>0 && inext == 0)
{
if(nFlag == 1)
sb.append("\\b");
sb.append("\\'");
}
else
{
if(inext == 0)
{
sb.append("\\dbch\\f31505");
if(nFlag == 1)
sb.append("\\b");
}
sb.append("\\
- 1
- 2
- 3
前往页