部分样例,留作以后参考:

ACTION:

public String exportProblemBill(){
        BufferedInputStream in = null;
        String relativePath;
        try {
            
             relativePath=URLDecoder.decode("download/problem/problemTemplate.docx",
                    "UTF-8");    
            in =getInputStrem(relativePath);
        System.out.println(relativePath+"22222222222");
            Map<String, String> properties = new HashMap<>();
            long id=from.getId();
            JtProblemInfo po=jtProblemService.getProblemInfoBywsdid(id);
            properties.put("#dept#", po.getCompany());
            properties.put("#problemcode#", po.getProblemCode());
            properties.put("#sltime#", po.getAcceptTime());
            properties.put("#approver#", po.getAskerName());
            properties.put("#tel#", po.getPhone());
            properties.put("#title#", po.getTitle());
            if(jtProblemService.getNamebycode(po.getSources(),"JTPROBLEM_INFO_SOURCE").equals(DOConstant.PROBLEM_SOUTCE_NAME1)){
                properties.put("#source1#", "■");
            }else{
                properties.put("#source1#", "□");
            }
            if(jtProblemService.getNamebycode(po.getSources(),"JTPROBLEM_INFO_SOURCE").equals(DOConstant.PROBLEM_SOUTCE_NAME2)){
                properties.put("#source2#", "■");
            }else{
                properties.put("#source2#", "□");
            }
            if(jtProblemService.getNamebycode(po.getSources(),"JTPROBLEM_INFO_SOURCE").equals(DOConstant.PROBLEM_SOUTCE_NAME3)){
                properties.put("#source3#", "■");
            }else{
                properties.put("#source3#", "□");
            }
            if(jtProblemService.getNamebycode(po.getSources(),"JTPROBLEM_INFO_SOURCE").equals(DOConstant.PROBLEM_SOUTCE_NAME4)){
                properties.put("#source4#", "■");
            }else{
                properties.put("#source4#", "□");
            }
            if(jtProblemService.getNamebycode(po.getSources(),"JTPROBLEM_INFO_SOURCE").equals(DOConstant.PROBLEM_SOUTCE_NAME5)){
                properties.put("#source5#", "■");
            }else{
                properties.put("#source5#", "□");
            }
            
            properties.put("#eventcode#", po.getIncidentCode());
            
            if(jtProblemService.getNamebycode(po.getProblemClass(),"JTPROBLEM_CLASS").equals(DOConstant.PROBLEM_CLASS_NAME1)){
                properties.put("#pclass1#", "■");
            }else{
                properties.put("#pclass1#", "□");
            }
            if(jtProblemService.getNamebycode(po.getProblemClass(),"JTPROBLEM_CLASS").equals(DOConstant.PROBLEM_CLASS_NAME2)){
                properties.put("#pclass2#", "■");
            }else{
                properties.put("#pclass2#", "□");
            }
            
            if(jtProblemService.getNamebycode(po.getProblemClass(),"JTPROBLEM_CLASS").equals(DOConstant.PROBLEM_CLASS_NAME3)){
                properties.put("#pclass3#", "■");
            }else{
                properties.put("#pclass3#", "□");
            }
            
            if(jtProblemService.getNamebycode(po.getProblemSubClass(),"JTPROBLEM_SUBCLASS").equals(DOConstant.PROBLEM_TYPE_NAME1)){
                properties.put("#ptype1#", "■");
            }else{
                properties.put("#ptype1#", "□");
            }
            
            if(jtProblemService.getNamebycode(po.getProblemSubClass(),"JTPROBLEM_SUBCLASS").equals(DOConstant.PROBLEM_TYPE_NAME2)){
                properties.put("#ptype2#", "■");
            }else{
                properties.put("#ptype2#", "□");
            }
            
            if(jtProblemService.getNamebycode(po.getProblemSubClass(),"JTPROBLEM_SUBCLASS").equals(DOConstant.PROBLEM_TYPE_NAME3)){
                properties.put("#ptype3#", "■");
            }else{
                properties.put("#ptype3#", "□");
            }
            
            if(jtProblemService.getNamebycode(po.getProblemSubClass(),"JTPROBLEM_SUBCLASS").equals(DOConstant.PROBLEM_TYPE_NAME4)){
                properties.put("#ptype4#", "■");
            }else{
                properties.put("#ptype4#", "□");
            }
            if(jtProblemService.getNamebycode(po.getProblemSubClass(),"JTPROBLEM_SUBCLASS").equals(DOConstant.PROBLEM_TYPE_NAME5)){
                properties.put("#ptype5#", "■");
            }else{
                properties.put("#ptype5#", "□");
            }
            if(jtProblemService.getNamebycode(po.getProblemSubClass(),"JTPROBLEM_SUBCLASS").equals(DOConstant.PROBLEM_TYPE_NAME6)){
                properties.put("#ptype6#", "■");
            }else{
                properties.put("#ptype6#", "□");
            }
            if(jtProblemService.getNamebycode(po.getProblemSubClass(),"JTPROBLEM_SUBCLASS").equals(DOConstant.PROBLEM_TYPE_NAME7)){
                properties.put("#ptype7#", "■");
            }else{
                properties.put("#ptype7#", "□");
            }
            if(jtProblemService.getNamebycode(po.getProblemSubClass(),"JTPROBLEM_SUBCLASS").equals(DOConstant.PROBLEM_TYPE_NAME8)){
                properties.put("#ptype8#", "■");
            }else{
                properties.put("#ptype8#", "□");
            }
            if(jtProblemService.getNamebycode(po.getProblemSubClass(),"JTPROBLEM_SUBCLASS").equals(DOConstant.PROBLEM_TYPE_NAME9)){
                properties.put("#ptype9#", "■");
            }else{
                properties.put("#ptype9#", "□");
            }
            
            if(jtProblemService.getNamebycode(po.getUrgency(),"JTPROBLEM_INFO_URGENCY").equals(DOConstant.PROBLEM__DEGREE_NAME1)){
                properties.put("#degree1#", "■");
            }else{
                properties.put("#degree1#", "□");
            }
            if(jtProblemService.getNamebycode(po.getUrgency(),"JTPROBLEM_INFO_URGENCY").equals(DOConstant.PROBLEM__DEGREE_NAME2)){
                properties.put("#degree2#", "■");
            }else{
                properties.put("#degree2#", "□");
            }
            if(jtProblemService.getNamebycode(po.getUrgency(),"JTPROBLEM_INFO_URGENCY").equals(DOConstant.PROBLEM__DEGREE_NAME3)){
                properties.put("#degree3#", "■");
            }else{
                properties.put("#degree3#", "□");
            }
            if(jtProblemService.getNamebycode(po.getUrgency(),"JTPROBLEM_INFO_URGENCY").equals(DOConstant.PROBLEM__DEGREE_NAME4)){
                properties.put("#degree4#", "■");
            }else{
                properties.put("#degree4#", "□");
            }
            
            if(jtProblemService.getNamebycode(po.getInfluencey(),"JTPROBLEM_INFO_INFLUENCEY").equals(DOConstant.PROBLEM_INFLUENCE_NAME1)){
                properties.put("#infleve1#", "■");
            }else{
                properties.put("#infleve1#", "□");
            }
            if(jtProblemService.getNamebycode(po.getInfluencey(),"JTPROBLEM_INFO_INFLUENCEY").equals(DOConstant.PROBLEM_INFLUENCE_NAME2)){
                properties.put("#infleve2#", "■");
            }else{
                properties.put("#infleve2#", "□");
            }
            if(jtProblemService.getNamebycode(po.getInfluencey(),"JTPROBLEM_INFO_INFLUENCEY").equals(DOConstant.PROBLEM_INFLUENCE_NAME3)){
                properties.put("#infleve3#", "■");
            }else{
                properties.put("#infleve3#", "□");
            }
            if(jtProblemService.getNamebycode(po.getInfluencey(),"JTPROBLEM_INFO_INFLUENCEY").equals(DOConstant.PROBLEM_INFLUENCE_NAME4)){
                properties.put("#infleve4#", "■");
            }else{
                properties.put("#infleve4#", "□");
            }
            if(jtProblemService.getNamebycode(po.getPriority(),"JTPROBLEM_INFO_PRIORITY").equals(DOConstant.PROBLEM_PORORITY_NAME1)){
                properties.put("#porority#", "■ 1□ 2□ 3□ 4□ 5");
            }
            if(jtProblemService.getNamebycode(po.getPriority(),"JTPROBLEM_INFO_PRIORITY").equals(DOConstant.PROBLEM_PORORITY_NAME2)){
                properties.put("#porority#", "□ 1■ 2□ 3□ 4□ 5");
            }
            if(jtProblemService.getNamebycode(po.getPriority(),"JTPROBLEM_INFO_PRIORITY").equals(DOConstant.PROBLEM_PORORITY_NAME3)){
                properties.put("#porority#", "□ 1□ 2■ 3□ 4□ 5");
            }
            if(jtProblemService.getNamebycode(po.getPriority(),"JTPROBLEM_INFO_PRIORITY").equals(DOConstant.PROBLEM_PORORITY_NAME4)){
                properties.put("#porority#", "□ 1□ 2□ 3■ 4□ 5");
            }
            if(jtProblemService.getNamebycode(po.getPriority(),"JTPROBLEM_INFO_PRIORITY").equals(DOConstant.PROBLEM_PORORITY_NAME5)){
                properties.put("#porority#", "□ 1□ 2□ 3□ 4■ 5");
            }
            properties.put("#descrption#", po.getDescription());
            List<JtProblemInfoDeal> list = jtProblemService.queryJtProblemByProblemId(id);
            properties.put("#delperson#", list.get(0).getHandleStaffName());
            properties.put("#responeTime#", list.get(0).getEndtime());
            properties.put("#analy#", list.get(0).getAnalysis());
            properties.put("#solution#", list.get(0).getSolution());
            properties.put("#fayz#", list.get(0).getVerification());
            properties.put("#delTime#", list.get(0).getSolveTime());
            
            properties.put("#ssps#", po.getReview());
            if(jtProblemService.getNamebycode(po.getCloseCode(),"PROBLEM_CLOSE_CODE").equals(DOConstant.PROBLEM_CLOSECODE_NAME1)){
                properties.put("#clcode1#", "■");
            }else{
                properties.put("#clcode1#", "□");
            }
            if(jtProblemService.getNamebycode(po.getCloseCode(),"PROBLEM_CLOSE_CODE").equals(DOConstant.PROBLEM_CLOSECODE_NAME2)){
                properties.put("#clcode2#", "■");
            }else{
                properties.put("#clcode2#", "□");
            }
            if(jtProblemService.getNamebycode(po.getCloseCode(),"PROBLEM_CLOSE_CODE").equals(DOConstant.PROBLEM_CLOSECODE_NAME3)){
                properties.put("#clcode3#", "■");
            }else{
                properties.put("#clcode3#", "□");
            }
            if(jtProblemService.getNamebycode(po.getCloseCode(),"PROBLEM_CLOSE_CODE").equals(DOConstant.PROBLEM_CLOSECODE_NAME4)){
                properties.put("#clcode4#", "■");
            }else{
                properties.put("#clcode4#", "□");
            }
            properties.put("#closeTime#", po.getCloseTime());
            properties.put("#ifknowledge#", po.getKnowledgeBase());
            properties.put("#changecode#", list.get(0).getChangeCode());
            properties.put("#zscode#", po.getKnowledgeCode());
            
            
            setDownloadFile(setOutStream(extractTemplate(in, properties)));
        } catch (UnCheckedException e) {
            result = MessageUtil.showMessageByCode(true, "");
            e.printStackTrace();
        } catch (Exception e) {
            result = MessageUtil.showMessageByCode(true, "");
            e.printStackTrace();
        }
        return "download";
    }
    
    
    /**
     * 获取输入流
     * @return
     * @throws FileNotFoundException
     */
    private BufferedInputStream getInputStrem(String filePath) throws FileNotFoundException{
        BufferedInputStream in = null;
        File f = new File(request.getSession().getServletContext().getRealPath(filePath));
        in = new BufferedInputStream(new FileInputStream(f));
        return in;
    }

