- Download the JXL rack package at http://jareye.com/
- operation
Read Workbook: Workbook workbook=Workbook.getWorkbook(new File("myfile.xls");
Read sheet: Sheet sheet=workbook.getSheet(0);
Read cell: Cell c2=sheet.getCell(2,1); / / cell starts from (0,0)
Read the value of cell: 1. Through the getContents method of cell
String string2=c2.getContents();
2.cell provides a getType() method
It can return the type information of cells. At the same time, JXL provides a CellType class to preset the type information in Excel, and JXL provides some subclasses of Cell class to respectively represent various types of cells, such as LabelCell, NumberCell, DateCell to respectively represent the cells of character, value and date type
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();
}
- Release the resource workbook.close() after each operation;
- Write excle operation
4.1 the WritableWorkbook WritableSheet Label can be used to insert the excel file
4.1.1: create excel workbook create worksheet create cell
- Create Workbook: WritableWorkbook wwb=Workbook.createWorkbook(new File("d.xsl"));
- Write WritetableWorkbook directly to the input stream
OutputStream os = new FileOutputStream(targetfile);
WritableWorkbook wwb = Workbook.createWorkbook(os);
-
Create sheet
WritableSheet ws = wwb.createSheet("address book", 0); / / create sheet - Creating Cells
Add text type cell
Label labelC = new Label(0, 0, "This is a Label cell");
ws.addCell(labelC);
///Import, update student data (existing excle table) 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("Student information file uploaded successfully"); } 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();// All rows int clos = rs.getColumns();// All columns // 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("male")) { sex = false; } else if (stuSex.equals("female")) { sex = true; } stuI = new StudentsId(); stuI.setSStudentId(stuId); student = null; student = studentDao.isexist(stuId); if (student != null) {// Update if the student exists in the table 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("The first"+(number++)+"Record"+stuId+"Update success\r\n"); } else {// If the student does not exist in the table, add 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("The first"+(number++)+"Record"+stuId+"Add success\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"; } // Export appointment activity student data public String exportActStudent() { try { System.out.println("List export"); WritableWorkbook wwb = null; // Save in disk d by default 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, "Student ID"); Label laelName = new Label(1, 0, "Full name"); Label labelSchool = new Label(2, 0, "college"); Label labelClass = new Label(3, 0, "class"); Label labelPhone = new Label(4, 0, "Contact information"); 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"; }
Reference: https://blog.csdn.net/lalioCAT/article/details/50580020