Java利用poi實現(xiàn)word表格轉(zhuǎn)excel
一、每行對象類
需要針對不同的表格進行對應的創(chuàng)建。
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;
}
}二、轉(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)容放入一個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 {
//創(chuàng)建一個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();
//創(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文檔,請使用只有表格且沒有合并表格的進行測試,
請使用只有表格且沒有合并表格的進行測試,
請使用只有表格且沒有合并表格的進行測試。
到此這篇關(guān)于Java利用poi實現(xiàn)word表格轉(zhuǎn)excel的文章就介紹到這了,更多相關(guān)Java word轉(zhuǎn)excel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis中傳遞多個參數(shù)的4種方法總結(jié)
這篇文章主要給大家介紹了關(guān)于Mybatis中傳遞多個參數(shù)的4種方法,并且介紹了關(guān)于使用Mapper接口時參數(shù)傳遞方式,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。2018-04-04
如何自定義hibernate validation注解示例代碼
Hibernate Validator 是 Bean Validation 的參考實現(xiàn) . Hibernate Validator 提供了 JSR 303 規(guī)范中所有內(nèi)置 constraint 的實現(xiàn),下面這篇文章主要給大家介紹了關(guān)于如何自定義hibernate validation注解的相關(guān)資料,需要的朋友可以參考下2018-04-04
Java 將文件轉(zhuǎn)為字節(jié)數(shù)組知識總結(jié)及實例詳解
這篇文章主要介紹了Java 將文件轉(zhuǎn)為字節(jié)數(shù)組實例詳解的相關(guān)資料,需要的朋友可以參考下2016-12-12
詳解如何使用Java流API構(gòu)建樹形結(jié)構(gòu)數(shù)據(jù)
在實際開發(fā)中,構(gòu)建樹狀層次結(jié)構(gòu)是常見需求,本文主要為大家詳細介紹了如何使用Java 8 Stream API將扁平化的菜單數(shù)據(jù)轉(zhuǎn)換為具有層級關(guān)系的樹形結(jié)構(gòu),需要的可以參考下2024-04-04