/**
     * 提取模板
     *
     * @param stream
     * @param properties
     * @return XWPFDocument
     * @throws IOException
     */
    protected XWPFDocument extractTemplate(InputStream stream, Map<String, String> properties) throws IOException {
        XWPFDocument document = new XWPFDocument(stream);
        replaceParagraphs(document.getParagraphs(), properties);
        replaceTables(document.getTablesIterator(), properties);
        return document;
    }

/**
     * 替换段落
     *
     * @param paragraphs    段落
     * @param properties    要替换成的值
     */
    private void replaceParagraphs(List<XWPFParagraph> paragraphs, Map<String, String> properties) {
        for (XWPFParagraph paragraph : paragraphs) {
            List<XWPFRun> runs = paragraph.getRuns();
            StringBuffer buffer = new StringBuffer();
            // 同一个关键
            boolean isKey = false;
            for (XWPFRun run : runs) {
                String textRun = run.getText(run.getTextPosition());
                // 判断是否为关键字的开始符
                if (textRun != null && textRun.contains("#")) {
                    // 不是同一个关键字,则重新设置关键字
                    if (!isKey) {
                        buffer = new StringBuffer();
                        String[] values = textRun.split("#");
                        if (values.length > 1 && textRun.endsWith("#")) {
                            String key = "#" + values[1] + "#";
                            String val = properties.get(key);
                            // 替换从数据库查找出来的内容
                            run.setText(textRun.replace(key, (StringUtils.isEmpty(val) ? "" : val)), 0);
                            isKey = false;
                        } else {
                            int index = textRun.indexOf("#");
                            String val = textRun.substring(0, index);
                            buffer.append(textRun.substring(index));
                            isKey = true;
                            // 保留开始符前的内容
                            run.setText(val, 0);
                        }
                    }
                    // 同一个关键字,则追加关键字
                    else {
                        buffer.append("#");
                        String key = buffer.toString();
                        String val = properties.get(key);
                        // 替换从数据库查找出来的内容
                        run.setText(textRun.replace("#", (StringUtils.isEmpty(val) ? "" : val)), 0);
                        isKey = false;
                    }
                } else if (isKey) {
                    buffer.append(textRun);
                    run.setText("", 0);
                }
            }
        }

}
    
    
    /**
     * 替换表格
     *
     * @param itTable
     * @param properties
     */
    private void replaceTables(Iterator<XWPFTable> itTable, Map<String, String> properties) {
        while (itTable.hasNext()) {
            XWPFTable table = itTable.next();//拿出表格
            extractLines(properties, table);//拿出行
        }
    }
    
    /**
     * 提取行
     *
     * @param properties
     * @param table
     */
    private void extractLines(Map<String, String> properties, XWPFTable table) {
        int rcount = table.getNumberOfRows();//拿出行的总数
        for (int j = 0; j < rcount; j++) {
            XWPFTableRow row = table.getRow(j);//拿出每一行
            List<XWPFTableCell> cells = row.getTableCells();//拿出每一个表格
            for (XWPFTableCell cell : cells) {
                replaceParagraphs(cell.getParagraphs(), properties);//替换数据
            }
        }
    }
    
    /**
     * 获取输出流
     * @param document
     * @throws IOException
     */
    protected InputStream setOutStream(XWPFDocument document) throws IOException{
        ByteArrayOutputStream out = null;
        out = new ByteArrayOutputStream();
        document.write(out);
        byte[] bytes = out.toByteArray();
        return new ByteArrayInputStream(bytes);
    }
    
    /**
     * 获得下载的文件名
     *
     * @return
     * @throws UnsupportedEncodingException
     */
    public String getDownloadFileName() throws UnsupportedEncodingException {
        return URLEncoder.encode("问题单.doc", "UTF-8");
    }
}

