controller
@RequestMapping(value=”wirteMessageMonthReport”, produces=”text/html;charset=UTF-8”)
public void wirteMessageMonthReport(HttpServletRequest request,HttpServletResponse response) throws Exception{
response.setCharacterEncoding(“UTF-8”);
/*
* 报送日期
*/
String year = request.getParameter(“year”);
String month = request.getParameter(“month”);
String type = request.getParameter(“report_Type”)==null?”“:request.getParameter(“report_Type”).toString();
String state = request.getParameter(“state”)==null?”“:request.getParameter(“state”).toString();
if(year==null ||year.length()==0) {
year = Calendar.getInstance().get(Calendar.YEAR)+”“;
month = (Calendar.getInstance().get(Calendar.MONTH)+1)+”“;
if(month.length()==1) {
month=”0”+month;
}
}
if(“search”.equals(type)||type.length()==0) {
reportService.wirteMessageMonthReprot2Html(year,month,response.getWriter());
}else {
if(state.equals(“first”)) {
OutputStream out=response.getOutputStream();
String filename=year+”年”+month+”月份网源协调监管信息月报_初稿.docx”;
response.setContentType(“application/x-download”);
response.addHeader(“Content-Disposition”, “attachment;filename=”+ URLEncoder.encode(filename,”utf-8”) );
reportService.wirteMonth2Word(year, month, out);
}else {
OutputStream out=response.getOutputStream();
String filename=year+”年”+month+”月份网源协调监管信息月报_审核稿.docx”;
response.setContentType(“application/x-download”);
response.addHeader(“Content-Disposition”, “attachment;filename=”+ URLEncoder.encode(filename,”utf-8”) );
reportService.wirteMonth2FinalWord(year, month, out);
}
}
}
#service
private String reportFinalWord = "MessageMonthReprotFinal.docx";
import org.docx4j.wml.Tbl;
public void wirteMonth2FinalWord(String year1,String month1, OutputStream out) throws Exception{
try{
DocxFileModelManage manage = new DocxFileModelManage();
final String yearmonth = year1+month1;//统计月份使用
final String year = year1;
final String month = month1;
final String dccId = RangeParams.S_CODE;
//动态参数结束
InputStream stream = this.getClass().getResourceAsStream(reportFinalWord);
manage.setHandtxt(
new AbstractDocTxtParamHander() {
//@Override
public void handerTxt(Map map) {
map.put("province_name", RangeParams.S_NAME);
map.put("ybtyrq_year", year);
map.put("ybtyrq_month", month);
map.put("year", cyear);
map.put("month", cmonth);
map.put("day", cday);
}
}
}
manage.setHandtbl(
new AbstractDocTableParamHander() {
@Override
public Tbl handerTbl(Tbl tbl, int tblno) {
switch(tblno){
case 1:
handtabl1(tbl);
break;
case 2:
//handtabl2(tbl);
break;
case 3:
//handtabl3(tbl);
break;
case 4:
//handtabl4(tbl);
break;
}
return super.handerTbl(tbl, tblno);
}
/**
* 插入附表2
* @param tbl
*/
private void handtabl1(Tbl tbl){
//附表1 当前年度月报上报情况
SgMessageReportFinalFj2Example finalFj2Example = new SgMessageReportFinalFj2Example();
finalFj2Example.createCriteria().andAttr1EqualTo(year).andAttr2EqualTo(month);
List<SgMessageReportFinalFj2> lst = sqlSession.selectList(xmlFJ2+".selectByExample",finalFj2Example);
if(lst.size()>0) {
int i=1;
for(SgMessageReportFinalFj2 finalFj:lst) {
int rowsLen = lst.size();//行数
int colLen = 7;//16列
String[] rowv = new String[colLen];
rowv[0] = ""+i;
rowv[1] = finalFj.getDw();
rowv[2] = finalFj.getJz();//第三列
rowv[3] = finalFj.getYcjclqk();//第四列
rowv[4] = finalFj.getStrFxrq();//第五列
rowv[5] = finalFj.getStrXcrq();//第六列
rowv[6] = finalFj.getBz();
if(i==1) {
super.setRowValue(tbl, 1, rowv);
}else {
super.insertRowValue(tbl, 1, rowv);
}
i++;
}
}
}
DocxFileModelSetinfo setinfo = new DocxFileModelSetinfo();
manage.handerDocxFile(setinfo,stream,out);
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
out.close();
}catch(Exception e){
}
}
}
生成html
public void wirteMessageMonthReprot2Html(String year,String month, PrintWriter out) throws Exception {
try{
String readFile= readFile(reportHtml);
List<String> keyList = new ArrayList<String>();//html模板需要替换的变量值列表
keyList.add("{province_name}");//省份名称
Map<String,Object> map = new HashMap<String,Object>();
String provinceName = URLDecoder.decode(RangeParams.S_NAME, "utf-8");
map.put("{province_name}", provinceName);
for (String key : keyList) {
readFile = readFile.replace(key, map.get(key)==null?"":map.get(key).toString());
}
out.write(readFile);
out.flush();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/**
* 读取文件内容
* @param fileName
* @return
* @throws UnsupportedEncodingException
*/
public String readFile(String fileName) throws UnsupportedEncodingException{
InputStream input = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
input =this.getClass().getResourceAsStream(fileName);
byte[] bytes = new byte[1024];
for(int n ; (n = input.read(bytes))>0 ; ){
out.write(bytes);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(input !=null){
input.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return new String(out.toByteArray(),"utf-8");
}