java根据rtf模版导出word
来源:互联网 发布:js按钮弹出对话框 编辑:程序博客网 时间:2024/05/22 17:22
自定义rtf模版
HashMap<String,Object> map = new HashMap<String,Object>();
//基本信息
map.put("name", StringUtil.isNull(r.getName())?"":r.getName());
map.put("link", StringUtil.isNull(link)?"":link);
map.put("email", StringUtil.isNull(email)?"":email);
map.put("gander", r.getGenderName());
map.put("birthday", StringUtil.isNull(r.getBirthday())?"":r.getBirthday());
map.put("degree", StringUtil.isNull(r.getDegreeName())?"":r.getDegreeName());
map.put("graduateSchool", StringUtil.isNull(r.getSchool())?"":r.getSchool());
map.put("speciality", StringUtil.isNull(r.getSpeciality())?"":r.getSpeciality());
map.put("living", StringUtil.isNull(r.getLivingPlaceName())?"":r.getLivingPlaceName());
String registerPlace = r.getRegisteredPlaceName(); //户口所在地
map.put("place", StringUtil.isNull(registerPlace)?"":registerPlace);
map.put("graduate",StringUtil.isNull(r.getGraduateDate())?"":r.getGraduateDate());
map.put("workDate",StringUtil.isNull(r.getWorkDate())?"":r.getWorkDate());
map.put("height",r.getHeight() == 0?"":r.getHeight()); //身高
map.put("marry",SiteConstant.getMarriageName(r.getIsMarried())); //是否结婚
String nation = SiteDictUtil.getNationName(r.getNation());//民族
map.put("nation",StringUtil.isNull(nation)?"":nation);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
RTFCreator.createByTemplate(new File(Constant.getRTFFile()),map,bos);
byte[] resumeByte = bos.toByteArray();
RTFCreator.writeToWord(resumeByte,path);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 该类读取RTF模版文件,通过替换其中变量的方式生成新的文件
*/
public class RTFCreator {
public RTFCreator() {
}
public static String toRTFString(String tmp) throws Exception {
byte a[] = tmp.getBytes("GBK");
String rr = "";
for (int i = 0; i < a.length; i++) {
String t2 = (Integer.toHexString(a[i] & 0xff));
if (a[i] < 16 && t2.length() == 1)
t2 = "0" + t2;
rr += "\\'" + t2;
}
return rr;
}
public static void createByTemplate(File fileTemplate,
HashMap<String, Object> map, OutputStream fos) throws Exception {
FileInputStream fileInputStream = new FileInputStream(fileTemplate);
createByTemplate(fileInputStream, map, fos);
}
/**
* 通过模版创建RTF文件<br>
* 模版中的变量写法说明:<br>
* 1、<$x$> x是变量,<$$>用于将变量括起来.<br>
* 2、<$for[y,i]$> <$y[i]$> <$z[i]$> <$for[i]$>
* y,z都是变量(一维数组),i是循环变量,<$$>用于将变量括起来.<br>
* 2、<$for[x,i,j]$> <$x[i,j]$> <$z[i,j]$> <$for[i,j]$>
* x,z都是变量(二维数组),i,j是循环变量,<$$>用于将变量括起来.<br>
*
* @param fileTemplate
* File
* @param map
* HashMap {key=变量名,value=变量名(循环输出时,可以是数组)}
* @param fos
* OutputStream
* @throws Exception
*/
public static void createByTemplate(InputStream fileInputStream,
HashMap<String, Object> map, OutputStream fos) throws Exception {
// 输出流
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
byte[] data = new byte[8096];
int iRead = -1;
while ((iRead = fileInputStream.read(data)) != -1) {
os.write(data, 0, iRead);
os.flush();
}
fileInputStream.close();
os.close();
// 替换其中的变量内容
replaceText(os.toByteArray(), fos, map, 0, 0);
} catch (Exception ex) {
if (fileInputStream != null)
fileInputStream.close();
os.close();
throw ex;
}
}
/**
* 替换文本
*
* @param all
* byte[]
* @param fos
* OutputStream 输出流
* @param map
* HashMap 映射{key,value}
* @param iIndex
* int
* @param jIndex
* int
* @throws Exception
*/
private static void replaceText(byte all[], OutputStream fos,
HashMap<String, Object> map, int iIndex, int jIndex)
throws Exception {
int iStart = 2;
int iEnd = -2;
boolean isPrint = true;
for (int i = 0; i < all.length; i++) {
if (i == all.length - 1) {
iStart = i + 3;
isPrint = true;
} else if (all[i] == '<' && all[i + 1] == '$') {
iStart = i + 2;
i = i++;
isPrint = true;
} else if (all[i] == '$' && all[i + 1] == '>') {
iEnd = i;
i++;
}
if (isPrint) {
fos.write(all, iEnd + 2, (iStart - 2) - (iEnd + 2));
isPrint = false;
}
if (iEnd > iStart) {
// 输出
byte aTmp[] = new byte[iEnd - iStart];
System.arraycopy(all, iStart, aTmp, 0, aTmp.length);
String tmp = new String(aTmp);
if (tmp.indexOf("for[") > -1) // 若是循环
{
String fieldName = tmp.substring(tmp.indexOf("[") + 1,
tmp.indexOf(","));
String varName = tmp.substring(tmp.indexOf(",") + 1,
tmp.length() - 1);
String endStr = "<$for[" + varName + "]$>";
while (true) {
byte tt[] = new byte[endStr.length()];
System.arraycopy(all, i, tt, 0, tt.length);
if (new String(tt).equals(endStr)) {
byte aForData[] = new byte[i - (iEnd + 2)];
System.arraycopy(all, (iEnd + 2), aForData, 0,
aForData.length);
// 递归
Object a[] = (Object[]) map.get(fieldName);
if (a != null) {
if (varName.split(",").length > 1)
for (int j = 0; j < ((String[]) a[iIndex]).length; j++)
replaceText(aForData, fos, map, iIndex,
j);
else
for (int j = 0; j < a.length; j++)
replaceText(aForData, fos, map, j, 0);
} else
replaceText(aForData, fos, map, 0, 0);
i = i + endStr.length();
break;
}
i++;
}
iEnd = i - 2;
iStart = i + 2;
} else // 若是一般变量
{
String fieldName = tmp;
if (fieldName.indexOf("[") > -1)
fieldName = fieldName.substring(0,
fieldName.indexOf("["));
Object obj = map.get(fieldName);
// int p = fieldName.indexOf(".");
/**
* if(p>-1) { EnBase cEnBase =
* (csls.common.base.EnBase)map.get
* (fieldName.substring(0,p)); obj =
* cEnBase.getValue(fieldName.substring(p+1)); }
**/
if (obj != null) {
if (tmp.indexOf("[") > -1) {
Object a[] = (Object[]) obj;
if (a[iIndex] instanceof String[]) {
String hex = RTFCreator
.toRTFString(String
.valueOf(((String[]) a[iIndex])[jIndex]));
fos.write(hex.getBytes());
} else {
String hex = RTFCreator.toRTFString(String
.valueOf(a[iIndex]));
fos.write(hex.getBytes());
}
} else {
String hex = RTFCreator.toRTFString(String
.valueOf(obj));
fos.write(hex.getBytes());
}
} else
fos.write(("<$" + tmp + "$>").getBytes());
iStart = iEnd + 2;
}
}
}
}
/**
* 写到文件
* @param b
* @param path
*/
@SuppressWarnings("unused")
public static void writeToWord(byte[] b,String path){
ByteArrayInputStream bais = new ByteArrayInputStream(b);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
try {
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
FileOutputStream ostream = new FileOutputStream(path);
poifs.writeFilesystem(ostream);
bais.close();
ostream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- java根据rtf模版导出word
- POI根据模版导出word
- 根据模版导出简单Word文档
- 根据word模版导出数据(2)
- WPF 根据Word模版导出数据到Word
- JAVA使用POI根据模版导出EXCEL
- java使用freemarker模版导出分页word
- 导出word使用模版
- 模版导出excel、word
- java根据ftl模板导出word文档
- FreeMarker+FTL模版导出word
- freemarker利用word模版导出word文档
- Java 写rtf(word) excel文件
- Java中根据模板导出数据到word的解决方案
- 【Apache POI】Java Web根据模板导出word文件
- java 根据模板,导出word并提供下载
- asp.net 根据模版下载word文档
- 根据excel模版导出数据到excel
- uva 301 - Transportation
- Silverlight不写实体类也能显示数据呵呵
- Perl 最简单配置文件
- 7每CPU页框高速缓存
- 程序员笔试题---国信证券
- java根据rtf模版导出word
- 043-OCPThe memory used by Streams will be allocated from the shared pool
- HttpServletResponse
- zendstudio文件编码修改问题
- 8slab分配器
- 返回顶部,最精简代码,带注释
- spring学习笔记(一)
- 9非连续内存区
- 读《神一样的产品经理》