記PageOffice實現文檔的在線編輯

1、postup.exe安裝
2、導入Jar包

  <!--openOffice架包 start-->
  <dependency>
      <groupId>com.zhuozhengsoft</groupId>
      <artifactId>pageoffice</artifactId>
      <version>4.5.0.2</version>
  </dependency>
  <dependency>
      <groupId>com.artofsolving</groupId>
      <artifactId>jodconverter</artifactId>
      <version>2.2.1</version>
  </dependency>
  <dependency>
    <groupId>com.github.livesense</groupId>
    <artifactId>jodconverter-core</artifactId>
    <version>1.0.5</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
  <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.6.0</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-nop -->
  <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-nop</artifactId>
      <version>1.6.0</version>
      <scope>test</scope>
  </dependency>
  <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>${fileupload.version}</version>
  </dependency>
  <!--end-->

3、jsp使用

<a id="exercise" href="#" onclick="showExercise()"><span>作業</span></a>
function showExercise() {
    var flag = '${DetailResource.chapterExercise}';
    if (flag == 'null' || flag == null || flag == ''){
        swal("提示","該章節無作業!");
    }else{
        $('#exercise').attr('href','javascript:POBrowser.openWindowModeless(\'../exercise/word?UserID=${sessionScope.UserID}&CourseID=${DetailResource.courseID}&ChapterID=${DetailResource.chapterID}&MinChapterID=${DetailResource.minChapterID}&ChapterExercise=${DetailResource.chapterExercise}\',\'width=1200px;height=800px;\')');
    }
    return false;
}

4、controller層

package com.graduation.controller;
import com.graduation.bean.Exercise;
import com.graduation.bean.ExerciseCheck;
import com.graduation.bean.ExerciseIntermediate;
import com.graduation.service.ExerciseService;
import com.graduation.util.CosineSimilarAlgorithm;
import com.graduation.util.WordRead;
import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import com.zhuozhengsoft.pageoffice.wordwriter.*;
import com.zhuozhengsoft.pageoffice.wordwriter.Font;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author K
 */
@Controller
@RequestMapping("/exercise")
public class ExerciseController {
    @Autowired
    private ExerciseService exerciseService;


