当前用于 通过java操作excel表格的工具类库 这一目的手段有两种:
1 jxl,一位日本程序员的开源类库。缺点是不能够支持Office 2003以上版本,对图片识别和处理能力仅限于PNG格式。
2 POI, Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。功能强悍、性能稳定。相对与jxl来讲,几乎占尽各方面优势。
采用poi的方法:
1,导入jar包,制作excel表格
poi-3.7
3,jsp上传excel,java并获得
<form id="empForm" >
<input type="file" id="excelPath" name="excelPath"/>
<input type="button" value="导入Excel" οnclick="importEmp()"/>
</form>
js判断文件类型
<script language="javascript">
//Excel文件导入到数据库中
function importEmp(){
//检验导入的文件是否为Excel文件
var excelPath = document.getElementById("excelPath").value;
if(excelPath == null || excelPath == ''){
alert("请选择要上传的Excel文件");
return;
}else{
var fileExtend = excelPath.substring(excelPath.lastIndexOf('.')).toLowerCase();
if(fileExtend == '.xls'){
}else{
alert("文件格式需为'.xls'格式");
return;
}
}
//提交表单
document.getElementById("empForm").action="<%=request.getContextPath()%>/importStus";
document.getElementById("empForm").submit();
}
</script>
struts.xml文件
<package name="tableInsert" extends="struts-default">
<action name="importStus" class="infoManage.action.tabInsertAction" method="tableInsert">
<result name="success">/JSP/Management/_tabInsert.jsp</result>
<result name="error">/JSP/Management/_tabInsert.jsp</result>
</action>
</package>
java文件获得:
//从页面接收参数:文件的路径
HttpServletRequest request = ServletActionContext.getRequest ();
String excelPath = request.getParameter("excelPath");
System.out.print("excel路径"+excelPath);
//输入流
InputStream fis = new FileInputStream("C:/Users/Administrator/Desktop/"+excelPath);
3,将获得的excel文件解析
//POI:得到解析Excel的实体集合
List<student> infos = ImportEmployee.importEmployeeByPoi(fis);
package infoManage.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
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.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
//import com.boxun.bean.ExcelWorkSheet;
//import com.boxun.bean.Userinfo;
//import com.boxun.biz.IUserBiz;
import bean.student;
import com.opensymphony.xwork2.ActionSupport;
public class ImportEmployee {
/**
* POI:解析Excel文件中的数据并把每行数据封装成一个实体
* @param fis 文件输入流
* @return List<EmployeeInfo> Excel中数据封装实体的集合
*/
public static List<student> importEmployeeByPoi(InputStream fis) {
//这里是解析出来的Excel的数据存放的List集合
List<student> infos = new ArrayList<student>(); //这里是解析出来的Excel中的每一条数据封装的实体BEAN.
student student = null;
try {
//创建Excel工作薄
HSSFWorkbook hwb = new HSSFWorkbook(fis);
//得到第一个工作表
HSSFSheet sheet = hwb.getSheetAt(0);
HSSFRow row = null;
//日期格式化
DateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
//遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数
System.out.print( hwb.getNumberOfSheets()+"列数"+sheet.getPhysicalNumberOfRows());
for(int i = 0; i < hwb.getNumberOfSheets(); i++) {
sheet = hwb.getSheetAt(i);
//遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数
for(int j = 1; j < sheet.getPhysicalNumberOfRows(); j++) {
row = sheet.getRow(j);
student = new student();
//此方法调用getCellValue(HSSFCell cell)对解析出来的数据进行判断,并做相应的处理
// if(tabInsertAction.getCellValue(row.getCell(0)) != null && !"".equals(tabInsertAction.getCellValue(row.getCell(0)))) {
// student.setOrgId(Long.valueOf(tabInsertAction.getCellValue(row.getCell(0))));
student.setStudentId(ImportEmployee.getCellValue(row.getCell(0)));
System.out.print("学生学号"+ImportEmployee.getCellValue(row.getCell(0)));
student.setStudentName(ImportEmployee.getCellValue(row.getCell(1)));
student.setDeptId(ImportEmployee.getCellValue(row.getCell(2)));
student.setClassId(ImportEmployee.getCellValue(row.getCell(3)));
student.setPassword(ImportEmployee.getCellValue(row.getCell(4)));
// }
// student.setEmployeeNumber(tabInsertAction.getCellValue(row.getCell(1)));
// student.setFullName(tabInsertAction.getCellValue(row.getCell(2)));
// student.setSex(tabInsertAction.getCellValue(row.getCell(3)));
// if(tabInsertAction.getCellValue(row.getCell(4)) != null && !"".equals(tabInsertAction.getCellValue(row.getCell(4)))) {
// try {
// student.setDateOfBirth(ft.parse(tabInsertAction.getCellValue(row.getCell(4))));
// } catch (ParseException e) {
// e.printStackTrace();
// }
// student.setTownOfBirth(tabInsertAction.getCellValue(row.getCell(5)));
// }
// student.setNationalIdentifier(tabInsertAction.getCellValue(row.getCell(6)));
infos.add(student);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return infos;
}
//判断从Excel文件中解析出来数据的格式
private static String getCellValue(HSSFCell cell){
String value = null;
//简单的查检列类型
switch(cell.getCellType())
{
case HSSFCell.CELL_TYPE_STRING://字符串
value = cell.getRichStringCellValue().getString();
break;
case HSSFCell.CELL_TYPE_NUMERIC://数字
long dd = (long)cell.getNumericCellValue();
value = dd+"";
break;
case HSSFCell.CELL_TYPE_BLANK:
value = "";
break;
case HSSFCell.CELL_TYPE_FORMULA:
value = String.valueOf(cell.getCellFormula());
break;
case HSSFCell.CELL_TYPE_BOOLEAN://boolean型值
value = String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR:
value = String.valueOf(cell.getErrorCellValue());
break;
default:
break;
}
return value;
}
}
此时,excel文件里的数据就解析成了bean数据存在info中
4,遍历info,将数据存入数据库。
//遍历解析Excel的实体集合
for(student info:infos) {
//判断学号是否存在(存在:做修改操作;不存在:做新增操作)
boolean state=tabInsertdao.selectEmpByEmpNum(info.getStudentId()); //判断学生是否存在
String state1=null;
if(state == true) { //该学生存在,修改操作
state1=tabInsertdao.UpdateStu(info);
}else{ //该学生不存在,添加操作
state1=tabInsertdao.addStu(info);
}
setInfoString(state1);
}
//关闭流
fis.close();
return SUCCESS;
有问题QQ:617238902联系