中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

java 使用POI 讀寫word 表格

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用

POI是apache的開源項(xiàng)目,其地址在http://poi.apache.org/

下面是起官方網(wǎng)站的標(biāo)題,POI是微軟文檔的javaAPI

Apache POI - the Java API for Microsoft Documents。

下面是一個(gè)POI讀寫word表格的例子:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

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;

public class POIWordUtil {

    public static void main(String[] args) throws Exception {
        Map<String, Text> replaces = new HashMap<String, Text>();

        replaces.put("${username}", Text.str("rongzhi_li"));
        replaces.put("${password}", Text.str("1123456"));
        replaces.put("${author}", Text.str("lee"));

        poiWordTableReplace("t1.doc", "t2.doc", replaces);
    }

    public static void poiWordTableReplace(String sourceFile, String newFile,
            Map<String, Text> replaces) throws Exception {
        FileInputStream in = new FileInputStream(sourceFile);
        HWPFDocument hwpf = new HWPFDocument(in);
        Range range = hwpf.getRange();// 得到文檔的讀取范圍
        TableIterator it = new TableIterator(range);
        // 迭代文檔中的表格
        while (it.hasNext()) {
            Table tb = (Table) it.next();
            // 迭代行,默認(rèn)從0開始
            for (int i = 0; i < tb.numRows(); i++) {
                TableRow tr = tb.getRow(i);
                // 迭代列,默認(rèn)從0開始
                for (int j = 0; j < tr.numCells(); j++) {
                    TableCell td = tr.getCell(j);// 取得單元格
                    // 取得單元格的內(nèi)容
                    for (int k = 0; k < td.numParagraphs(); k++) {
                        Paragraph para = td.getParagraph(k);

                        String s = para.text();
                        final String old = s;
                        for (String key : replaces.keySet()) {
                            if (s.contains(key)) {
                                s = s.replace(key, replaces.get(key).getText());
                            }
                        }
                        if (!old.equals(s)) {// 有變化
                            para.replaceText(old, s);
                            s = para.text();
                            System.out.println("old:" + old + "->" + "s:" + s);
                        }

                    } // end for
                } // end for
            } // end for
        } // end while

        FileOutputStream out = new FileOutputStream(newFile);
        hwpf.write(out);

        out.flush();
        out.close();

    }
}
public abstract class Text {

    public abstract String getText();

    public static Text str(final String string) {
        return new Text() {
            @Override
            public String getText() {
                return string;
            }
        };
    }

}

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:Android中的“再按一次返回鍵退出程序“的代碼

下一篇:Android判斷是否為Wifi網(wǎng)絡(luò)