导出 doc
阅读原文时间:2023年07月11日阅读:1

/**
* 导出word
* @return
* @throws Exception
*/
@JCall
public String word() throws Exception{
String data=WebUtil.getParam("submitData");
// {"nf":"2005","jd":"第四季度:10月-12月","yf":"","zdy_ks":"","zdy_js":""}

    // 数据处理  
    List<Map<String, Object>> sj = sjcl(data);

    //文件名  
    String fileName = DateUtil.format("yyyyMMddHHmmssSSS", new Date())+ ".doc";  
    fileName = URLEncoder.encode(fileName, "utf-8");

    CustomXWPFDocument doc = mortgage(sj,data); // (用于处理.docx文件的高(ish)级别类) 自定义 XWPFDocument,并重写 createPicture()方法 

    try{//输出文件

        PropertiesUtil propertiesUtil=new PropertiesUtil("/db.properties");  
        // 作用是使客户端浏览器,区分不同种类的数据,并根据不同的MIME调用浏览器内不同的程序嵌入模块来处理相应的数据。  
        FileOutputStream os = new FileOutputStream(new File(System.getProperty("catalina.home")+"/webapps/"+this.getRequest().getContextPath()+"/doc/"+fileName));  
        // 获得字节流,通过该字节流的write(byte\[\] bytes)可以向response缓冲区中写入字节,再由Tomcat服务器将字节内容组成Http响应返回给浏览器。  
        // OutputStream os = getResponse().getOutputStream();  
        doc.write(os);  
        //os.flush();  
        os.close();  
        return fileName;  
    } catch (Exception e) {  
        e.printStackTrace();  
        // 没有数据 返回什么到前台呢  
        return "false";  
    }  
}

