Skip to content

Instantly share code, notes, and snippets.

@Phonbopit
Created June 21, 2013 12:49
Show Gist options
  • Save Phonbopit/5830930 to your computer and use it in GitHub Desktop.
Save Phonbopit/5830930 to your computer and use it in GitHub Desktop.
อ่านข้อมูลจากไฟล์ excel ด้วย Java โดยใช้ jXLS @see http://devsharing.com/2013/java/java-jxls-file-reader/
package com.devsharing.example.jxls;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import net.sf.jxls.reader.ReaderBuilder;
import net.sf.jxls.reader.ReaderConfig;
import net.sf.jxls.reader.XLSDataReadException;
import net.sf.jxls.reader.XLSReadStatus;
import net.sf.jxls.reader.XLSReader;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.xml.sax.SAXException;
public class JxlsReader {
static String pathXML = "res/report/student-config.xml";
static String pathXLS = "res/template/Student-Import.xls";
public XLSReadStatus importFile(Map<String, Object> model) {
ReaderConfig.getInstance().setSkipErrors(true);
InputStream inputXML = null;
InputStream inputXLS = null;
try {
inputXML = new FileInputStream(new File(pathXML));
inputXLS = new FileInputStream(new File(pathXLS));
XLSReader mainReader = ReaderBuilder.buildFromXML( inputXML );
mainReader.read( inputXLS, model);
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (XLSDataReadException e) {
e.printStackTrace();
}finally {
try {
inputXML.close();
inputXLS.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class MainApp {
public static void main(String[] args) {
boolean state = true;
Scanner scan = new Scanner(System.in);
do{
System.out.println("Choose option below ");
System.out.println("1 : jXLS Writer");
System.out.println("2 : jXLS Reader");
switch (scan.nextInt()) {
case 1:
jXLSWriter();
state = false;
break;
case 2:
jXLSReader();
state = false;
break;
default:
System.out.println("Please choose only 1 or 2");
}
}while(state);
scan.close();
}
}
public static void jXLSReader () {
List studentList = new ArrayList();
Map<String, Object> model = new HashMap<String, Object>();
model.put("studentList", studentList);
JxlsReader reader = new JxlsReader();
reader.importFile(model);
for (Student stu : studentList) {
System.out.println( stu.getId() + " " +
stu.getName() + " " +
stu.getLastName() + " " +
stu.getAddress());
}
}
public static void jXLSWriter () {
//Initial student
Student student = new Student(1,"Somsak","Somboon","Bangkok");
Student student2 = new Student(2,"Peter","Ayothaya","ChiangMai");
Student student3 = new Student(3,"Jack","Sparrow","savvy");
//Create List of student
List studentList = new ArrayList();
studentList.add(student);
studentList.add(student2);
studentList.add(student3);
Map<String, Object> modelList = new HashMap<String, Object>();
modelList.put("student", studentList);
JxlsReport jxls = new JxlsReport();
String status = jxls.export(modelList);
System.out.println(status);
}
package com.devsharing.example.jxls;
public class Student {
int id;
String name;
String lastName;
String address;
//Constructor
public Student(int id, String name, String lastName, String address) {
this.id = id;
this.name = name;
this.lastName = lastName;
this.address = address;
}
public Student() {
// TODO Auto-generated constructor stub
}
//Getter and Setter
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
<?xml version="1.0" encoding="utf-8"?>
<workbook>
<worksheet name="Student">
<section startRow="0" endRow="1">
</section>
<loop startRow="2" endRow="2" items="studentList" var="student" varType="com.devsharing.example.jxls.Student">
<section startRow="2" endRow="2">
<mapping row="2" col="0">student.id</mapping>
<mapping row="2" col="1">student.name</mapping>
<mapping row="2" col="2">student.lastName</mapping>
<mapping row="2" col="3">student.address</mapping>
</section>
<loopbreakcondition>
<rowcheck offset="0">
<cellcheck offset="0">-STOP-</cellcheck>
</rowcheck>
</loopbreakcondition>
</loop>
</worksheet>
</workbook>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment