简介POI处理word中的:文本、图、表和宏

WORD文本组成:文本、图、表和宏。HWPF主要关注文本。 

1. 
File Information Block (FIB): 文件信息块 

2. 
文本提取  text extraction 
基本文本提取 : org.apache.poi.hwpf.extractor.WordExtractor 
getText() : TEXT 
getParagraphText() : TEXT 
getTextFromPieces() : YMMV 

特殊文本提取 : org.apache.poi.hwpf.HWPFDocument 
getRange() 

输入流: HWPFDocument 

3. 
页眉和页脚: 
a. 创建 org.apache.poi.hwpf.HWPFDocument 
b. 创建 org.apache.poi.hwpf.usermodel.HeaderStores 

4. 
插入文本 
Range 范围 Paragraph 段落 CharacterRun ? 
insertBefore() 
insertAfter() 
delete() 

5. 
读取WORD文档 
a. 首先是 File Information Block (FIB):它指定文件的大小,位置,文本开始位置。 
b. 开始读取文本块 
non-unicode 块 与 unicode 块 的偏移值需要通过掩码来区分。 

6. 
文本样式 
a. 段落样式 
b. 字条样式 

7. 
官方单元测试代码 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/ 

8. 
示例 
本程序主要是提取WORD的表格到EXCEL. 
运行方法: wordTable2Excel.需要c:\test.doc存在. 

WordTable.java 

Java代码   收藏代码
  1. package cn.bisoft.java.poi;  
  2.   
  3. import java.io.FileInputStream;  
  4. import org.apache.poi.hwpf.HWPFDocument;  
  5. import org.junit.Test;  
  6.   
  7. public class WordTable {  
  8.       
  9.     /** 
  10.      * 转换WORD中的表格内容到EXCEL 
  11.      */  
  12.     @Test  
  13.     public void wordTable2Excel()  
  14.     {  
  15.         String docName = "c:\\test.doc";  
  16.           
  17.         /** 1. 读取WORD表格内容 */  
  18.         HWPFDocument doc = null;  
  19.           
  20.         try {  
  21.             doc = new HWPFDocument(new FileInputStream(docName));  
  22.         } catch (Exception e) {  
  23.             e.printStackTrace();  
  24.         }   
  25.           
  26.         String text = doc.getRange().text();  
  27.           
  28.         /** 2. 放置分隔符:将不可见字符使用空格(32)替换 */  
  29.         char[] charArray = text.toCharArray();  
  30.           
  31.         for(int i = 0; i < charArray.length; i++)  
  32.         {  
  33.             if (charArray[i] <= 31)  
  34.             {  
  35.                 charArray[i] = 32;  
  36.             }  
  37.         }  
  38.           
  39.         text = String.valueOf(charArray);  
  40.           
  41.         /** 3. 将内容用空格切片 */  
  42.         String[] textArray = text.trim().replaceAll("[ ]+"" ").split(" ");  
  43.           
  44.         /** 4.将数据加载到WORD表格模型对象 */  
  45.         WordTableModel wtm = new WordTableModel();  
  46.           
  47.         for(int i = 0; i < textArray.length; i++)  
  48.         {  
  49.             String s = textArray[i].trim();  
  50.               
  51.             // 行首单元格正则匹配  
  52.             if (s.matches("^[a-zA-Z]$") || s.matches("^[0-9]{2}$") || s.matches("^[0-9]{3}$") || s.matches("^[0-9]{4}$"))  
  53.             {  
  54.                 // 换行   
  55.                 wtm.resetMap();  
  56.             }  
  57.               
  58.             // 加载数据, 每行内容按顺序加载 到相应单元格  
  59.             wtm.autoMap(s.trim());  
  60.               
  61.         }  
  62.           
  63.         // 结束加载  
  64.         wtm.endAutoMap();  
  65.           
  66.         /** 5. 保存到EXCEL文件*/  
  67.         wtm.save2Excel("c:\\test.xls""sheet1");  
  68.           
  69.     }  
  70. }  



