惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
Cisco Talos Blog
Cisco Talos Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
D
Docker
S
SegmentFault 最新的问题
博客园 - 聂微东
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
M
MIT News - Artificial intelligence
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The GitHub Blog
The GitHub Blog
GbyAI
GbyAI
L
LangChain Blog
Vercel News
Vercel News
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
Engineering at Meta
Engineering at Meta
T
Threat Research - Cisco Blogs
T
Threatpost
Scott Helme
Scott Helme
T
Tailwind CSS Blog
Latest news
Latest news
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
The Register - Security
The Register - Security
罗磊的独立博客
P
Proofpoint News Feed
腾讯CDC
S
Schneier on Security
雷峰网
雷峰网
A
About on SuperTechFans
T
Tenable Blog
F
Full Disclosure
Cyberwarzone
Cyberwarzone
博客园_首页
有赞技术团队
有赞技术团队
K
Kaspersky official blog

涂涂研版

关注IT技术和学习分享,提供ZBlogPHP主题模板插件定制! 关注IT技术和学习分享,提供ZBlogPHP主题模板插件定制! 关注IT技术和学习分享,提供ZBlogPHP主题模板插件定制! 关注IT技术和学习分享,提供ZBlogPHP主题模板插件定制! zblogphp调用所属分类的文章 zblog建表-标准建表 zblogphp评论名等记忆功能和asp一样 zblogasp 判断是否蜘蛛来源-百度 zblogphp:获取当天now时间戳的文章总数实例
关注IT技术和学习分享,提供ZBlogPHP主题模板插件定制!
2024-05-10 · via 涂涂研版

phpword php导出word wordpress实例,设置字体,导出代码等

雨夜 教程、积累

wordpress代码片段

function content_to_word(){
    global $wpdb;
    date_default_timezone_set('Asia/Shanghai');
    $last_post_id=_cao('last_post_id_setting')!==''?_cao('last_post_id_setting')-1:get_option('last_post_id');//       update_option('last_post_id',1);//  var_dump(get_option('last_post_id'));exit;
    $min_id=!empty($_POST['history_id'])?$_POST['history_id']:$last_post_id;
   
    $post_num=$_POST['num']?$_POST['num']:0;
    $posts_data = $wpdb->get_results("SELECT ID,post_title FROM $wpdb->posts  WHERE post_type= 'post' AND post_status = 'publish'  AND ID > ".$min_id."  order by ID asc ");
    
    $message='';
    $i=$_POST['i']?$_POST['i']:0;
   
    if($i==$post_num){
                    
                  $message=array('code'=>3,'message'=>'<div id="message" class="csf-notice csf-notice-success" style="margin-top:10px" ><p>本次生成任务已执行完,共生成'.$i.'篇文档</p></div>','history'=>$history_id);
                  
                  update_option( 'last_post_id', $_POST['history_id'] );
                  
                 echo json_encode($message);exit;
    }
    
        foreach ($posts_data  as $k=>$v){
           
            $history_id=$v->ID;
           
            //$post_content=$wpdb->get_var("SELECT post_content FROM $wpdb->posts  WHERE ID= '$v->ID'  ");
            
            $post_content = get_post( $v->ID )->post_content;
            
            $post_content=str_replace('[rihide]','',$post_content);
            $post_content=str_replace('[/rihide]','',$post_content);
            
             
            require 'vendor/autoload.php';
            
            
            $phpWord = new \PhpOffice\PhpWord\PhpWord();

            $section = $phpWord->addSection();
           
            $phpWord->setDefaultFontSize('16');//设置文档字体大小
            
           // $myTextElement = $section->addText(strip_tags($post_content));
           
           // $myTextElement->setFontStyle($fontStyle);
            
            $year=date( "Y");
            
            $basic_dir=ABSPATH."wp-content/document/";
            if(!file_exists($basic_dir))
            {
                 
        		@mkdir($basic_dir,0755);
      
        	}
            $upload_dir=$basic_dir.$year;
           
           if(!file_exists($upload_dir))
            {
                 
        		@mkdir($upload_dir,0755);
      
        	}
        
        	$month=date( "m");
            $Dir1=$upload_dir."/".$month."/";
            if(!file_exists($Dir1))
            {
        			@mkdir($Dir1,0755);
        
        	}
	        
            $fileName_1="/".$v->post_title."_".$v->ID."_".date('_YmdHis').".docx";
            $fileName=$Dir1.$fileName_1;
            
            \PhpOffice\PhpWord\Shared\Html::addHtml($section, $post_content, false, false);

            // Save file
            $phpWord->save($fileName);
            
            
        //    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
           // $res=$objWriter->save($fileName);
             
                $i++;
                if(!file_exists($fileName)){
                     $error.='文章生成失败,文章ID:'.$v->ID.'文章标题:'.$v->post_title.',';
                     $message=array('code'=>0,'title'=>$title,'message'=>'<div id="message" class="csf-notice csf-notice-error " style="margin-top:10px"><p>'.$error.'</p></div>');
                     echo json_encode($message);exit;
                     exit;
                }else{
                    update_post_meta( $v->ID, 'cao_status', 1);
                    
                    $down= array(0=>array('name'=>$v->post_title,'url'=>home_url().'/wp-content/document/'.$year.'/'.$month.$fileName_1));
                    update_post_meta( $v->ID, 'cao_downurl_new',$down);
                    $res_data.='文章:"'.$v->post_title.'"已生成文档,ID:'.$v->ID.'。';
                    
                    $message=array('code'=>2,'title'=>$v->post_title,'message'=>'<div id="message"  class="csf-notice csf-notice-success" style="margin-top:10px"><p>文章'.$res_data.'目前共生成'.$i.'篇文档</p ></div>','history'=>$history_id,'i'=>$i,'all'=>count($posts_data));
                     echo json_encode($message);exit;
                 }
              
               
            
        } 
        if(!$message){
               
                $message=array('code'=>1,'title'=>$title,'message'=>'<div id="message" class="csf-notice csf-notice-success" style="margin-top:10px" ><p>数据库中无需要生成文档的文章</p></div>','history'=>$history_id);
                echo json_encode($message);exit;
        }
        
    
    echo json_encode($message);exit;}add_action('wp_ajax_content_to_word', 'content_to_word');

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请通知我们,一经查实,本站将立刻删除。