程式CODE
2013年9月30日 星期一
SFS3自製模組時,若需要輸出SXW檔的語法
SXW其實是個zip檔
所以,原理就是把一些需要的檔案zip出來,
檔名設定為SWX即可。
以下為範例
<?
//輸出個別sxw檔
function make_ooo($title_name,$action,$ClassName,$SubjectName,$TeacherName){
global $CONN,$UPLOAD_PATH;
//Openofiice的路徑
$oo_path=$UPLOAD_PATH."time_table/sample";
$filename=$title_name."_time_table.sxw";
//新增一個 zipfile 實例
$ttt = new EasyZip;
$ttt->setPath($oo_path);
//加入 xml 檔案到 zip 中,共有五個檔案
//讀入應該要包進去的檔,含圖檔
if (is_dir($oo_path)) {
if ($dh = opendir($oo_path)) {
while (($file = readdir($dh)) !== false) {
if($file=="." or $file==".." or $file=="content.xml" or $file=="Configurations2" or $file=="Thumbnails" or strtoupper(substr($file,-4))=='.SXW') {
continue;
}elseif(is_dir($oo_path."/".$file)){
if ($dh2 = opendir($oo_path."/".$file)) {
while (($file2 = readdir($dh2)) !== false) {
if($file2=="." or $file2==".."){
continue;
}else{
$data = $ttt->read_file($oo_path."/".$file."/".$file2);
$ttt->add_file($data,$file."/".$file2);
}
}
closedir($dh2);
}
}else{
$data = $ttt->read_file($oo_path."/".$file);
$ttt->add_file($data,$file);
}
}
closedir($dh);
}
}
//再讀出 content.xml
$data = $ttt->read_file($oo_path."/content.xml");
// 加入換頁 tag
$data = str_replace("<office:automatic-styles>",'<office:automatic-styles><style:style style:name="sfs_break_page" style:family="paragraph" style:parent-style-name="Standard"><style:properties fo:break-before="page"/></style:style>',$data);
//拆解 content.xml
$arr1 = explode("<office:body>",$data);
//檔頭
$con_head = $arr1[0]."<office:body>";
$arr2 = explode("</office:body>",$arr1[1]);
//資料內容
$con_body = $arr2[0];
//檔尾
$con_foot = "</office:body>".$arr2[1];
if($action=="teacher"){
$class_name="◎◎教室";
$tea_array=explode("_",$title_name);
$teacher_name=get_teacher_name($tea_array[2]);
}
if($action=="class"){
//查出某班的導師姓名
$class_array=explode("_",$title_name);
$classnum=substr($class_array[2].$class_array[3],1,3);
$str = "select teacher_sn from teacher_post where class_num='".$classnum."'";
$result = $CONN->Execute($str) or die ($str);
list($teacher_sn)=$result->FetchRow();
$teacher_name=get_teacher_name($teacher_sn);
$class_base_p=class_base();
$class_name=$class_base_p[$classnum];
//導師時間
for($i=1;$i<=5;$i++){
$SubjectName[$i][0]="導師時間";
$TeacherName[$i][0]=$teacher_name;
}
}
//置換今年度
$tyear=substr($title_name,0,3);
$con_body = str_replace("year",$tyear,$con_body);
//置換班級名及教師名
$class_name=iconv("big5","UTF-8",$class_name);
$con_body = str_replace("class",$class_name,$con_body);
$teacher_name=iconv("big5","UTF-8",$teacher_name);
$con_body = str_replace("teacher",$teacher_name,$con_body);
//sxw檔的中文是UTF-8,SFS3裡的字串先由big5轉為utf8
for($i=1;$i<=5;$i++){
for($j=0;$j<=7;$j++){
$SubjectName[$i][$j]=iconv("big5","UTF-8",$SubjectName[$i][$j]);
//如果科目名稱大於四個字,只好取前兩個字
if(strlen($SubjectName[$i][$j])>12){
$SubjectName[$i][$j] = mb_substr($SubjectName[$i][$j],0,2,"UTF-8");
}
//如果科目名稱大於兩個字,換行
if(strlen($SubjectName[$i][$j])>6){
$SubjectName[$i][$j] = mb_substr($SubjectName[$i][$j],0,2,"UTF-8")."</text:p><text:p text:style-name='P7'>".mb_substr($SubjectName[$i][$j],2,2,"UTF-8");
}
$con_body = str_replace("s".$i.$j,$SubjectName[$i][$j],$con_body);
$ClassName[$i][$j]=iconv("big5","UTF-8",$ClassName[$i][$j]);
$con_body = str_replace("c".$i.$j,$ClassName[$i][$j],$con_body);
$TeacherName[$i][$j]=iconv("big5","UTF-8",$TeacherName[$i][$j]);
$con_body = str_replace("t".$i.$j,$TeacherName[$i][$j],$con_body);
}
}
$replace_data=$con_head.$con_body.$con_foot;
//把一些多餘的標籤以空白取代
$pattern[]="/\{([^\}]*)\}/";
$replacement[]="";
$replace_data=preg_replace($pattern, $replacement, $replace_data);
$ttt->add_file($replace_data,"content.xml");
//產生 zip 檔
$sss = $ttt->file();
//以串流方式送出 ooo.sxw
header("Content-disposition: attachment; filename=$filename");
header("Content-type: application/vnd.sun.xml.writer");
header("Pragma: no-cache");
header("Expires: 0");
echo $sss;
exit;
}
?>
另外,若要使用者上傳SWX檔,就要先unzip這個檔,
放置好位置,需要輸出時,再zip出來,改附檔名,下載。
不過,我還沒有研究出來。
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言