利用正则表达式提取txt文件形成一个xls文件保存下来

目的是把txt文件里面的课程名字、授课老师、课程目录、课程简介爬取下来,然后在excel表格里显示出来。利用前面的正则表达式,再加上一些jxl包的东西。注意,要想形成xls文件要下载一个jxl包才行,网上应该有的找,我不记得在哪里下载的了,嘻嘻.
放代码:

/**   
* Title: test9 
* Description:     
* Copyright: Copyright (c) 20192019 
* Company:广州大学-计算机科学与技术 
* Makedate:2019年8月27日 下午4:05:52 
* @author:李松涛 * 
* @version 1.0
* @since 1.0 
**/

package test9;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;

public class sample {

	public static void main(String[] args) throws IOException {

		HSSFWorkbook wb = new HSSFWorkbook();// 创建一个excel文件
		// 建立新的excel的sheet对象(excel的表单)
		HSSFSheet sheet = wb.createSheet("课程信息");

		// 在sheet里创建第一行
		sheet.setColumnWidth(0, 5000);// 表示第一个格子的宽度
		sheet.setColumnWidth(1, 2500);// 表示第二个格子的宽度
		sheet.setColumnWidth(2, 2500);
		sheet.setColumnWidth(3, 2500);

		FileInputStream file = new FileInputStream(
				"D://Desktop//项目//数据库//2013版理论教学大纲汇总(计算机专业)-修改稿66门[系统导出2015-3-12].txt");// 要提取的文件的位置
		BufferedInputStream Bfile = new BufferedInputStream(file);
		byte[] b = new byte[1024];
		String result = "";
		int bytesRead = 0;
		while ((bytesRead = Bfile.read(b)) != -1) {
			result += new String(b, 0, bytesRead);
		}
		// 下面是正则表达式设置前后点
		Pattern course = Pattern.compile("(?s)(?<=课程名称).*?(?=课)");
		Pattern catalog = Pattern.compile("(?s)(?<=二、课程的教学内容、重点和难点).*?(?=三、学时分配)");
		Pattern teacher = Pattern.compile("(?s)(?<=执笔者).*?(?=审)");
		Pattern synopsis = Pattern.compile("(?s)(?<=课程教学大纲).*?(?=一、教学大纲说明)");

		Matcher matcher1 = course.matcher(result);// 课程名字
		Matcher matcher2 = teacher.matcher(result);// 授课老师
		Matcher matcher3 = catalog.matcher(result);// 课程目录
		Matcher matcher4 = synopsis.matcher(result);// 课程简介

		HSSFRow row = null;
		int i = 0;

		while (matcher1.find()) {

			row = sheet.createRow(i);//创建第i+1行,0就是第一行,1就是第二行
			if (i == 0) {
				row.createCell(0).setCellValue("课程名字");//createCell(i)表示创建第i+1个格子,0就是从左到右第一个格子,1就是第二个
			} else {
				row.createCell(0).setCellValue(matcher1.group());//setCellValue()就是excel格子里面的值

			}

			if (matcher2.find()) {
				if (i == 0) {
					row.createCell(1).setCellValue("授课老师");
				} else {
					row.createCell(1).setCellValue(matcher2.group());

				}
				if (matcher3.find()) {
					if (i == 0) {
						row.createCell(2).setCellValue("课程目录");
					} else {
						row.createCell(2).setCellValue(matcher3.group());

					}
					if (matcher4.find()) {
						if (i == 0) {
							row.createCell(3).setCellValue("课程大纲");
						} else {
							row.createCell(3).setCellValue(matcher4.group());

						}
					}
					++i;
				}
				FileOutputStream output = new FileOutputStream("D:/Desktop/test9.xls");//导出来的xls文件的名字
				wb.write(output);// 写入磁盘
				output.close();

			}

		}
	}
}

大致如此,详细说明看注解哈

你可能感兴趣的:(日常记录)