整合營銷服務商

          電腦端+手機端+微信端=數據同步管理

          免費咨詢熱線:

          Java 用文本或表格替換Word書簽內容

          文將使用Free Spire.Doc for Java免費控件來演示如何替換Word文檔中添加有書簽的文本段落。具體內容如下:

          • 文本替換書簽內容
          • 表格替換書簽內容

          在運行代碼前,需將jar包導入IDEA。可通過下載產品包手動導入或在Maven下的pom.xml文件中添加如下引用。

          <repositories>
                  <repository>
                      <id>com.e-iceblue</id>
                      <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
                  </repository>
          </repositories>
          <dependencies>
              <dependency>
                  <groupId>e-iceblue</groupId>
                  <artifactId>spire.doc.free</artifactId>
                  <version>3.9.0</version>
              </dependency>
          </dependencies>

          示例1 用文本替換書簽內容

          import com.spire.doc.*;
          import com.spire.doc.documents.*;
          
          public class ReplaceWithText {
              public static void main(String[] args) {
                  //加載Word文檔
                  Document doc = new Document("C:\\Users\\Test1\\Desktop\\Sample.docx");
          
                  //定位到書簽"MyBookmark"
                  BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
                  bookmarkNavigator.moveToBookmark("MyBookmark");
          
                  //使用文本替換原書簽的內容, false表示不保留原來的格式
                  bookmarkNavigator.replaceBookmarkContent("使用文本替換書簽內容", false);
          
                  //保存文檔
                  doc.saveToFile("output/ReplaceWithText.docx", FileFormat.Docx);
              }
          }

          或使用Html String替換書簽內容

          import com.spire.doc.*;
          import com.spire.doc.documents.*;
          import com.spire.doc.fields.ParagraphBase;
          
          public class ReplaceWithHTMLString {
              public static void main(String[] args) {
                  //加載Word文檔
                  Document doc = new Document("C:\\Users\\Test1\\Desktop\\Sample.docx");
          
                  //臨時添加一個section
                  Section tempSection = doc.addSection();
                  //添加段落到section并添加Html string到段落
                  String html = "使用文本替換書簽內容";
                  tempSection.addParagraph().appendHTML(html);
          
                  //獲取段落的第一項和最后一項
                  ParagraphBase firstItem = (ParagraphBase)tempSection.getParagraphs().get(0).getItems().getFirstItem();
                  ParagraphBase lastItem = (ParagraphBase)tempSection.getParagraphs().get(0).getItems().getLastItem();
                  //創建TextBodySelection對象
                  TextBodySelection selection = new TextBodySelection(firstItem, lastItem);
                  //創建TextBodyPart對象
                  TextBodyPart bodyPart = new TextBodyPart(selection);
          
                  //定位到書簽"MyBookmark"
                  BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
                  bookmarkNavigator.moveToBookmark("MyBookmark");
          
                  //使用Html string替換原書簽的內容
                  bookmarkNavigator.replaceBookmarkContent(bodyPart);
          
                  //移除臨時添加的section
                  doc.getSections().remove(tempSection);
          
                  //保存結果文檔
                  doc.saveToFile("output/ReplaceWithHTMLString.docx", FileFormat.Docx);
              }
          }

          替換前后對比:

          示例2 用表格替換書簽內容

          import com.spire.doc.*;
          import com.spire.doc.documents.*;
          
          public class ReplaceWithTable {
              public static void main(String[] args) {
                  //加載Word文檔
                  Document doc = new Document("C:\\Users\\Test1\\Desktop\\Sample.docx");
          
                  String[][] data =
                          {
                          new String[]{"名稱", "額定容量", "電源", "工作時間"},
                          new String[]{"LED-901充電式手電筒", "900mAH", "AC110V/220V", "26個小時"},
                          };
          
                  //創建表格
                  Table table = new Table(doc, true);
                  table.resetCells(2, 4);
                  for (int i = 0; i < data.length; i++) {
                      TableRow dataRow = table.getRows().get(i);
                      for (int j = 0; j < data[i].length; j++) {
                          dataRow.getCells().get(j).addParagraph().appendText(data[i][j]);
                      }
                  }
          
                  //創建TextBodyPart對象
                  TextBodyPart bodyPart= new TextBodyPart(doc);
                  bodyPart.getBodyItems().add(table);
          
                  //定位到書簽"MyBookmark"
                  BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
                  bookmarkNavigator.moveToBookmark("MyBookmark");
          
                  //使用表格替換原書簽的內容
                  bookmarkNavigator.replaceBookmarkContent(bodyPart);
          
                  //保存文檔
                  doc.saveToFile("output/ReplaceWithTable.docx", FileFormat.Docx);
              }
          }

          替換效果:

          推薦閱讀:

          如何添加、讀取、刪除Word書簽

          件傳遞有兩種方式,冒泡和捕獲。事件傳遞定義了元素事件觸發的順序,在冒泡中,內部元素的事件會先被觸發,然后在觸發外部元素,如先觸發p元素然后觸發div元素。在捕獲事件中,外部元素先被觸發,然后內部事件觸發。addEventListener() 方法可以指定 "useCapture" 參數來設置傳遞類型:addEventListener(event, function, useCapture);

          創建新的HTML元素(節點)appendChild()

          HTML Collection與NodeList的區別:NodeList 與 HTMLCollection 有很多類似的地方。NodeList 與 HTMLCollection 都與數組對象有點類似,可以使用索引 (0, 1, 2, 3, 4, ...) 來獲取元素。NodeList 與 HTMLCollection 都有 length 屬性。節點列表不是一個數組!節點列表無法使用數組的方法: valueOf(), pop(), push(), 或 join() 。

          頁數據提取的方法很多,從其基本原理來說很多就是通過模擬http請求,發送給服務器,然后接收響應,解析響應的結果。整個過程說簡單也簡單,說復雜也復雜。這里來整理下做過的一些事,走過的路,遇到的坑。



          1,基本思路


          這里舉一個java下載的例子,說明簡單的思路。


          public void downPDF(String urlString, String filename, String pdf,

          String chk, String chk1, String chk2, String chk3) throws Exception {

          URL server = new URL(urlString);

          HttpURLConnection connection = (HttpURLConnection) server

          .openConnection();

          connection.setRequestMethod("GET");

          connection.setDoInput(true);

          connection.setDoOutput(true);

          connection.setUseCaches(false);

          connection.addRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

          connection.addRequestProperty("Accept-Language", "zh-cn,zh;q=0.5");

          connection.addRequestProperty("Accept-Encoding", "gzip, deflate");

          connection.addRequestProperty("Accept-Charset","GB2312,utf-8;q=0.7,*;q=0.7");

          connection.addRequestProperty("Cookie","chk=");

          connection.addRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR2.0.50727; MS-RTC LM 8)");

          // if (headers != null) {

          // for (Header h : headers) {

          // System.out.println(h.getName() + ":" + h.getValue());

          // connection.addRequestProperty(h.getName(), h.getValue());

          // }

          // }

          connection.connect();

          InputStream is = connection.getInputStream();

          OutputStream os = new FileOutputStream(filename);

          byte[] buffer = new byte[1024 * 128];


          if (true) {

          int byteReaded = is.read(buffer);

          while (byteReaded != -1) {

          os.write(buffer, 0, byteReaded);

          byteReaded = is.read(buffer);

          }


          主站蜘蛛池模板: 国产伦理一区二区三区| 国产一区二区高清在线播放 | 亚洲国产精品自在线一区二区| 日韩一区二区在线观看| 日韩少妇无码一区二区三区| 国产韩国精品一区二区三区| 久久精品国产一区二区三区肥胖| 亚洲一区二区三区电影| 少妇一夜三次一区二区| 视频一区二区中文字幕| 无码精品一区二区三区免费视频| 亚洲av无码一区二区三区在线播放| 精品午夜福利无人区乱码一区| 国产精品乱码一区二区三| www一区二区www免费| 无码国产伦一区二区三区视频 | 在线观看视频一区二区| www亚洲精品少妇裸乳一区二区| 麻豆精品久久久一区二区| 亚洲乱码av中文一区二区| 成人在线视频一区| 中文字幕在线一区二区在线| 精品一区二区三区AV天堂| 麻豆一区二区99久久久久| 国产精品亚洲一区二区三区| 久久国产免费一区| 日韩毛片一区视频免费| 国产一区二区久久久| 国产午夜精品一区二区三区嫩草 | 99精品国产高清一区二区| 国产伦理一区二区三区| 精品一区二区三区在线观看视频| 国产亚洲综合一区二区三区| 亚洲电影国产一区| 亚洲片国产一区一级在线观看| 亚洲一区二区三区亚瑟| 国产精华液一区二区区别大吗| 国产成人一区二区三区高清| 中文字幕亚洲综合精品一区| 在线精品国产一区二区三区| 精品国产免费一区二区|