- 在http://jareye.com/上下载JXL架包
- 操作
读取工作薄:Workbook workbook=Workbook.getWorkbook(new File(“myfile.xls”));
读取工作表:Sheet sheet=workbook.getSheet(0);
读取单元格:Cell c2=sheet.getCell(2,1);//单元格是从(0,0)开始的
读取单元格的值:1.通过cell的getContents方法
String string2=c2.getContents();
2.cell提供一个getType()方法
能够返回单元格的类型信息,同时JXL提供了一个CellType类用来预设Excel中的类型信息,而且JXL提供了一些Cell类的子类用来分别用来表示各种类型的单元格,如LabelCell,NumberCell,DateCell分别表示字符、数值、日期类型的单元格
if (c2.getType() == CellType. LABEL)
{
LabelCell nc = (LabelCell) c2;
String number b2 = nc. getString();
}
if (c2.getType() == CellType. DATE)
{
DateCell nc = (DateCell) c2;
Date number b2 = nc. getDate();
}
if (c2.getType() == CellType.NUMBER)
{
NumberCell nc = (NumberCell) c2;
double number b2 = nc.getValue();
}
- 每次操作完都要释放资源 workbook.close();
- 写excle操作
4.1通过WritableWorkbook WritableSheet Label可以实现对EXcle文件的插入工作
4.1.1:创建Excle工作薄 创建工作表 创建单元格
- 创建工作薄:WritableWorkbook wwb=Workbook.createWorkbook(new File(“d.xsl”));
- 将WritetableWorkbook直接写入输入流
OutputStream os = new FileOutputStream(targetfile);
WritableWorkbook wwb = Workbook.createWorkbook(os);
- 创建工作表
WritableSheet ws = wwb.createSheet("通讯录", 0);//创建sheet - 创建单元格
添加文本类单元格
Label labelC = new Label(0, 0, "This is a Label cell");
ws.addCell(labelC);
/// 导入,更新学生数据(已经有的excle表格)
public String importExcleStudent() throws IOException {
createTime = getTime();
String path = "/excleFile";
String realpath = ServletActionContext.getServletContext().getRealPath(path);
File target = new File(realpath, importStudentFileName);
FileWriter writer =createFileWirter(realpath+"/importExcleStudent.log");
if (target.exists()) {
target.delete();
}
try {
FileUtils.copyFile(importStudent, target);
System.out.println("学生信息文件上传成功");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FileInputStream fi = new FileInputStream(target);
Workbook rwb = Workbook.getWorkbook(fi);
Sheet rs = rwb.getSheet(0);
int rows = rs.getRows();// 所有行
int clos = rs.getColumns();// 所有列
// System.out.println(rows+","+clos);
Boolean sex = false;
String stuId, stuName = null, stuSex = null, stuSchool = null, stuCollege = null, stuProfession = null,
stuClass = null, stuClassType = null, stuJobstatus = null, stuTutor = null, stuPhone = null,
stuIdcard = null;
StudentsId stuI = null;
Students student = null;
Students stu = null;
int number=1,j=0;
for (int i = 1; i < rows; i++) {
//System.out.println(i+",,"+rows);
j = 0;
stuId = rs.getCell(j++, i).getContents().trim();
stuName = rs.getCell(j++, i).getContents();
stuSex = rs.getCell(j++, i).getContents();
stuSchool = rs.getCell(j++, i).getContents();
stuCollege = rs.getCell(j++, i).getContents();
stuProfession = rs.getCell(j++, i).getContents();
stuClass = rs.getCell(j++, i).getContents();
stuClassType = rs.getCell(j++, i).getContents();
stuJobstatus = rs.getCell(j++, i).getContents();
stuTutor = rs.getCell(j++, i).getContents();
stuPhone = rs.getCell(j++, i).getContents();
stuIdcard = rs.getCell(j++, i).getContents();
if (stuSex.equals("男")) {
sex = false;
} else if (stuSex.equals("女")) {
sex = true;
}
stuI = new StudentsId();
stuI.setSStudentId(stuId);
student = null;
student = studentDao.isexist(stuId);
if (student != null) {// 表中存在该学生,则更新
stuI.setSId(student.getId().getSId());
stu = new Students(stuI, stuName, sex, stuSchool, stuCollege, stuProfession, stuClass,
stuClassType, stuJobstatus, stuTutor, stuPhone, stuIdcard,
MD5Util.MD5(stuId.substring(stuId.length() - 6)));
// System.out.println(stuId + "--" + stuId.length() + ">>" + stuId.substring(stuId.length() - 6));
stu.setSCreateTime(student.getSCreateTime());
stu.setSChangeTime(getTime());
studentDao.updatestudent(stu);
writer.write("第"+(number++)+"记录"+stuId+"更新成功\r\n");
} else {// 表中不存在该学生,则添加
System.out.println(stuId + "--" + stuId.length() + ">>" + stuId.substring(stuId.length() - 6));
stu = new Students(stuI, stuName, sex, stuSchool, stuCollege, stuProfession, stuClass,
stuClassType, stuJobstatus, stuTutor, stuPhone, stuIdcard,
MD5Util.MD5(stuId.length()>6?stuId.substring(stuId.length() - 6):("1234567"+stuId).substring(("1234567"+stuId).length() - 6)));
stu.setSCreateTime(createTime);
stu.setSChangeTime(createTime);
stu.setSPicture("https://sdsy.zzjc.edu.cn/SDSYw/image/1.jpg");
studentDao.addstudent(stu);
writer.write("第"+(number++)+"记录"+stuId+"添加成功\r\n");
}
writer.flush();
}
writer.close();
fi.close();
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e){
System.out.println(e);
}finally{
}
s = JSONTools.createJsonObject("s", "OK");
return "success";
}
// 导出预约活动学生数据
public String exportActStudent() {
try {
System.out.println("名单导出");
WritableWorkbook wwb = null;
// 默认保存在d盘
String path = ServletActionContext.getServletContext().getRealPath("/excleFile");
File file = new File(path + "/mingdan.xls");
// System.out.println(file);
if (!file.exists()) {
file.createNewFile();
}
wwb = Workbook.createWorkbook(file);
WritableSheet ws = wwb.createSheet("sheet0", 0);
List<Activiting> list = actRecordDao.JionActStudent(id);
Label labelSId = new Label(0, 0, "学号");
Label laelName = new Label(1, 0, "姓名");
Label labelSchool = new Label(2, 0, "学院");
Label labelClass = new Label(3, 0, "班级");
Label labelPhone = new Label(4, 0, "联系方式");
ws.addCell(labelSId);
ws.addCell(laelName);
ws.addCell(labelSchool);
ws.addCell(labelClass);
ws.addCell(labelPhone);
int i = 1;
for (Activiting actR : list) {
ws.addCell(new Label(0, i, actR.getId().getASId()));
ws.addCell(new Label(1, i, actR.getStudent().getSName()));
ws.addCell(new Label(2, i, actR.getStudent().getSCollege()));
ws.addCell(new Label(3, i, actR.getStudent().getSClass()));
ws.addCell(new Label(4, i++, actR.getStudent().getSPhone()));
}
wwb.write();
wwb.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
s = JSONTools.createJsonObject("data", "OK");
return "success";
}
参考:https://blog.csdn.net/lalioCAT/article/details/50580020