Java操作Excel表(二)学生信息管理系统
上一期
前言
上一期查看了源码并且实现了读和写的操作以后,这一期就直接拿来做项目了。其中最经典的项目就是学生信息管理系统,有IO实现的,也有SQL实现的,今天这里就是要用Excel来替代前两种方式来实现。
建表
这里给出四个字段,然后代码里面建立相应的实体类
字符串模糊查询辅助方法
这里提到过 public boolean matchStringByIndexOf(String str,String part) {
int count = 0;
int index = 0;
while( ( index = str.indexOf(part, index) ) != -1 )
{
index = index+part.length();
count++;
}
if(count < 1){
return false;
}
return true;
}
全代码
package com.alvin.service.impl;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import com.alvin.domain.ZStudent;
import com.alvin.service.StringUtilService;
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.poifs.filesystem.POIFSFileSystem;
import com.alvin.service.ExcelService;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.WriteException;
import jxl.Sheet;
public class ExcelServiceImpl implements ExcelService{
private String fileName = "E:/ExcelProject/student.xls";
private StringUtilService stringUtilService = new StringUtilServiceImpl();
/**
* 添加数据
* @throws IOException
* @throws WriteException
*/
public void writeExcel(ZStudent zStudent) throws Exception {
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(fileName);
POIFSFileSystem poifsFileSystem=new POIFSFileSystem(fileInputStream); //使用POI提供的方法得到excel的信息
HSSFWorkbook workbook=new HSSFWorkbook(poifsFileSystem);