/** * @Title: repStrPHPword * @Description: todo(模板标签文件和数据进行替换,然后到处word) * @param 模板标签文件地址 $docfile * @param 生成的最新文件目录 $causedir * @param 导出的数据源数组 $bookNamearr * @param 生成的最新文件名 $fileName * @param 导出的文件类型,默认为word $type * @author 王昭侠 * @date 2015-6-29 下午4:54:45 * @throws */ public function repStrPHPword($docfile, $causedir, $bookNamearr, $fileName, $type = "word", $export = 'word') { set_time_limit(0); //验证模板标签文件是否存在 if (!file_exists($docfile)) { $this->error("模板文件不存在,请检查"); } //验证最新文件目录是否存在 if (!file_exists($causedir)) { //不存在则生成最新文件目录 $this->createFolders($causedir); //判断目标文件夹是否存在 } import('@.ORG.PHPWord', '', $ext = '.php'); $PHPWord = new PHPWord(); $filepath = pathinfo($docfile); $filenameGBK = $causedir . '/' . $fileName . '.' . $filepath['extension']; $filenameUTF8 = iconv("UTF-8", "GBK", $filenameGBK); $document = $PHPWord->loadTemplate($docfile); $document->clearAllBiaoji(); foreach ($bookNamearr as $k => $v) { if ($v["is_datatable"]) { $data = array(); $data["showname"] = $v["showname"]; $data["value"] = $v["value"]; $data["showtype"] = $v["showtype"] !== NULL ? $v["showtype"] : 0; $data["showtitle"] = $v["showtitle"] !== NULL ? $v["showtitle"] : 0; $data["zihao"] = $v["zihao"]; $data["hangjianju"] = $v["hangjianju"]; $data["ziti"] = $v["ziti"]; if (isset($v["colORrow"])) { $data["colORrow"] = $v["colORrow"]; } //title_none 如果等于1表示去掉hearder头部 if (is_array($v["value"]) && !isset($v["colORrow"]) && $v['title_none'] != 1) { foreach ($v["value"] as $kk => $vv) { $data["titleArr"][] = empty($vv["showname"]) ? "" : $vv["showname"]; $data["fieldwidth"][] = $v["fieldwidth"][$vv["name"]]; } } $document->setValue($v["name"], $data); } else { $document->setValue($v["name"], $v); } } if (!empty($_SESSION["htmltodocx_img"])) { $content = $document->getStr(); $document->clearTemplateTag(); $document->save($filenameUTF8); // New portrait section $section = $PHPWord->createSection(); // Add image elements foreach ($_SESSION["htmltodocx_img"] as $v => $k) { $section->addImage($k["src"], $k["style"]); $section->addTextBreak(1); } // Save File $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $imgurl = UPLOAD_SampleWord . 'Image.docx'; $objWriter->save($imgurl); unset($_SESSION["htmltodocx_img"]); $documentImg = $PHPWord->loadTemplate($imgurl); $documentImg->setStr($content); $documentImg->save($filenameUTF8); unlink($imgurl); } else { $document->clearTemplateTag(); $document->save($filenameUTF8); } if ($export == 'swf') { //生成PDF $HttpSoctetIOAction = A('Http'); $filenamePDFUTF8 = $HttpSoctetIOAction->createPDF($causedir, $filenameUTF8); $swfurl = str_replace("pdf", "swf", $filenamePDFUTF8); $swfurl = preg_replace("/^([\\s\\S]+)\\/Public\\//", "", $swfurl); if ($type == "pdf") { $filenameUTF8 = $filenamePDFUTF8; } } //导出后,就修改版本信息 $savewordmodel = M('mis_system_template_saveword'); $filenameUTF8Quan = preg_replace("/^([\\s\\S]+)\\/Public\\//", "", $filenameUTF8); //获取参数 $id = $_REQUEST['id']; $saveWordId = $_REQUEST['fileid']; $modelname = $this->getActionName(); $wordMap['modelid'] = $id; $wordMap['modelname'] = $modelname; $wordMap['id'] = $saveWordId; if ($export == 'swf' && !$saveWordId) { //如果是在线查看,却又没有版本id 那么直接获取最新版本号 $swfnewmap['modelid'] = $id; $swfnewmap['modelname'] = $modelname; $saveWordId = $wordMap['id'] = $savewordmodel->where($swfnewmap)->order("id desc")->getField("id"); } $wordData['fileurl'] = $filenameUTF8Quan; $wordData['swfurl'] = $swfurl; $wordData['isexport'] = 1; if ($modelname == 'MisSalesMyProject') { $wordData['modelid'] = $id; $wordData['modelname'] = $modelname; unset($wordMap['id']); $wlist = $savewordmodel->where($wordMap)->select(); if ($wlist) { $query = $savewordmodel->where($wordMap)->save($wordData); } else { $query = $savewordmodel->add($wordData); } } else { if ($wordMap['id']) { $query = $savewordmodel->where($wordMap)->save($wordData); } } $savewordmodel->commit(); if ($export == 'word') { ob_end_clean(); header("Cache-Control: public"); header("Content-Type: application/force-download"); header("Content-Disposition: attachment; filename=" . basename($filenameUTF8)); readfile($filenameUTF8); exit; } elseif ($export == "swf") { // if($saveWordId){ // $swfmap['id']=$saveWordId; // $templateModel = M("mis_system_template_saveword"); // $rs = $templateModel->where($swfmap)->order("id desc")->find(); // $file_path = "/Public/".$rs['swfurl'];//暂时修改,等保存路径正常后直接取路径 // }else{ // $file_path = "/Public/".$swfurl;//暂时修改,等保存路径正常后直接取路径 // } //$file_path = preg_replace("/^([\s\S]+)\/Public/", "__PUBLIC__", $rs['swfurl']); // $this->assign("file_name", $rs['filename']); // $this->assign("file_type", 'file'); // $this->assign('file_path', $file_path); // $this->display("Public:playswf"); // $ip = GetHostByName($_SERVER['SERVER_NAME']);//获取本机IP $ip = C("DB_HOST_WORD"); //"192.168.0.238"; require_once "http://{$ip}:8088/JavaBridge/java/Java.inc"; //此行必须 $PageOfficeCtrl = new Java("com.zhuozhengsoft.pageoffice.PageOfficeCtrlPHP"); //此行必须 $PageOfficeCtrl->setServerPage("http://{$ip}:8088/JavaBridge/poserver.zz"); //此行必须,设置服务器页面 java_set_file_encoding("utf8"); //设置中文编码,若涉及到中文必须设置中文编码 $PageOfficeCtrl->setAllowCopy(false); //禁止拷贝 //$PageOfficeCtrl->setMenubar(false);//隐藏菜单栏 $PageOfficeCtrl->setOfficeToolbars(false); //隐藏Office工具条 $PageOfficeCtrl->setCustomToolbar(false); //隐藏自定义工具栏 $PageOfficeCtrl->setJsFunction_AfterDocumentOpened("AfterDocumentOpened"); //打开excel文档 $PageOfficeCtrl->UserAgent = $_SERVER['HTTP_USER_AGENT']; //若使用谷歌浏览器此行代码必须有,其他浏览器此行代码可不加 $OpenMode = new Java("com.zhuozhengsoft.pageoffice.OpenModeType"); $file_path = preg_replace("/^([\\s\\S]+)\\/Public/", "/Public/", $filenameUTF8); $PageOfficeCtrl->webOpen($file_path, $OpenMode->docReadOnly, "张三"); //此行必须 $this->assign('PageOfficeCtrl', $PageOfficeCtrl->getDocumentView("PageOfficeCtrl1")); $this->display("Public:showEditWord"); exit; } }
<?php $ip = GetHostByName($_SERVER['SERVER_NAME']); //获取本机IP require_once "http://" . $ip . ":8080/JavaBridge/java/Java.inc"; //此行必须 $FileMakerCtrl = new Java("com.zhuozhengsoft.pageoffice.FileMakerCtrlPHP"); //此行必须 $FileMakerCtrl->setServerPage("http://" . $ip . ":8080/JavaBridge/poserver.zz"); //此行必须,设置服务器页面 java_set_file_encoding("GBK"); //设置中文编码,若涉及到中文必须设置中文编码 $id = $_REQUEST["id"]; $filepath = "doc/" . $id . "/" . date('Ymd', time()); $type = $_REQUEST['type']; $doc = new Java("com.zhuozhengsoft.pageoffice.wordwriter.WordDocument"); //声明WordDocument变量 $FileMakerCtrl->UserAgent = $_SERVER['HTTP_USER_AGENT']; //禁用右击事件 $doc->setDisableWindowRightClick(true); //给数据区域赋值,即把数据填充到模板中相应的位置 //$doc->openDataRegion("PO_company")->setValue("北京卓正志远软件有限公司"); $FileMakerCtrl->setSaveFilePage("SaveMaker.php?id=" . $id . "&type=" . $type); $FileMakerCtrl->setWriter($doc); $FileMakerCtrl->setJsFunction_OnProgressComplete("OnProgressComplete()"); $DocumentOpenType = new Java("com.zhuozhengsoft.pageoffice.DocumentOpenType"); $FileMakerCtrl->fillDocument($filepath . "/" . $type . ".doc", $DocumentOpenType->Word); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>
<?php if (!defined('THINK_PATH')) { exit; } $ip = GetHostByName($_SERVER['SERVER_NAME']); require_once "http://" . $ip . ":8080/JavaBridge/java/Java.inc"; $PageOfficeCtrl = new Java("com.zhuozhengsoft.pageoffice.PageOfficeCtrlPHP"); $PageOfficeCtrl->setServerPage("http://" . $ip . ":8080/JavaBridge/poserver.zz"); java_set_file_encoding("utf8"); $doc = new Java("com.zhuozhengsoft.pageoffice.wordwriter.WordDocument"); $dataRegion1 = $doc->openDataRegion("PO_question"); $dataRegion1->setEditing(true); $dataRegion1->setValue(""); $dataRegion1->setSubmitAsFile(true); $dataRegion2 = $doc->openDataRegion("PO_answer"); $dataRegion2->setEditing(true); $dataRegion2->setValue(""); $dataRegion2->setSubmitAsFile(true); $dataRegion3 = $doc->openDataRegion("PO_jiexi"); $dataRegion3->setEditing(true); $dataRegion3->setValue(""); $dataRegion3->setSubmitAsFile(true); $PageOfficeCtrl->setWriter($doc); $PageOfficeCtrl->addCustomToolButton("uTeach编题区", "Save", 9); $PageOfficeCtrl->setSaveDataPage("/Word/SaveData.php?id=" . $_SESSION['uid']); $PageOfficeCtrl->setMenubar(false); $PageOfficeCtrl->setTitlebar(false); $PageOfficeCtrl->UserAgent = $_SERVER['HTTP_USER_AGENT']; $OpenMode = new Java("com.zhuozhengsoft.pageoffice.OpenModeType"); $PageOfficeCtrl->webOpen($questionDocPath, $OpenMode->docSubmitForm, "张三");