zoukankan      html  css  js  c++  java
  • Java POI Word 写文档

    一个使用Apache POI写word文档的实例:

    package apache.poi;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.apache.poi.hwpf.HWPFDocument;
    import org.apache.poi.hwpf.usermodel.Range;
    import org.apache.poi.poifs.filesystem.DirectoryEntry;
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;
    
    public class ExportDocTest {
    
        public static void main(String[] args) {
            String destFile = "D:\11.doc";
            // #####################根据自定义内容导出Word文档#################################################
            StringBuffer fileCon = new StringBuffer();
            fileCon
                    .append("               张大炮            男              317258963215223
    "
                            + "2011     09        2013     07       3
    "
                            + "    二炮研究              成人
    "
                            + "2013000001                             2013     07     08");
            fileCon
                    .append("
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    ");
    
            new ExportDocTest().exportDoc(destFile, fileCon.toString());
    
            // ##################根据Word模板导出单个Word文档###################################################
            Map<String, String> map = new HashMap<String, String>();
    
            map.put("name", "Zues");
            map.put("sex", "男");
            map.put("idCard", "200010");
            map.put("year1", "2000");
            map.put("month1", "07");
            map.put("year2", "2008");
            map.put("month2", "07");
            map.put("gap", "2");
            map.put("zhuanye", "计算机科学与技术");
            map.put("type", "研究生");
            map.put("bianhao", "2011020301");
            map.put("nowy", "2011");
            map.put("nowm", "01");
            map.put("nowd", "20220301");
            // 注意biyezheng_moban.doc文档位置,此例中为应用根目录
            HWPFDocument document = new ExportDocTest().replaceDoc(
                    "biyezheng_moban.doc", map);
            ByteArrayOutputStream ostream = new ByteArrayOutputStream();
            try {
                document.write(ostream);
                // 输出word文件
                OutputStream outs = new FileOutputStream(destFile);
                outs.write(ostream.toByteArray());
                outs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    
        /**
         * 
         * @param destFile
         * @param fileCon
         */
        public void exportDoc(String destFile, String fileCon) {
            try {
                // doc content
                ByteArrayInputStream bais = new ByteArrayInputStream(fileCon
                        .getBytes());
                POIFSFileSystem fs = new POIFSFileSystem();
                DirectoryEntry directory = fs.getRoot();
                directory.createDocument("WordDocument", bais);
                FileOutputStream ostream = new FileOutputStream(destFile);
                fs.writeFilesystem(ostream);
                bais.close();
                ostream.close();
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 读取word模板并替换变量
         * 
         * @param srcPath
         * @param map
         * @return
         */
        public HWPFDocument replaceDoc(String srcPath, Map<String, String> map) {
            try {
                // 读取word模板
                FileInputStream fis = new FileInputStream(new File(srcPath));
                HWPFDocument doc = new HWPFDocument(fis);
                // 读取word文本内容
                Range bodyRange = doc.getRange();
                // 替换文本内容
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    bodyRange.replaceText("${" + entry.getKey() + "}", entry
                            .getValue());
                }
                return doc;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
    }

    例子中用到的附件(点击下载)

    本文转自:http://www.cnblogs.com/shihujiang/archive/2012/04/11/2442013.html

  • 相关阅读:
    SQL Server DB 基于多核CPU的设置
    如何在64位的Windows 2008的系统配置导入Excel功能
    如果在IIS中没有将虚拟目录配置为应用程序,则可能导致此错误
    .NET 数据绑定中空格符的问题
    .net 读取客户端文件的方法
    spring4+quartz
    web端 图片上传
    Maven配置setting.xml详细说明
    Timer
    实现quartz定时器及quartz定时器原理介绍
  • 原文地址:https://www.cnblogs.com/dreammyle/p/4606856.html
Copyright © 2011-2022 走看看