当前位置: 首页 > news >正文

济南做设计公司网站做网站建设公司

济南做设计公司网站,做网站建设公司,泰安工作招聘,自己做网站怎么赢利需求:做个公文系统,需要将正文文档在某个节点点击套红按钮,实现文档套红 试了很多方法,大多数网上能查到但是实际代码不能找到关键方法,可能是跟包的版本有关系,下面记录能用的这个。 一:添加依…

需求:做个公文系统,需要将正文文档在某个节点点击套红按钮,实现文档套红
试了很多方法,大多数网上能查到但是实际代码不能找到关键方法,可能是跟包的版本有关系,下面记录能用的这个。

一:添加依赖

        <dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>4.1.2</version></dependency><dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.9.1</version><exclusions><exclusion><artifactId>slf4j-log4j12</artifactId><groupId>org.slf4j</groupId></exclusion></exclusions></dependency><dependency><groupId>e-iceblue</groupId><artifactId>spire.doc</artifactId><version>12.6.2</version></dependency>

二、文档

模板:{{date}}是可以获取到的变量
在这里插入图片描述
文档:
在这里插入图片描述

三、代码

public static void word2RedDocument(String content, Map<String, Object> data, String destDocx) throws Exception {//模板文件地址String model = "D:\\套红模板.docx";//模板文件 参数填写XWPFTemplate template = XWPFTemplate.compile(model).render(data);//获取模板文件  公文NiceXWPFDocument main = template.getXWPFDocument();//正文文档NiceXWPFDocument sub = new NiceXWPFDocument(new FileInputStream(content));List<XWPFParagraph> paragraphs = main.getParagraphs();NiceXWPFDocument newDoc = new NiceXWPFDocument();for (XWPFParagraph p:paragraphs) {if( null != p && p.getText().contains("正文")){//这里是要去掉正文两个字,自己debug看了索引,为了保险起见应该遍历run判断p.removeRun(0);XWPFRun run = p.createRun();// 合并两个文档到指定位置newDoc = main.merge(Arrays.asList(sub),run);break;}}// 设置页码--开始--没有需求可以删掉XWPFFooter footer = newDoc.createFooter(HeaderFooterType.DEFAULT);//创建一个新的XWPFFooter对象XWPFParagraph paragraph = footer.createParagraph();//创建新的XWPFParagraph对象paragraph.setAlignment(ParagraphAlignment.CENTER);//设置样式居中//设置段落对象XWPFRun runPre = paragraph.createRun();//新的段落对象runPre.setText("- ");XWPFRun run = paragraph.createRun();//新的段落对象CTFldChar fldChar = run.getCTR().addNewFldChar();//新的CTFldChar对象fldChar.setFldCharType(STFldCharType.Enum.forString("begin"));CTText ctText = run.getCTR().addNewInstrText();ctText.setStringValue("PAGE  \\* MERGEFORMAT");ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));fldChar = run.getCTR().addNewFldChar();fldChar.setFldCharType(STFldCharType.Enum.forString("end"));//设置段落对象XWPFRun runSuf = paragraph.createRun();//新的段落对象runSuf.setText(" -");// 将页脚添加到所有的页面XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(newDoc);headerFooterPolicy.createFooter(STHdrFtr.DEFAULT, new XWPFParagraph[]{paragraph});
// 设置页码--结束--没有需求可以删掉//可以是生成新文档,也可以生成到原来的正文content = "D:\\新文档.docx";// 生成新文档FileOutputStream out = new FileOutputStream(content);newDoc.write(out);newDoc.close();out.close();//doc转pdfdoc2Pdf(content);
//        ByteArrayOutputStream os = new ByteArrayOutputStream();
//        newDoc.write(os);
//        InputStream is = new ByteArrayInputStream(os.toByteArray());
//        os.close();}

调用方法测试

 public static void main(String[] args) throws Exception {String sourceFile = "D:\\模板.docx";String targetFile = "D:\\测试.docx";Map<String, Object> data = new HashMap<>(2);List<String> list = Arrays.asList("技术", "测试", "评选结果", "测试", "评选结果", "测试", "评选结果");StringBuilder builder = new StringBuilder();for (int i =0;i<list.size();i++) {builder.append(list.get(i));if (i != list.size() -1){builder.append("  ");}}data.put("num", "931");data.put("year", "2024");data.put("name", "销售中心销售中心销售中心销售中心销售中心销售中心销售中心销售中心销售中心销售中心销售中心销售中心销售中心销售中心销售中心销售中心销售中心");data.put("keyword",builder );
//        data.put("keyword", Arrays.asList("技术","测试","评选结果","测试","评选结果","测试","评选结果"));
//        data.put("keyword", Arrays.asList("技术","测试"));data.put("user", "李斯");
//        data.put("company", "股份有限公司技术股份有限公司");data.put("date", getChineseDate());word2RedDocument(targetFile,data,"新文档.docx");}

转换时间的方法

public static String getChineseDate() {Calendar cal = Calendar.getInstance();SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA);String date = sdf.format(cal.getTime());// 将数字转换为汉字String[] chineseNumbers = {"〇", "一", "二", "三", "四", "五", "六", "七", "八", "九"};StringBuilder chineseDate = new StringBuilder();for (int i = 0; i < date.length(); i++) {if(i==4 || i==7 || i==10){chineseDate.append(date.charAt(i));} else {int number = Character.getNumericValue(date.charAt(i));if(i==5 || i==8 ){if(number==0){continue;}}chineseDate.append(chineseNumbers[number]);}}return chineseDate.toString();}

执行方法:
在这里插入图片描述
结果文档如下:
在这里插入图片描述

XWPFDocument类相关:

在这里插入图片描述

http://www.ds6.com.cn/news/100977.html

相关文章:

  • 深圳市盐田区建设局网站网页制作软件dreamweaver
  • 招商网站建设关键词排名seo优化
  • 做网站不给源代码谷歌浏览器官网下载安装
  • 郑州做网站的联系方式百度网盘下载安装
  • 网站开发工程师基础公司页面设计
  • 专业商业空间设计公司南昌seo排名扣费
  • 网站项目书范文网站模板平台资源
  • 建个淘宝那样的网站需要多少钱友情链接怎么互换
  • 广西汽车网网站建设谷歌官方网站首页
  • 做移动网站优化海外网站
  • 网站做哪些主题比较容易做网址收录查询
  • 对于诈骗网站怎么做营销推广的公司
  • 长春哪里有做网站的如何被百度收录
  • 怎样建立自己购物网站百度网址大全官网
  • 网站开发环境有什么关键字
  • 颍上网站建设网店运营怎么学
  • 做网站或者app百度搜索排行seo
  • 做网站1万多杭州优化建筑设计
  • 长沙制作网站软件安卓在线视频嗅探app
  • net网站开发实例爱站网
  • 外贸需要网站做生产车间展示搜索引擎seo如何赚钱
  • 专门做澳大利亚项目的网站百度指数有三个功能模块
  • 国内最好的旅游网站seo赚钱培训
  • 潍坊做电商的网站淄博搜索引擎优化
  • 没有收款接口网站怎么做收款全网seo
  • 那些网站是做俄罗斯鞋子建网站费用
  • wordpress 标签数量seogw
  • 在58上做网站接的到货吗河南郑州最新消息
  • 石材外贸在哪个网站做2022最新新闻
  • wordpress制作分销网站百度指数工具