当前位置: 代码网 > it编程>编程语言>Java > Java利用poi实现word表格转excel

Java利用poi实现word表格转excel

2025年03月20日 Java 我要评论
一、每行对象类需要针对不同的表格进行对应的创建。package org.example.wordtoexcel;/** * @auther: rll * @date: 2022/1/11 10:57

一、每行对象类

需要针对不同的表格进行对应的创建。

package org.example.wordtoexcel;
/**
 * @auther: rll
 * @date: 2022/1/11 10:57
 * @description: 每行对象类
 */
public class myexcelbook {
 
    private string xuhao;
 
    private string yingyongming;
 
    private string yibenzhang;
 
    private string jianshu;
 
    private string jianshe;
 
    private string jixiao;
 
    private string jindu;
 
    private string qiantou;
 
    private string canyu;
 
    public string getxuhao() {
        return xuhao;
    }
 
    public void setxuhao(string xuhao) {
        this.xuhao = xuhao;
    }
 
    public string getyingyongming() {
        return yingyongming;
    }
 
    public void setyingyongming(string yingyongming) {
        this.yingyongming = yingyongming;
    }
 
    public string getyibenzhang() {
        return yibenzhang;
    }
 
    public void setyibenzhang(string yibenzhang) {
        this.yibenzhang = yibenzhang;
    }
 
    public string getjianshu() {
        return jianshu;
    }
 
    public void setjianshu(string jianshu) {
        this.jianshu = jianshu;
    }
 
    public string getjianshe() {
        return jianshe;
    }
 
    public void setjianshe(string jianshe) {
        this.jianshe = jianshe;
    }
 
    public string getjixiao() {
        return jixiao;
    }
 
    public void setjixiao(string jixiao) {
        this.jixiao = jixiao;
    }
 
    public string getjindu() {
        return jindu;
    }
 
    public void setjindu(string jindu) {
        this.jindu = jindu;
    }
 
    public string getqiantou() {
        return qiantou;
    }
 
    public void setqiantou(string qiantou) {
        this.qiantou = qiantou;
    }
 
    public string getcanyu() {
        return canyu;
    }
 
    public void setcanyu(string canyu) {
        this.canyu = canyu;
    }
}

二、转换测试

package org.example.wordtoexcel;
 
import java.beans.propertydescriptor;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.lang.reflect.field;
import java.lang.reflect.method;
import java.util.arraylist;
 
import org.apache.poi.hssf.usermodel.hssfrow;
import org.apache.poi.hssf.usermodel.hssfsheet;
import org.apache.poi.hssf.usermodel.hssfworkbook;
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;
 
/**
 * @auther: rll
 * @date: 2022/1/11 15:57
 * @description: word表格转excel
 */
public class readwordtowriteexcel {
 
    static string filename = "c:\\users\\desktop\\testfile";
 
    public static void main(string[] args) throws exception {
        readword();
    }
 
    public static void readword() {
 
        arraylist<myexcelbook> list = new arraylist<>();
        list.clear();
        try {
 
            fileinputstream in = new fileinputstream(filename + ".doc");//载入文档
            poifsfilesystem pfs = new poifsfilesystem(in);
            hwpfdocument hwpf = new hwpfdocument(pfs);
            range range = hwpf.getrange();
            tableiterator it = new tableiterator(range);
 
            //遍历word
            while (it.hasnext()) {
                table tb = it.next();
                //遍历行
                for (int i = 0; i < tb.numrows(); i++) {
                    tablerow tr = tb.getrow(i);
                    //迭代列
                    myexcelbook excelbook = new myexcelbook();
                    class aclass = excelbook.getclass();
                    for (int j = 0; j < tr.numcells(); j++) {
                        tablecell td = tr.getcell(j);//取得单元格
                        //取得单元格的内容
                        field[] fields = aclass.getdeclaredfields();
 
                        //遍历单元格所有内容放入一个sb
                        stringbuilder sb = new stringbuilder();
                        for (int k = 0; k < td.numparagraphs(); k++) {
                            paragraph para = td.getparagraph(k);
                            string s = para.text();
                            sb.append(s);
                        }
                        //反射遍历excel字段,赋值给book对象
                        //反射赋值和取值
                        field field = fields[j];
                        string fieldname = field.getname();
                        propertydescriptor pd = new propertydescriptor(field.getname(),
                                aclass);
 
                        // 获取set方法
                        method setmethod = pd.getwritemethod();
                        setmethod.invoke(excelbook, sb.deletecharat(sb.length() - 1).tostring());//去掉最后一个标记符号
 
                        sb.delete(0, sb.length());
                    }
                    list.add(excelbook);
                }
            }
 
            writeexecl(list);
 
        } catch (exception e) {
            e.printstacktrace();
        }
    }
 
    public static void writeexecl(arraylist<myexcelbook> list) throws exception {
        //创建一个webbook,对应一个excel文件
        hssfworkbook wb = new hssfworkbook();
        //在webbook中添加一个sheet,对应excel文件中的sheet
        hssfsheet sheet = wb.createsheet("sheet1");
 
        hssfrow row = null;
 
        for (int i = 0; i < list.size(); i++) {
            //在sheet中添加行
            row = sheet.createrow(i);
            myexcelbook excelbook = list.get(i);
 
            //反射取值
            class aclass = excelbook.getclass();
            field[] fields = aclass.getdeclaredfields();
            for (int j = 0; j < fields.length; j++) {
                field field = fields[j];
                string fieldname = field.getname();
                propertydescriptor pd = new propertydescriptor(fieldname,
                        aclass);
                //get方法
                method getmethod = pd.getreadmethod();
                //创建单元格
                row.createcell(j).setcellvalue(string.valueof(getmethod.invoke(excelbook)));
            }
 
        }
        //生成文件
        try {
            fileoutputstream fout = new fileoutputstream(filename + ".xls");
            wb.write(fout);
            fout.close();
        } catch (exception e) {
            e.printstacktrace();
        }
 
    }
 
}

测试的word文档,请使用只有表格且没有合并表格的进行测试,

请使用只有表格且没有合并表格的进行测试,

请使用只有表格且没有合并表格的进行测试。

到此这篇关于java利用poi实现word表格转excel的文章就介绍到这了,更多相关java word转excel内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com