/**
*
准备导出模板
*
@author
HuangHua
*
@param
templateFile
模板文件
*
@return
*
@throws
Exception
*/
public
Template readyTemplate(String templateFile)
throws
Exception{
Configuration configuration =
new
Configuration
()
;
configuration.setDefaultEncoding(
"utf-8"
);
//
注意这里要设置编码
//
加载模板文件,放在
/WebContent/file
下
configuration.setClassForTemplateLoading(
this
.getClass(),
"/com/kedacom/pms/team/file"
);
Template temp =
null
;
try
{
//
文件名
,
还有这里要设置编码
temp = configuration.getTemplate(templateFile,
"utf-8"
);
return
temp;
}
catch
(Exception e) {
throw
new
Exception(e);
}
}
|
/**
*
导出体能训练文件
*
@author
HuangHua
*
@param
fitnesstest
体能训练
*
@param
scoreList
科目成绩列表
*
@param
templateFile
模板文件
*
@param
targetFile
输出文件
*
@return
*
@throws
Exception
*/
public
File exportFitnesstestWord(Fitnesstest fitnesstest, List
//
第一步:准备导出模板
Template temp =
this
.readyTemplate(templateFile);
//
第二步:读取数据
if
(fitnesstest !=
null
) {
Map
。。。。。。
//
第三步:写文件
File file = DocUtil.
exportDoc
(temp, dataMap, targetFile);
return
file;
}
else
{
return
null
;
}
}
|
/**
*
根据模板导出
doc
文档
*
@author
HuangHua
*
@param
temp
导出模板
*
@param
dataMap
数据内容
*
@param
targetFile
输出文件
*
@return
*/
public
static
File exportDoc(Template temp, Map
File outFile =
null
;
Writer out =
null
;
try
{
outFile = File.
createTempFile
(targetFile,
".doc"
);
out =
new
BufferedWriter(
new
OutputStreamWriter(
new
FileOutputStream(outFile),
"utf-8"
));
}
catch
(Exception e1) {
e1.printStackTrace();
}
try
{
temp.process(dataMap, out);
}
catch
(Exception e) {
e.printStackTrace();
}
try
{
out.
flush
();
out.
close
();
}
catch
(
IOException
e) {
e.printStackTrace();
}
return
outFile;
}
|
/**
*
导出体能训练成绩信息
*
@author
HuangHua
*
@return
*/
public
void
exportFitnesstest(){
try
{
HttpServletResponse response = ServletActionContext.
getResponse
();
String targetFile =
"
体能达标成绩表
"
;
File sourceFile =
fitnesstestService
.exportFitnesstestFile(
fitnesstestId
,targetFile);
this
.responseFile(response, sourceFile, targetFile);
}
catch
(Exception e) {
log
.error(
"exportLawLevel failed"
, e);
}
}
/**
*
导出文件响应
*
@author
HuangHua
*
@param
response
*
@param
sourceFile
源文件
*
@param
targetFile
输出文件
*/
private
void
responseFile(HttpServletResponse response,File sourceFile,String targetFile){
InputStream fis =
null
;
OutputStream toClient =
null
;
try
{
fis =
new
BufferedInputStream(
new
FileInputStream(sourceFile));
byte
[] buffer =
new
byte
[fis.available()];
fis.read(buffer);
fis.close();
//
清空
response
response.reset();
//
设置
response
的
Header,
这里要用
URLEncoder
转下才能正确显示中文名称
targetFile = URLEncoder.
encode
(targetFile,
"utf-8"
);
response.addHeader(
"Content-Disposition"
,
"attachment;filename="
+ targetFile+
".doc"
);
response.addHeader(
"Content-Length"
,
""
+ sourceFile.length());
toClient =
new
BufferedOutputStream(response.getOutputStream());
response.setContentType(
"application/msword"
);
toClient.write(buffer);
toClient.flush();
}
catch
(Exception e) {
e.printStackTrace();
}
finally
{
try
{
if
(fis!=
null
){
fis.close();
}
if
(toClient!=
null
){
toClient.close();
}
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
|