Struts:

<action name="problemDownLoad" class="jtProblemAction" method="exportProblemBill">
            <result name="download" type="stream">
                   <param name="contentType">application/x-msword</param>
                    <param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
                    <param name="bufferSize">1024</param>
                    <param name="inputName">downloadFile</param>
              </result>
        </action>

往word表中写数据相关推荐

  1. delete表1条件是另一个表中的数据,多表连接删除(转)

    DELETE删除多表数据,怎样才能同时删除多个关联表的数据呢?这里做了深入的解释: 1. delete from t1 where 条件 2.delete t1 from t1 where 条件 3. ...

  2. 在Sql2005中,向表中插入数据时遇到uniqueidentifier列,如何插入数据?

    在Sql2005中,向表中插入数据时遇到uniqueidentifier列,如何插入数据? 原文 http://blog.csdn.net/quou2002/article/details/59650 ...

  3. mysql 目录武沛齐_MySQL数据表中的数据操作

    1.插入数据 insert into t_user (username,password,nickname) values ('foye','123','佛爷'); 以下方式必须写出所有的字段 ins ...

  4. hbase中为何不能向表中插入数据_Hbase快速入门(超精炼总结)

    基本概念: HBase是列簇式Key-Value存储系统,构建在HDFS之上的.支持随机插入和删除. 总结Hbase的架构核心,就两个字"有序" . 磁盘的读写,随机与顺序,相差3 ...

  5. delete表1条件是另一个表中的数据,多表连接删除

    2019独角兽企业重金招聘Python工程师标准>>> 数据库中有两张表. DELETE cdb_posts,cdb_threads FROM cdb_posts ,cdb_thre ...

  6. oracle的删除的row如何,Oracle 删除大表中部分数据

    需求: 项目中有一张表大概有7000多万条数据,造成表空间已满,需要清理部分数据,打算清理3000万. 2B 做法: delete from table_name where ID > '400 ...

  7. mysql5.7循环,python3.4用循环往mysql5.7中写数据并输出的实现方法

    python3.4用循环往mysql5.7中写数据并输出的实现方法 来源:中文源码网    浏览: 次    日期:2018年9月2日 python3.4用循环往mysql5.7中写数据并输出的实现方 ...

  8. word表格转换成html,delphi将word表格转换成html_取得Word 表格中的数据

    delphi将word表格转换成html_取得Word 表格中的数据 1.delphi将word表格转换成html procedure TForm1.Button1Click(Sender: TObj ...

  9. python3.7输出语句_python3.4用循环往mysql5.7中写数据并输出的实现方法

    如下所示: #!/usr/bin/env python # -*- coding:utf-8 -*- # __author__ = "blzhu" ""&quo ...

最新文章

  1. 会汇编语言再学c语言,只有C语言功底的我学习汇编语言可以吗?
  2. Innodb Buffer Pool的三种Page和链表
  3. eclipse-可视化swing设计插件windowbuilder
  4. 洛谷 [P1024]一元三次方程求解【二分答案】
  5. 干货 | 数据挖掘过关40题
  6. Mysql学习总结(8)——MySql基本查询、连接查询、子查询、正则表达查询讲解...
  7. xml格式怎么转换excel_pdf怎么转换成excel?这个转换技巧你值得拥有!
  8. 英国首相用华为P20 Pro自拍引热议,网友:真香,比炸鱼和薯条还香
  9. 调一下电脑底色,保护眼睛哦
  10. Swift - 使用导航条和导航条控制器来进行页面切换并传递数据
  11. 电脑显示已连接网络但是无internet访问
  12. 安装教程之postman下载及安装
  13. linux 进程死循环,排查程序死循环,死锁的方法 ——pstack
  14. Go语言中rune方法如何使用
  15. 宁夏大学计算机科学与技术排名,2016宁夏自治区大学各学科门类最佳专业排行榜...
  16. 网页设计期末结课作业 html+css+js房地产企业官网网站制作与实现
  17. android ipv6测试,搭建IPv6本地环境测试App
  18. 数论及其应用——同余式定理
  19. 5款好用的mysql客户端
  20. Chapter7 Cloud Infrastructure Mechanisms

热门文章

  1. C++生日聚会(BFS)
  2. nodejs常用的黑窗口命令
  3. IOS开发之——设置APP图标、名字、启动页(50)
  4. 【译】Vertical-Align: All You Need To Know
  5. r语言绘制精美pcoa图_「R」数据可视化5:PCA和PCoA图
  6. xp计算机用户名和密码忘记了怎么办,电脑的XP系统密码被不小心忘记了怎么办?...
  7. 护眼灯买什么样的好?这几款2022年最佳的护眼灯值得一看!
  8. 黑科技计算机玩游戏,别再嘲笑苹果电脑玩不了游戏!有了这项黑科技后比台式机还猛...
  9. JavaScript 学习笔记 - 12 JavaScript 应用示例
  10. 2023年兔年好看的头像