有选择读取word表格中的数据并写入excel文件中

  • 最近学院要举行科技报告会活动,许多同学积极参与,交了许多报名表到我这里,而我需要将这些信息进行汇总,整理出一个excel表格,看着一个个word文件放在我的工作文件夹中头发发麻,这一个个的整理好麻烦,不但速度慢而且容易出错,我就想用编程自动替我读取数据并写入目标文件。结果还真让我写成了,下面一一讲解步骤:

  • 问题描述:如下图所示,我需要提取姓名、学号、合作者、学院及专业、联系方式、指导老师、参选题目这几栏的信息。
    有选择读取word表格中的数据并写入excel文件中_第1张图片

  • 步骤一:下载POI插件
    POI:下载地址;我所使用的是poi-3.10-FINAL。建好java工程后右键点击,选择配置选项,按照下面图片步骤。
    有选择读取word表格中的数据并写入excel文件中_第2张图片
    有选择读取word表格中的数据并写入excel文件中_第3张图片
    有选择读取word表格中的数据并写入excel文件中_第4张图片

  • 使用方法
    有选择读取word表格中的数据并写入excel文件中_第5张图片

  • 代码解释

package readInformation;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.POIXMLProperties.CoreProperties;
import org.apache.poi.hssf.usermodel.HSSFCell;
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;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;


/*
 * function:读取参赛作品登记表中的姓名、学号、联系方式、专业、合作者、参选题目、指导老师
 * 
 * 
 * 使用这段代码需要两个前提条件:
 * 	1:加载poi-3.10-FINAL文件夹中的jar扩展包
 * 	2:word版本必须在2007以上,格式和样本一样
 * 这两步可以看使用手册
 */

public class readFile {
	private static FileOutputStream out = null ;
	private static FileInputStream in = null ;
	private static String filePathDir = "C:\\Users\\wangjunzuo\\Desktop\\ll";  //word文件存放的文件夹 绝对路径
	public static HSSFWorkbook workbook = null;
	
	/*
	 * 进行文件的第一行便签写入,就是excel文件的第一行:姓名、学号、联系方式等等
	 */
	public static void creatLables(String[] labels,String worksheet) throws IOException {
		out = new FileOutputStream("result.xls"); 
		workbook = new HSSFWorkbook();
		HSSFSheet sheet1 = workbook.createSheet(worksheet); 
		HSSFRow rw = workbook.getSheet(worksheet).createRow(0);
		for(int i=0;i > people = new ArrayList() ;
		FileOutputStream out = new FileOutputStream("result.xls");  //结果输出文件名
		File file = new File(filePathDir);
		File[] files = file.listFiles();  //获取文件夹下word文件名列表
		
		for(File a:files) {
			System.out.println(a.toString());
			in = new FileInputStream(a);   //依次读入每一个word文件
			XWPFDocument doc = new XWPFDocument(in);
			List tables = doc.getTables();
			for (XWPFTable table : tables) {
			    List rows = table.getRows();   // 获取表格的行
			    ArrayList single = new ArrayList();  //储存每一表格的所有单元信息
			    for (XWPFTableRow row : rows) {
			        List tableCells = row.getTableCells();
			        for (XWPFTableCell cell : tableCells) {
			             String text = cell.getText();
			             single.add(text);
			        }//end for
			    }//end for
			    people.add(single);
			}//end for		
		}
		
		for(int i=0;i

你可能感兴趣的:(数据结构及算法)