Java利用poi實(shí)現(xiàn)word表格轉(zhuǎn)excel
一、每行對(duì)象類
需要針對(duì)不同的表格進(jìn)行對(duì)應(yīng)的創(chuàng)建。
package org.example.wordToExcel; /** * @Auther: rll * @Date: 2022/1/11 10:57 * @Description: 每行對(duì)象類 */ 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; } }
二、轉(zhuǎn)換測試
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表格轉(zhuǎn)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);//取得單元格 //取得單元格的內(nèi)容 Field[] fields = aClass.getDeclaredFields(); //遍歷單元格所有內(nèi)容放入一個(gè)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對(duì)象 //反射賦值和取值 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());//去掉最后一個(gè)標(biāo)記符號(hào) sb.delete(0, sb.length()); } list.add(excelBook); } } writeExecl(list); } catch (Exception e) { e.printStackTrace(); } } public static void writeExecl(ArrayList<MyExcelBook> list) throws Exception { //創(chuàng)建一個(gè)webbook,對(duì)應(yīng)一個(gè)Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); //在webbook中添加一個(gè)sheet,對(duì)應(yīng)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(); //創(chuàng)建單元格 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文檔,請(qǐng)使用只有表格且沒有合并表格的進(jìn)行測試,
請(qǐng)使用只有表格且沒有合并表格的進(jìn)行測試,
請(qǐng)使用只有表格且沒有合并表格的進(jìn)行測試。
到此這篇關(guān)于Java利用poi實(shí)現(xiàn)word表格轉(zhuǎn)excel的文章就介紹到這了,更多相關(guān)Java word轉(zhuǎn)excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis中傳遞多個(gè)參數(shù)的4種方法總結(jié)
這篇文章主要給大家介紹了關(guān)于Mybatis中傳遞多個(gè)參數(shù)的4種方法,并且介紹了關(guān)于使用Mapper接口時(shí)參數(shù)傳遞方式,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04如何自定義hibernate validation注解示例代碼
Hibernate Validator 是 Bean Validation 的參考實(shí)現(xiàn) . Hibernate Validator 提供了 JSR 303 規(guī)范中所有內(nèi)置 constraint 的實(shí)現(xiàn),下面這篇文章主要給大家介紹了關(guān)于如何自定義hibernate validation注解的相關(guān)資料,需要的朋友可以參考下2018-04-04Java 將文件轉(zhuǎn)為字節(jié)數(shù)組知識(shí)總結(jié)及實(shí)例詳解
這篇文章主要介紹了Java 將文件轉(zhuǎn)為字節(jié)數(shù)組實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-12-12JavaWeb之Ajax的基本使用與實(shí)戰(zhàn)案例
ajax技術(shù)是使頁面能局部刷新的一種技術(shù),下面這篇文章主要給大家介紹了關(guān)于JavaWeb之Ajax的基本使用與實(shí)戰(zhàn)案例的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08詳解如何使用Java流API構(gòu)建樹形結(jié)構(gòu)數(shù)據(jù)
在實(shí)際開發(fā)中,構(gòu)建樹狀層次結(jié)構(gòu)是常見需求,本文主要為大家詳細(xì)介紹了如何使用Java 8 Stream API將扁平化的菜單數(shù)據(jù)轉(zhuǎn)換為具有層級(jí)關(guān)系的樹形結(jié)構(gòu),需要的可以參考下2024-04-04