Java adds new rows to the word table and assigns values

Preface:
I believe that everyone has had the experience of operating word documents and tables, for each row of data recording and operation is more convenient. But this is not enough. For some OA projects, you need to open word documents online and add data to word tables through background settings, or even add new rows to tables. This requires the ability of developers to integrate activex controls into the Java environment. For most Java development engineers, it is too expensive to master activex controls just to meet the needs of their own projects.

So today we will introduce a middleware technology - page office. This middleware technology has integrated most of the functions of operating office documents in the web system. We only need to pay attention to the business logic of our web system. The learning cost is low and the functions are rich. Now let's use page office to realize the function of adding new rows and assigning values to word tables while opening word documents online in Java language.

First look at the effect:
This is the original document

Following is the document opened by the page office background settings

You can see that the document table has been increased from three to five rows, and the corresponding data has been added to the corresponding cells through the background.

Deployment steps (only 4 steps)
1. official website http://www.zhuozhengsoft.com/dowm/ Download integration files, introduce jar packages, configure web.xml


Go to the integration file you just downloaded, find lib, put the contents in the Lib of the project web-inf, introduce the jar package, and then introduce the page office configuration of web.xml into the wb.xml of your project.

2. Place a label or button on the parent page aaa.jsp (page that needs to open the document)
Introduce the js file required by pageoffice before writing a tag

<script type="text/javascript" src="/jquery.min.js"></script>
<script type="text/javascript" src="/pageoffice.js" id="po_js_main"></script>


Then add a tag

<a href="javascript: POBrowser.open Windows Modeless ('WordTable.jsp','width=1200px; height=800px;');">Insert new rows into Table in Word document and assign values</a>

3. Create a file named WordTable.jsp in the parent page's sibling directory

 

<a href="javascript:POBrowser.openWindowModeless('WordTable.jsp' , 'width=1200px;height=800px;');">towards Word In document Table Insert new rows and assign values</a>
3.Create a name under the parent page's sibling directory WordTable.jsp Documents
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page
    import="com.zhuozhengsoft.pageoffice.*,com.zhuozhengsoft.pageoffice.wordwriter.*"%>
<%
    PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
    WordDocument doc = new WordDocument();
    Table table1 = doc.openDataRegion("PO_T001").openTable(1);
 
    table1.openCellRC(1,1).setValue("PageOffice assembly");
    int dataRowCount = 5;//Number of rows to insert data
    int oldRowCount = 3;//Number of rows in the table
    // Expanded form
    for (int j = 0; j < dataRowCount - oldRowCount; j++)
    {
        table1.insertRowAfter(table1.openCellRC(2, 5));  //Insert a new row under the last cell in line 2
    }
    // Fill data
    int i = 1;
    while (i <= dataRowCount)
    {   
        table1.openCellRC(i, 2).setValue("AA" + String.valueOf(i));
        table1.openCellRC(i, 3).setValue("BB" + String.valueOf(i));
        table1.openCellRC(i, 4).setValue("CC" + String.valueOf(i));
        table1.openCellRC(i, 5).setValue("DD" + String.valueOf(i));
        i++;
    }
    poCtrl.setWriter(doc);
    poCtrl.setServerPage(request.getContextPath()+"/poserver.zz");
    poCtrl.webOpen("test.doc", OpenModeType.docNormalEdit,"Zhang Yi Ming");
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
 
        <title>Word Medium Table Data Filling</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
 
    </head>
 
    <body>
        <div style="width: auto; height: 750px;">
        <%=poCtrl.getHtmlCode("PageOfficeCtrl1")%>
        </div>
    </body>
</html>

4. New Documents
Create a new word file named test.doc (if you create a new docx file, change the third step of poCtrl.webOpen ("test.doc", "OpenModeType.docNormalEdit", "Zhang Fan Name"); change the code to poCtrl.webOpen ("test.docx", "OpenModeType.Normal Edit"); and change the code to poCtrl.webOpen ("test.docx", "OpenModeType.Normal Edit").

Place the newly created test.doc file in the WordTable.jsp peer directory.

Open the test.doc file locally, create a table with three rows and five columns, and then click the full selection flag in the upper left corner of the table to select the entire table.

With the entire table selected, click on the menu bar to insert - Bookmark - Book signature set to PO_T001 - click Add.

Then start the project to visit aaa.jsp directly and click on the link. At this time, you will be prompted to install the plug-in. After successful installation, you will be prompted to register, fill in the relevant information, and fill in the registration code Q37LN-W8NI-KFSQ-LEY3Y to open the document.
Note: If you need more functions, you can download the sample code from the page Office website and throw the samples4 folder directly into Tomcat's webapps. Start Tomcat. The browser accesses http://localhost:8080/Samples4/index.html. Look at the following link in the sample and see the code in the WordTable folder under the samples4 folder directly.

 

If you are new to page office, you can also watch videos and get started quickly. http://www.zhuozhengsoft.com/Technical/

Tags: Front-end JSP Java Javascript xml

Posted on Sat, 12 Oct 2019 00:53:04 -0700 by beboo002