Java读取并导出Word中的表格(Excel),导出文件为Excel

来源:互联网 发布:做淘宝客能挣钱吗2016 编辑:程序博客网 时间:2024/05/29 08:40

看公司的同事很费劲的在一条一条地从Word中的表格复制粘贴到Excel,

我从网上找个两个demo给合在了一起,帮他解决了问题。最下方有源码。

一个两个类

第一个:

package com.wbs.test;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.List;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;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;public class Test {static String fileName = "testFile";public static void main(String[] args) throws IOException {testWord();}public static void testWord(){ArrayList<User> list = new ArrayList<>();list.clear();  try{  //载入文档最好格式为.doc后缀  //.docx后缀文件可能存在问题,可将.docx后缀文件另存为.doc      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);           //迭代文档中的表格      while (it.hasNext()) {                     Table tb = (Table) it.next();// 原本在此处新建 对象User user = new User();// 但导出的数量不对          //迭代行,默认从0开始          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(); //                      System.out.println(s);                      if(s.contains("点位名称")){                      User user = new User();                      list.add(user);                    list.get(list.size()-1).setName(td.getParagraph(k+1).text());                                                                  }                                            if(s.contains("所属区")){                      list.get(list.size()-1).setArea(td.getParagraph(k+1).text());                      }                      if(s.contains("维护日期")){                      list.get(list.size()-1).setDate(td.getParagraph(k+1).text());                                                                  }                                            if(s.contains("维护措施")){                      list.get(list.size()-1).setCuoshi(td.getParagraph(k+1).text());                      }                                                                  //                      System.out.println(s);                  } //end for                  }   //end for          }   //end for          //          list.add(user);               } //end while            createExecl(list);            System.out.println(list.size());       }catch(Exception e){   e.printStackTrace();  } }// end methodpublic static void createExecl(ArrayList<User> list) {// 第一步,创建一个webbook,对应一个Excel文件HSSFWorkbook wb = new HSSFWorkbook();// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheetHSSFSheet sheet = wb.createSheet("sheet1");// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制shortHSSFRow row = sheet.createRow((int) 0);// 第四步,创建单元格,并设置值表头 设置表头居中HSSFCellStyle style = wb.createCellStyle();style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式HSSFCell cell = row.createCell((short) 0);cell.setCellValue("点位名称");cell.setCellStyle(style);cell = row.createCell((short) 1);cell.setCellValue("所属区");cell.setCellStyle(style);cell = row.createCell((short) 2);cell.setCellValue("维护日期");cell.setCellStyle(style);cell = row.createCell((short) 3);cell.setCellValue("维护措施");cell.setCellStyle(style);cell = row.createCell((short) 4);// cell.setCellValue("年龄");// cell.setCellStyle(style);// cell = row.createCell((short) 3);// cell.setCellValue("生日");// cell.setCellStyle(style);for (int i = 0; i < list.size(); i++) {row = sheet.createRow((int) i + 1);User user = (User) list.get(i);// 第四步,创建单元格,并设置值row.createCell((short) 0).setCellValue(user.getName());row.createCell((short) 1).setCellValue(user.getArea());row.createCell((short) 2).setCellValue(user.getDate());row.createCell((short) 3).setCellValue(user.getCuoshi());// row.createCell((short) 2).setCellValue((double) stu.getAge());// cell = row.createCell((short) 3);// cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu// .getBirth()));}// 第六步,将文件存到指定位置try {FileOutputStream fout = new FileOutputStream(fileName + ".xls");// 选中项目右键,点击Refresh,即可显示导出文件wb.write(fout);fout.close();} catch (Exception e) {e.printStackTrace();}}}

第二个:

package com.wbs.test;public class User {String area ;String name;String date;String cuoshi;public String getArea() {return area;}public void setArea(String area) {this.area = area;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getDate() {return date;}public void setDate(String date) {this.date = date;}public String getCuoshi() {return cuoshi;}public void setCuoshi(String cuoshi) {this.cuoshi = cuoshi;}}

下载源码


原创粉丝点击