首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
您找到你想要的搜索结果了吗?
是的
没有找到

.NET Core使用NPOI导出复杂Word详解

最近使用NPOI做了个导出Word文档的功能,关于使用.NET Core 导出Word文档的方式有很多。最终我为什么选择了NPOI来实现了这个功能,首先是NPOI是一个开源,免费且容易上手的第三方框架(并且现在已支持.NET Core,GitHub源码地址:https://github.com/tonyqus/npoi)。因为之前使用NPOI导出Execl比较多,这次第一次使用NPOI 来导出Word文档还真没有什么头绪。首先看了下GItHub中的源码有一个简单Word导出的示例,然后在看了网上有很多关于NPOI导出Word文档的案例,发现一个特点网上的好像都差不多,对于我而言网上的这些案例完全能够实现我的这个功能,但是感觉看了网上这些案例对NPOI实例化段落,表格和设置相关样式不太清楚(可能是因为自己笨),并且假如使用网上的方法来实现我的功能的话代码量会比较大,而且感觉代码非常的冗余(我是一个追求代码简洁的人,怎么能够容忍这样的事情发生呢!),因此通过查阅了一些资料和自己的理解,把关于使用NPOI导出Word时所要涉及的一些段落,表格样式做了相关注释,和把段落和表格的创建实例,设置文字、字体、对齐方式都封装了起了(为了少写代码),文章末尾会附上一个完整的案例下载地址。

03

word导出03

public static void exportWordImg(String wordpath,List<String> contentlist,String[] imglist) throws Exception{         exportQuestionWord t=new exportQuestionWord();         WordprocessingMLPackage wordMLPackage = t.createWordprocessingMLPackage();         MainDocumentPart mp = wordMLPackage.getMainDocumentPart();         ObjectFactory factory = Context.getWmlObjectFactory();         //图片页眉         //Relationship relationship =t.createHeaderPart(wordMLPackage, mp, factory);         //文字页眉         //Relationship relationship =t.createTextHeaderPart(wordMLPackage, mp, factory, "页眉", JcEnumeration.CENTER);         //t.createHeaderReference(wordMLPackage, mp, factory, relationship);         t.addParagraphTest(wordMLPackage, mp, factory,contentlist,imglist);         //t.addPageBreak(wordMLPackage, factory);         //t.createNormalTableTest(wordMLPackage, mp, factory);         //页脚         //relationship =t.createFooterPageNumPart(wordMLPackage, mp, factory);         //t.createFooterReference(wordMLPackage, mp, factory, relationship);         t.saveWordPackage(wordMLPackage, new File(wordpath));     }     public void addParagraphTest(WordprocessingMLPackage wordMLPackage,                                  MainDocumentPart t, ObjectFactory factory,List<String> contentlist,String[] imglist) throws Exception {         RPr titleRPr = getRPr(factory, "黑体", "000000", "30", STHint.EAST_ASIA,                 true, false, false, false);         RPr boldRPr = getRPr(factory, "宋体", "000000", "24", STHint.EAST_ASIA,                 true, false, false, false);         RPr fontRPr = getRPr(factory, "宋体", "000000", "22", STHint.EAST_ASIA,                 false, false, false, false);         P paragraph=factory.createP();         Text txt = null;         R run=null;         File file=null;         InputStream is=null;         if(contentlist!=null || contentlist.size()>0){         for (int i = 0; i < contentlist.size(); i++) {             if(contentlist.get(i).contains("22.发生肺水肿时的应急处理错误的是")){

01

word转出图片(使用免费插件)02

/**      * 将word文档, 转换成pdf, 中间替换掉变量      * @param source 源为word文档, 必须为docx文档      * @param target 目标输出      * @param params 需要替换的变量      * @throws Exception      */     public static void wordConverterToPdf(InputStream source,                                           OutputStream target, Map<String, String> params) throws Exception {         wordConverterToPdf(source, target, null, params);     }     /**      * 将word文档, 转换成pdf, 中间替换掉变量      * @param source 源为word文档, 必须为docx文档      * @param target 目标输出      * @param params 需要替换的变量      * @param options PdfOptions.create().fontEncoding( "windows-1250" ) 或者其他      * @throws Exception      */     public static void wordConverterToPdf(InputStream source, OutputStream target,                                           PdfOptions options,                                           Map<String, String> params) throws Exception {         //HWPFDocument doc=new HWPFDocument(source);         XWPFDocument doc = new XWPFDocument(source);         paragraphReplace(doc.getParagraphs(), params);         for (XWPFTable table : doc.getTables()) {             for (XWPFTableRow row : table.getRows()) {                 for (XWPFTableCell cell : row.getTableCells()) {                     paragraphReplace(cell.getParagraphs(), params);                 }             }         }         PdfConverter.getInstance().convert(doc, target, options);     }     /** 替换段落中内容 */     private static void paragraphReplace(List<XWPFParagraph> paragraphs, Map<String, String> params) {         if (MapUtils.isNotEmpty(params)) {             for (XWPFParagraph p : paragraphs){                 for (XWPFRun r : p.getRuns()){                     String content = r.getText(r.getTextPosition());                     if(StringUtils.isNotEmpty(content) && params.containsKey(content)) {                         r.setText(params.get(content), 0);                     }                 }             }         }     }

01
领券