zoukankan      html  css  js  c++  java
  • Java使用POI读取Word中的表格


    个人博客 地址:https://www.wenhaofan.com/a/20190627135921

    代码

    package live.autu.word;
    
    import java.io.FileInputStream;
    
    import org.apache.poi.hwpf.HWPFDocument;
    import org.apache.poi.hwpf.usermodel.Paragraph;
    import org.apache.poi.hwpf.usermodel.Range;
    import org.apache.poi.hwpf.usermodel.Table;
    import org.apache.poi.hwpf.usermodel.TableCell;
    import org.apache.poi.hwpf.usermodel.TableIterator;
    import org.apache.poi.hwpf.usermodel.TableRow;
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;
    
    /**
     * Hello world!
     *
     */
    
    public class App {
    	public static void main(String[] args) {
    		//doc文档路径
    		String filePath = "C:\Users\autu\Desktop\test.doc";
    		//test.print(filePath,"第一个表");
    		
    		System.out.println(App.read(filePath,"第一个表"));;
    	}
    
    	/**
    	 * 读取文档中表格
    	 * @param filePath doc路径
    	 * @param set 第几个表格
    	 */
    	public static String read(String filePath,String tableName) {
    
    		StringBuilder sb=new StringBuilder();
    		
    		
    		try (FileInputStream in = new FileInputStream(filePath); // 载入文档
    				POIFSFileSystem pfs = new POIFSFileSystem(in);
    				HWPFDocument hwpf = new HWPFDocument(pfs);) {
    		 
    			Range range = hwpf.getRange();// 得到文档的读取范围
    			TableIterator it = new TableIterator(range);
    			// 迭代文档中的表格
    	 
    			while (it.hasNext()) {
    				Table tb = (Table) it.next();
    			 
    				// 迭代行,默认从0开始,可以依据需要设置i的值,改变起始行数,也可设置读取到那行,只需修改循环的判断条件即可
    				outer:for (int i = 0; i < tb.numRows(); i++) {
    					TableRow tr = tb.getRow(i);
    					// 迭代列,默认从0开始
    					for (int j = 0; j < tr.numCells(); j++) {
    						TableCell td = tr.getCell(j);// 取得单元格
    						// 取得单元格的内容
    						for (int k = 0; k < td.numParagraphs(); k++) {
    							Paragraph para = td.getParagraph(k);
    							String s = para.text();
    							// 去除后面的特殊符号
    							if (null != s && !"".equals(s)) {
    								s = s.substring(0, s.length() - 1);
    							}
    							s=s.trim();
    							if(tableName.trim().equals(s)||i!=0) {
    								sb.append(s + "	");
    							} else {
    								break outer;
    							}	
    						}
    					}
    					sb.append( "
    ");
    				}
    			}
    
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		
    		return sb.toString();
    	}
    
    }

    依赖

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>4.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-scratchpad</artifactId>
        <version>4.0.1</version>
    </dependency>

    效果图

            test.doc

            http://qiniu.wenhaofan.com/520520_20190627135716.png

             控制台打印
            http://qiniu.wenhaofan.com/520520_20190627135748.png


  • 相关阅读:
    要发布游戏啦,平台要 3000个6位不重复的验证码 看我怎么做!!!
    数字化婚姻配对尝试
    java 和 .NET 的 类继承方面 的不同
    Asp.Net Core 实现谷歌翻译ApI 免费版
    Asp.Net Core 全局模型验证
    Asp.Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty
    Windows 10 远程连接出现函数错误 【这可能由于CredSSP加密Oracle修正】
    矫正系统时间
    linux 安装WildFly 及可能遇到的问题
    ubuntu 安装 jdk-7
  • 原文地址:https://www.cnblogs.com/fanwenhao/p/11096596.html
Copyright © 2011-2022 走看看