You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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();
}
}
}

Powered by TurnKey Linux.