WordTableModel 
Java代码   收藏代码
  1. package cn.bisoft.java.poi;  
  2.   
  3. import org.apache.poi.ss.usermodel.Cell;  
  4. import org.apache.poi.ss.usermodel.Row;  
  5. import org.apache.poi.ss.usermodel.Sheet;  
  6.   
  7. import cn.bisoft.java.poi.util.ExcelUtil;  
  8.   
  9. public class WordTableModel {  
  10.   
  11.     public static  String SINGLE_WORD = "^[a-z][A-Z]$";  
  12.     public static  String SINGLE_NUMBER = "^[0-9]$";  
  13.     public static  String SINGLE_VAR = "^[0-9a-zA-Z_]$";  
  14.     public static  String PSTN = "^[0-9]{4}[- ]?[0-9]{7}$"// 固定号码  
  15.     public static  String MSISDN = "^1[0-9]{10}$"// 移动号码  
  16.     public static  String IDCARD = "(^\\d{15}$)|(^\\d{17}([0-9]|[Xx])$)"//身份证  
  17.     public static  String EMAIL = "^\\w{3,}@\\w.{3,}\\w{2,}$"// 电子邮件  
  18.       
  19.     private String[][] table = new String[100000][1000];  
  20.     private int curColumn = 0;  
  21.     private int curRow = 0;  
  22.   
  23.     public void autoMap(String value)  
  24.     {  
  25.         table[curRow][curColumn] = value;  
  26.         curColumn++;  
  27.     }  
  28.       
  29.     public void resetMap()  
  30.     {  
  31.         curColumn = 0;  
  32.         curRow++;  
  33.     }  
  34.       
  35.     public void endAutoMap()  
  36.     {  
  37.         curRow++;  
  38.     }  
  39.       
  40.     /** 
  41.      * @param fileName 
  42.      */  
  43.     public void save2Excel(String fileName, String sheetName)  
  44.     {  
  45.         Sheet sheet = ExcelUtil.createSheet(fileName, sheetName);  
  46.           
  47.         for(int i = 0; i < curRow; i++)  
  48.         {  
  49.             Row row = sheet.createRow(i);  
  50.               
  51.             for(int j = 0; j < curColumn; j++)  
  52.             {  
  53.                 Cell cell = row.createCell(j);  
  54.                 cell.setCellValue(table[i][j]);  
  55.             }  
  56.         }  
  57.           
  58.         ExcelUtil.saveSheet(fileName, sheet);  
  59.     }  
  60.       
  61. }  



ExcelUtil 

Java代码   收藏代码
  1. package cn.bisoft.java.poi.util;  
  2.   
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6.   
  7. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  8. import org.apache.poi.ss.usermodel.Sheet;  
  9. import org.apache.poi.ss.usermodel.Workbook;  
  10.   
  11. public class ExcelUtil {  
  12.       
  13.     public static Sheet createSheet(String fileName, String sheetName)  
  14.     {  
  15.         Workbook wb = new HSSFWorkbook();  
  16.         FileOutputStream fileOut = null;  
  17.         Sheet sheet = null;  
  18.           
  19.         try {  
  20.             fileOut = new FileOutputStream(fileName);  
  21.             sheet = wb.createSheet(sheetName);  
  22.             wb.write(fileOut);  
  23.             fileOut.close();  
  24.         } catch (FileNotFoundException e1) {  
  25.             e1.printStackTrace();  
  26.         } catch (IOException e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.           
  30.         return sheet;  
  31.     }  
  32.       
  33.     public static void saveSheet(String fileName, Sheet sheet)  
  34.     {  
  35.         FileOutputStream fileOut = null;  
  36.           
  37.         try {  
  38.             fileOut = new FileOutputStream(fileName);  
  39.             sheet.getWorkbook().write(fileOut);  
  40.             fileOut.close();  
  41.         } catch (FileNotFoundException e) {  
  42.             e.printStackTrace();  
  43.         } catch (IOException e) {  
  44.             e.printStackTrace();  
  45.         }  
  46.     }  
  47. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值