// 数据处理  
private List<Map<String, Object>> sjcl(String data) throws Exception {  
    JSONObject Jdata =JSONObject.fromObject(data);  
    String nf = Jdata.get("nf").toString();  
    String jd = Jdata.get("jd").toString();  
    String yf = Jdata.get("yf").toString();  
    String zdy\_ks = Jdata.get("zdy\_ks").toString();  
    String zdy\_js = Jdata.get("zdy\_js").toString();  
    // {"nf":"2005","jd":"第四季度:10月-12月","yf":"","zdy\_ks":"","zdy\_js":""}

    String sql = " SELECT T.TASK\_ID,T.TASK\_NAME,T.XTYH\_ID,T.TASK\_DATE,T.TASK\_TEXT, T.BY2,t.by3,T.CREATE\_USER,T.CREATE\_DATE,T.INSERT\_PERSON,T.INSERT\_TIME,T.DATA\_FLAG,T.XMLX,T.SFGK,T.TASK\_NAME\_ID "  
                +" FROM FSXC3.T\_FSXC\_TASK T "  
                +" WHERE T.DATA\_FLAG <> 'D' "  
                +" AND T.XTYH\_ID='"+SysUtil.getUserId()+"' ";  
    // dateTime=dateTime+" 00:00:00";  
    // String sql =  " select \* from t\_fsxc\_task t where t.XTYH\_ID='"+SysUtil.getUserId()+"' and t.TASK\_DATE like to\_date('"+dateTime+"','yyyy-MM-dd')  and t.MODIFY\_FLAG<>'D'  ";  
    // date>='2010-01-01' and date<='2010-10-1'  
    if(!nf.equals("") && jd .equals("") && yf.equals("")){ // 年份不等于空,季度等于空,月份等于空  >>年度  
        // nf+="-01-01";  
        sql += " AND T.TASK\_DATE like '%"+nf+"%' "; // 模糊查询

    }else if(!jd.equals("")){ // 季度(问?第一:只选了季度;第二:季度加年份)  
        String jdks = "";  
        String jdjs = "";  
        if(jd.equals("第一季度:1月-3月")){  
            jdks += "-01-01";  
            jdjs += "-03-31";  
        }else if(jd.equals("第二季度:4月-6月")){  
            jdks += "-04-01";  
            jdjs += "-06-30";  
        }else if(jd.equals("第三季度:7月-9月")){  
            jdks += "-07-01";  
            jdjs += "-09-30";  
        }else if(jd.equals("第四季度:10月-12月")){  
            jdks += "-10-01";  
            jdjs += "-12-31";  
        }  
        if(!nf.equals("")){  
            sql += " AND T.TASK\_DATE >= '"+nf+jdks+"' AND T.TASK\_DATE <= '"+nf+jdjs+"' ";  
        }else{  
            // 获得当前年份 加上季度时间  
            Calendar date = Calendar.getInstance();  
            String year = String.valueOf(date.get(Calendar.YEAR));

            sql +=" AND T.TASK\_DATE >= '"+year+jdks+"' AND T.TASK\_DATE <= '"+year+jdjs+"' ";  
        }  
    }else if(!yf.equals("")){ // 月份  
        // 获得当前年份 加上季度时间  
        Calendar date = Calendar.getInstance();  
        int year = date.get(Calendar.YEAR); // 年份

        String Iyf = yf.substring(0,1); // 截取String中数字部分的月份  
        int month = Integer.parseInt(Iyf); // 如果有0就只有数字

        String yfx ="-"+yf+"-01"; 

        /////////////////////////////////////////////  
        // 获得某年某月的最后一天  
        Calendar cal = Calendar.getInstance();  
            //设置年份  
        cal.set(Calendar.YEAR,year);  
            //设置月份  
        cal.set(Calendar.MONTH, month-1);  
            //获取某月最大天数  
        int lastDay = cal.getActualMaximum(Calendar.DAY\_OF\_MONTH);  
        ///////////////////////////////////////

        String yfd ="-0"+Iyf+lastDay; // 没有31号的月份会不会报错 // 获取这个月的最后一天

        if(!nf.equals("")){  
            sql += " AND T.TASK\_DATE >= '"+nf+yfx+"' AND T.TASK\_DATE <= '"+nf+yfd+"' ";  
        }else{  
            sql +=" AND T.TASK\_DATE >= '"+year+yfx+"' AND T.TASK\_DATE <= '"+year+yfd+"' ";  
        }  
    }

    if(!zdy\_ks.equals("")){  
        sql +=" AND T.TASK\_DATE >= '"+zdy\_ks+"' ";  
    }

    if(!zdy\_js.equals("")){  
        sql +=" AND T.TASK\_DATE <= '"+zdy\_js+"' ";  
    }

    // (List集合中的对象是一个Map对象,而这个Map对象的键是String类型,值是Object类型)  
    List<Map<String, Object>> sj = this.dBSelect(sql.toString()); // 调用查询数据库  
    return sj;  
}