    @RequestMapping("/word")
    public String openWord(@RequestParam(value = "UserID",required = true) String UserID,@RequestParam(value = "CourseID",required = true) String CourseID,@RequestParam(value = "ChapterID",required = true) String ChapterID,@RequestParam(value = "MinChapterID",required = true) String MinChapterID,@RequestParam(value = "ChapterExercise",required = true)String ChapterExercise, HttpServletRequest request, Map<String,Object> map){
        System.out.println(CourseID+ChapterID+MinChapterID+UserID);
        System.out.println(ChapterExercise);
        String path = request.getServletContext().getRealPath("");
        PageOfficeCtrl poCtrl=new PageOfficeCtrl(request);
        /*poCtrl.setDisableCopyOnly(true);*/
        request.setAttribute("poCtrl", poCtrl);
        String filePath = CourseID+ChapterID+MinChapterID+UserID;
        String initfilePath = ChapterExercise;
        poCtrl.setServerPage(request.getContextPath()+"/poserver.zz");//設置服務頁面
        poCtrl.addCustomToolButton("提交","Save",1);//添加自定義保存按鈕
        poCtrl.setSaveFilePage("save?fileName="+filePath+"&CourseID="+CourseID+"&ChapterID="+ChapterID+"&MinChapterID="+MinChapterID+"&UserID="+UserID);//設置處理文件保存的請求方法
        //打開word

        try {
            poCtrl.webOpen(path + "\\static\\exercises\\"+filePath+".doc",OpenModeType.docAdmin,UserID);
            System.out.println("進入已存在文件");
        }catch (Exception e) {
            try {
                poCtrl.webOpen(path + "\\static\\exercises\\"+initfilePath+".doc",OpenModeType.docAdmin,UserID);
                System.out.println("已進入初始化word");
            }catch (Exception ex){
                System.out.println("本節無作業!");
            }

        }
        poCtrl.setTagId("PageOfficeCtrl1");
        return "word";//返回到word.jsp
   		}
   		//打開word文件時動態添加表格
   		@RequestMapping("/openExerciseWord")
	    public String openExerciseWord(@RequestParam(value = "UserID",required = true) String UserID,@RequestParam(value = "CourseID",required = true) String CourseID,@RequestParam(value = "ChapterID",required = true) String ChapterID,@RequestParam(value = "MinChapterID",required = true) String MinChapterID,@RequestParam(value = "ChapterExercise",required = true)String ChapterExercise, HttpServletRequest request, Map<String,Object> map){
	        System.out.println(CourseID+ChapterID+MinChapterID+UserID);
	        System.out.println(ChapterExercise);
	        String path = request.getServletContext().getRealPath("");
	        PageOfficeCtrl poCtrl=new PageOfficeCtrl(request);
	        /*poCtrl.setDisableCopyOnly(true);*/
	        WordDocument doc = new WordDocument();
	        DataRegion  data = doc.createDataRegion("PO_table",DataRegionInsertType.Before, "[End]");
	        //在word中指定的"PO_table1"的數據區域內動態創建一個3行5列的表格
	        Table table1 = data.createTable(2,1,WdAutoFitBehavior.wdAutoFitWindow);
	        //合併(1,1)到(3,1)的單元格並賦值
	        table1.openCellRC(1,1).setValue("成績");
	        //給表格table1中剩餘的單元格賦值
	        /*table1.setRowsHeight(10f);
	        table1.setPreferredWidth(70f);*/
	        //設置表格的邊框
	       /* Border border=table1.getBorder();
	        // 設置邊框的類型
	        border.setBorderType(WdBorderType.wdFullGrid);//包含內邊框
	        //設置邊框的顏色
	        border.setLineColor(Color.red);
	        //設置邊框的線條樣式
	        border.setLineStyle(WdLineStyle.wdLineStyleDot );
	        //設置邊框的粗細
	        border.setLineWidth(WdLineWidth.wdLineWidth150pt);*/
	
	        //設置表格內字體樣式
	        /*Font font=data.getFont();
	        //設置字體的是否加粗
	        font.setBold(true);
	        //設置字體的顏色
	        font.setColor(Color.BLACK);
	        //設置字體是否爲斜體
	        font.setItalic(true);
	        //設置字體名稱
	        font.setName("宋體");
	        //設置字體大小
	        font.setSize(12f);*/
	        poCtrl.setWriter(doc);
	        request.setAttribute("poCtrl", poCtrl);
	        String filePath = CourseID+ChapterID+MinChapterID+UserID;
	        String initfilePath = ChapterExercise;
	        poCtrl.setServerPage(request.getContextPath()+"/poserver.zz");
	        poCtrl.addCustomToolButton("提交","Save",1);
	        poCtrl.setSaveFilePage("save?fileName="+filePath+"&CourseID="+CourseID+"&ChapterID="+ChapterID+"&MinChapterID="+MinChapterID+"&UserID="+UserID);
	        //打開word
	        poCtrl.setSaveDataPage("/ex/saveExercise?fileName="+filePath+"&CourseID="+CourseID+"&ChapterID="+ChapterID+"&MinChapterID="+MinChapterID+"&UserID="+UserID);
	
	        try {
	            poCtrl.webOpen(path + "\\static\\exercises\\"+filePath+".doc",OpenModeType.docNormalEdit,UserID);
	            System.out.println("進入已存在文件");
	        }catch (Exception e) {
	            try {
	                poCtrl.webOpen(path + "\\static\\exercises\\"+initfilePath+".doc",OpenModeType.docNormalEdit,UserID);
	                System.out.println("已進入初始化word");
	            }catch (Exception ex){
	                System.out.println("本節無作業!");
	            }
	
	        }
	        poCtrl.setTagId("PageOfficeCtrl1");
	        return "word";
	    }
	    @RequestMapping("/save")
	    public void save(@RequestParam(value = "fileName",required = true) String filePath,@RequestParam(value = "CourseID",required = true) String CourseID,@RequestParam(value = "ChapterID",required = true) String ChapterID,@RequestParam(value = "MinChapterID",required = true) String MinChapterID,@RequestParam(value = "UserID",required = true) String UserID,HttpServletRequest request, HttpServletResponse response){
	        System.out.println(filePath);
	        FileSaver fs = new FileSaver(request, response);
	        String path = request.getServletContext().getRealPath("\\") + "\\static\\exercises\\"+filePath+".doc";
	        fs.saveToFile(path);
	        fs.close();
	        int flag = exerciseService.QueryPresence(CourseID,ChapterID,MinChapterID,filePath,UserID);
	        if(flag == 0){
	            exerciseService.AddExercise(CourseID,ChapterID,MinChapterID,filePath,UserID);
	   	}
}

4、word.jsp

<%@ page language="java"
         import="java.util.*,com.zhuozhengsoft.pageoffice.*"
         pageEncoding="UTF-8"%>
<%
    PageOfficeCtrl poCtrl=(PageOfficeCtrl)request.getAttribute("poCtrl");
%>
<html>
<head>
    <title>作業</title>
</head>
<body>
<div style="width:100%; height:100%;">
    <%=poCtrl.getHtmlCode("PageOfficeCtrl1")%>
</div>
<script type="text/javascript">
    function Save() {
        document.getElementById("PageOfficeCtrl1").WebSave();
    }
</script>
</body>
</html>
發佈了18 篇原創文章 · 獲贊 4 · 訪問量 6136
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章