PHP 简单运用phpword 固定模板填充内容

 public function Wordserver(){
        $session = yii::$app->session;
        //模板的路径,word的版本最好是docx,要不然可能会读取不了,根据自己的模板位置调整
        $filepath = \Yii::getAlias('@frontend/web/gagaword/');
        $path = $filepath."2212-打印头保险服务条款(用户保存版).docx";
        $type = ".docx";
        //生成word路径,根据自己的目录调整
        $fname = '2212-打印头保险服务条款(用户保存版)'.date('ymdHis',time()).rand(1000,9999).'.'.$type;//2212-打印头保险服务条款(用户保存版)2210271624249276.docx
        $session['gagawordname'] = $fname;//存起来  发邮件用
        $time = date('Ymd',time());
        $new_file = \Yii::getAlias('@frontend/web/wordfile/').$time."/";
        if(!file_exists($new_file))
        {
            //检查是否有该文件夹,如果没有就创建,并给予最高权限
            mkdir($new_file, 0777);
        }

        $filePath = $new_file.$fname;

        //声明一个模板对象、读取模板
        $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($path);

        //替换模板中的变量,对应word里的 ${test}
        $customer_name = "ddddd";//用户信息
        $service_name  = "222";//销售单位
        $model = "333";//整机型号
        $host_sn = "444";//整机SN
        $number = "555";//打印头批号
        $service_type = "666";//购买服务类型
        $service_content = "vvvvvv";//购买服务内容
//        $test ="这是替换的内容";
//        $templateProcessor->setValue('test',$customer_name);//传真
        $templateProcessor->setValue('customer_name',$customer_name);
        $templateProcessor->setValue('service_name',$service_name);
        $templateProcessor->setValue('model',$model);
        $templateProcessor->setValue('host_sn',$host_sn);
        $templateProcessor->setValue('number',$number);
        $templateProcessor->setValue('service_type',$service_type);
        $templateProcessor->setValue('service_content',$service_content);

        //生成新的word
        $templateProcessor->saveAs($filePath);

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当根据Word模板填充内容生成Word文档时,可以使用Apache POI的`XWPFDocument`类提供的方法来处理空值。以下是一个示例代码: ```java import org.apache.poi.xwpf.usermodel.*; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class WordTemplateFiller { public static void main(String[] args) { try { // 加载Word模板文件 FileInputStream templateStream = new FileInputStream("template.docx"); XWPFDocument document = new XWPFDocument(templateStream); // 填充内容 Map<String, String> placeholders = new HashMap<>(); placeholders.put("name", "John Doe"); placeholders.put("age", "30"); placeholders.put("address", ""); replacePlaceholders(document, placeholders); // 保存填充后的文档 FileOutputStream outputStream = new FileOutputStream("filled_template.docx"); document.write(outputStream); outputStream.close(); System.out.println("Word文档生成成功!"); } catch (IOException e) { e.printStackTrace(); } } private static void replacePlaceholders(XWPFDocument document, Map<String, String> placeholders) { for (XWPFParagraph paragraph : document.getParagraphs()) { for (XWPFRun run : paragraph.getRuns()) { String text = run.getText(0); if (text != null) { for (Map.Entry<String, String> entry : placeholders.entrySet()) { String placeholder = "${" + entry.getKey() + "}"; if (text.contains(placeholder)) { text = text.replace(placeholder, entry.getValue()); run.setText(text, 0); } } } } } } } ``` 在上述代码中,我们首先加载Word模板文件,然后定义了一个`placeholders`的映射,其中包含了要替换的占位符和对应的值。接下来,我们调用`replacePlaceholders`方法来替换文档中的占位符。 在`replacePlaceholders`方法中,我们遍历文档中的每个段落和文本运行,通过检查文本内容中是否包含占位符来确定是否需要替换。如果找到了匹配的占位符,则将其替换为对应的值。 请注意,上述代码中使用的占位符格式为`${placeholder}`,你可以根据实际情况修改为其他格式。 对于空值的处理,可以在`placeholders`映射中将空字符串或其他默认值设置为相应的占位符值,这样在填充文档时,空值的占位符会被替换为默认值。 以上是一个基本示例,你可以根据自己的需求进行修改和扩展。同时,需要确保在项目中添加了Apache POI的依赖库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值