|
|
import com.wb.excel.api.WSheet;
|
|
|
import com.wb.excel.api.exception.TemplateNotMatchException;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* 实例
|
|
|
*/
|
|
|
public class ExampleTest {
|
|
|
|
|
|
public static void main(String[] args) throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
|
|
|
// testTemplate();
|
|
|
// testExport();
|
|
|
testImport();
|
|
|
}
|
|
|
|
|
|
public static void testTemplate() {
|
|
|
try {
|
|
|
WSheet WSheet = new WSheet<>(User.class);
|
|
|
output("user_template.xlsx", WSheet.getBytes());
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static void testExport() {
|
|
|
|
|
|
try {
|
|
|
//第一步,准备数据模型及数据,模型需要打上注解
|
|
|
List<User> pos = new ArrayList();
|
|
|
|
|
|
User user = new User();
|
|
|
user.setWz("张三");
|
|
|
user.setDate(new Date());
|
|
|
user.setBr(true);
|
|
|
user.setBt((byte) 1);
|
|
|
user.setZf('A');
|
|
|
user.setDzs((short) 1);
|
|
|
user.setZs(1);
|
|
|
user.setCzs((long) 1);
|
|
|
user.setFd(1.0f);
|
|
|
user.setSjd(1.0);
|
|
|
pos.add(user);
|
|
|
pos.add(user);
|
|
|
|
|
|
//第二步,初始化数据
|
|
|
WSheet<User> WSheet = new WSheet<>(pos);
|
|
|
|
|
|
//第四步,导出xlsx文件
|
|
|
output("user.xlsx", WSheet.getBytes());
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (IllegalAccessException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (InvocationTargetException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static void testImport() {
|
|
|
try {
|
|
|
File file = new File("user.xlsx");
|
|
|
FileInputStream stream = new FileInputStream(file);
|
|
|
|
|
|
byte[] bytes = new byte[stream.available()];
|
|
|
stream.read(bytes);
|
|
|
|
|
|
WSheet<User> WSheet = new WSheet<>(bytes, User.class);
|
|
|
|
|
|
if (WSheet.hasError()) {
|
|
|
System.out.println("数据出现非法值");
|
|
|
output("user_err.xlsx", WSheet.getBytes(true));
|
|
|
} else {
|
|
|
System.out.println("数据全部通过");
|
|
|
List<User> list = WSheet.transferList(User.class);
|
|
|
System.out.println("本次读取数据" + list.size() + "条!");
|
|
|
for (User user : list) {
|
|
|
System.out.println(user);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (IllegalAccessException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (InvocationTargetException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (InstantiationException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (TemplateNotMatchException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static void output(String path, byte[] bytes) {
|
|
|
try {
|
|
|
File file = new File(path);
|
|
|
file.createNewFile();
|
|
|
|
|
|
FileOutputStream fileOutputStream = null;
|
|
|
try {
|
|
|
fileOutputStream = new FileOutputStream(file);
|
|
|
fileOutputStream.write(bytes);
|
|
|
} finally {
|
|
|
try {
|
|
|
fileOutputStream.flush();
|
|
|
fileOutputStream.close();
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
}
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|