// word  
private CustomXWPFDocument mortgage(List<Map<String, Object>> sj,String tj) throws Exception {  
    JSONObject jsonObject=JSONObject.fromObject(tj);  
    //{"nf":"2005","jd":"第四季度:10月-12月","yf":"","zdy\_ks":"","zdy\_js":""}  
    String bt="";  
    if(!jsonObject.getString("nf").equals("")){  
        bt=jsonObject.getString("nf");  
    }else if(!jsonObject.getString("jd").equals("")){  
        bt+=jsonObject.getString("jd")+"个人工作";  
    }else if(!jsonObject.getString("yf").equals("")){  
        bt+=jsonObject.getString("yf")+"个人工作";  
    }  
    if(!jsonObject.getString("zdy\_ks").equals("") && !jsonObject.getString("zdy\_js").equals("")){  
        bt=jsonObject.getString("zdy\_ks").replace("T00:00:00", "")+"到"+jsonObject.getString("zdy\_js").replace("T00:00:00", "")+"个人工作";  
    }

    CustomXWPFDocument document= new CustomXWPFDocument();

    //添加标题  
    XWPFParagraph titleParagraph = document.createParagraph();

    //设置段落居中  
    titleParagraph.setAlignment(ParagraphAlignment.CENTER);

    XWPFRun titleParagraphRun = titleParagraph.createRun();  
    titleParagraphRun.setBold(true);  
    titleParagraphRun.setText(bt);  
    titleParagraphRun.setFontFamily("方正小标宋简体");  
    titleParagraphRun.setColor("000000");  
    titleParagraphRun.setFontSize(22);

  //换行  
    XWPFParagraph paragraph1 = document.createParagraph();  
    XWPFRun paragraphRun1 = paragraph1.createRun();  
    paragraphRun1.setText("\\r");

    for(int i=0;i<sj.size();i++){  
        //段落  
        XWPFParagraph firstParagraph = document.createParagraph();  
        XWPFRun run = firstParagraph.createRun();  
        String ybgcm=(i+1)+"."+SysUtil.toStr(sj.get(i).get("task\_date"))+SysUtil.toStr(sj.get(i).get("task\_name"))+"-"+SysUtil.toStr(sj.get(i).get("by2"))+" ("+SysUtil.toStr(sj.get(i).get("by3"))+")";  
        run.setText(ybgcm);  
        run.setBold(true);  
        run.setColor("000000");  
        run.setFontSize(15);

      //段落  
        XWPFParagraph Paragraph2 = document.createParagraph();  
        XWPFRun run2 = Paragraph2.createRun();  
        run2.setBold(false);  
        run2.setText("工作内容:"+SysUtil.toStr(sj.get(i).get("task\_text")));  
        run2.setColor("000000");  
        run2.setFontSize(15);

      //段落  
        XWPFParagraph Paragraph3 = document.createParagraph();  
        XWPFRun run3 = Paragraph3.createRun();  
        run3.setBold(false);  
        run3.setText("附件:");  
        run3.setColor("000000");  
        run3.setFontSize(15);

        //获取附件  
        String sqlString ="select t.file\_info\_id,t.file\_extend,t.file\_info\_c3,t.file\_filename from T\_P\_FILE\_INFO t   where t.data\_flag <> 'D' and t.file\_index ='"+SysUtil.toStr(sj.get(i).get("task\_id"))+"'";  
        List<Map<String, Object>> fjList=dBSelect(sqlString);  
        for(int j=0;j<fjList.size();j++){  
            String imagePath =SysUtil.toStr(fjList.get(j).get("file\_info\_id"))+"."+SysUtil.toStr(fjList.get(j).get("file\_extend"));  
            String imgurl = System.getProperty("catalina.home")+"/webapps/webdav/"+imagePath;  
            int format;  
            if(imgurl.endsWith(".emf")) format = XWPFDocument.PICTURE\_TYPE\_EMF;  
            else if(imgurl.endsWith(".wmf")) format = XWPFDocument.PICTURE\_TYPE\_WMF;  
            else if(imgurl.endsWith(".pict")) format = XWPFDocument.PICTURE\_TYPE\_PICT;  
            else if(imgurl.endsWith(".jpeg") || imgurl.endsWith(".jpg")) format = XWPFDocument.PICTURE\_TYPE\_JPEG;  
            else if(imgurl.endsWith(".png")) format = XWPFDocument.PICTURE\_TYPE\_PNG;  
            else if(imgurl.endsWith(".dib")) format = XWPFDocument.PICTURE\_TYPE\_DIB;  
            else if(imgurl.endsWith(".gif")) format = XWPFDocument.PICTURE\_TYPE\_GIF;  
            else if(imgurl.endsWith(".tiff")) format = XWPFDocument.PICTURE\_TYPE\_TIFF;  
            else if(imgurl.endsWith(".eps")) format = XWPFDocument.PICTURE\_TYPE\_EPS;  
            else if(imgurl.endsWith(".bmp")) format = XWPFDocument.PICTURE\_TYPE\_BMP;  
            else if(imgurl.endsWith(".wpg")) format = XWPFDocument.PICTURE\_TYPE\_WPG;  
            else {  
                System.err.println("Unsupported picture: " + imgurl +  
                ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");

                //不是图片,直接输入超链接  
                appendExternalHyperlink(this.getRequest().getScheme()+"://"+this.getRequest().getServerName()+":"+this.getRequest().getServerPort()+"/webdav/"+imagePath,  
                        fjList.get(j).get("file\_filename").toString(), document.createParagraph());  
                continue;  
                }

            File dir = new File(imgurl);  
            if (dir.exists()) {  
                String picId = document.addPictureData(new FileInputStream(imgurl), format);  
                document.createPicture(picId, document.getNextPicNameNumber(format), 500, 400);  
             }  
        }

    }

    return document;  
}