J-Excel萬能Excel導(dǎo)入導(dǎo)出工具
J-Excel 是萬能的 Excel 導(dǎo)入導(dǎo)出工具:
支持從List<Map>中導(dǎo)出
支持從List<POJO>中導(dǎo)入導(dǎo)出
支持從List<POJO里面還有List<POJO>>中導(dǎo)入導(dǎo)出
支持導(dǎo)出類似課程表結(jié)構(gòu)類型縱表
支持國際化
不寫一個(gè)配置文件!
示例請(qǐng)參照:
public class TestExcel {
static {
SimpleConfigurator.addConfigurator(new DbConfig("jdbc:mysql://localhost/digitalcampus?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8&autoReconnect=true", DbConfig.DEFAULT_CONFIG_NAME));
}
/**
* 從List<Map>中導(dǎo)出
* @param workbook
* @throws Exception
*/
public static void testSimpleMapExport(Workbook workbook) throws Exception {
Hyberbin hyberbin = new Hyberbin();
List<Map> list = hyberbin.getMapList("select * from dc_xxkc");
Sheet sheet = workbook.createSheet("testSimpleMapExport");
List<String> cols = new ArrayList<String>();
List<FieldColumn> fieldColumns = hyberbin.getFieldColumns();
for (FieldColumn column : fieldColumns) {
cols.add(column.getColumn());
}
SimpleExportService service = new SimpleExportService(sheet, list, (String[]) cols.toArray(new String[]{}), "學(xué)校課程");
service.setDic("KCLX", "KCLX").addDic("KCLX", "1", "國家課程").addDic("KCLX", "2", "學(xué)校課程");//設(shè)置數(shù)據(jù)字典
service.doExport();
}
/**
* 從List<Vo>中導(dǎo)出
* @param workbook
* @throws Exception
*/
public static void testSimpleVoExport(Workbook workbook) throws Exception {
Hyberbin<SchoolCourse> hyberbin = new Hyberbin(new SchoolCourse());
List<SchoolCourse> list = hyberbin.showAll();
Sheet sheet = workbook.createSheet("testSimpleVoExport");
//ExportExcelService service = new ExportExcelService(list, sheet, "學(xué)校課程");
ExportExcelService service = new ExportExcelService(list, sheet, new String[]{"id", "courseName", "type"}, "學(xué)校課程");
service.addDic("KCLX", "1", "國家課程").addDic("KCLX", "2", "學(xué)校課程");//設(shè)置數(shù)據(jù)字典
service.doExport();
}
/**
* 從List<Vo>,vo中還有簡單循環(huán)節(jié)中導(dǎo)出
* @param workbook
* @throws Exception
*/
public static void testVoHasListExport(Workbook workbook) throws Exception {
Hyberbin<SchoolCourse> hyberbin = new Hyberbin(new SchoolCourse());
List<SchoolCourse> list = hyberbin.showAll();
List<String> strings = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
strings.add("我是第" + i + "個(gè)循環(huán)字段");
}
for (SchoolCourse course : list) {
course.setBaseArray(strings);
}
Sheet sheet = workbook.createSheet("testVoHasListExport");
ExportExcelService service = new ExportExcelService(list, sheet, new String[]{"id", "courseName", "type", "baseArray"}, "學(xué)校課程");
service.addDic("KCLX", "1", "國家課程").addDic("KCLX", "2", "學(xué)校課程");//設(shè)置數(shù)據(jù)字典
service.setGroupConfig("baseArray", new GroupConfig(10) {
@Override
public String getLangName(int innerIndex, int index) {
return "我是第" + index + "個(gè)循環(huán)字段";
}
});
service.doExport();
service.exportTemplate();//生成下拉框
}
/**
* 從List<Vo>,vo中還有復(fù)雜循環(huán)節(jié)中導(dǎo)出
* @param workbook
* @throws Exception
*/
public static void testVoHasListVoExport(Workbook workbook) throws Exception {
Hyberbin<SchoolCourse> hyberbin = new Hyberbin(new SchoolCourse());
List<SchoolCourse> list = hyberbin.showAll();
for (SchoolCourse course : list) {
List<InnerVo> innerVos = new ArrayList<InnerVo>();
for (int i = 0; i < 10; i++) {
innerVos.add(new InnerVo("key1", "value1"));
}
course.setInnerVoArray(innerVos);
}
Sheet sheet = workbook.createSheet("testVoHasListVoExport");
ExportExcelService service = new ExportExcelService(list, sheet, new String[]{"id", "courseName", "type", "innerVoArray"}, "學(xué)校課程");
service.addDic("KCLX", "1", "國家課程").addDic("KCLX", "2", "學(xué)校課程");//設(shè)置數(shù)據(jù)字典
for (int i = 0; i < 10; i++) {
service.addTook("hiddenvalue", "key", i, "something");
}
service.setGroupConfig("innerVoArray", new GroupConfig(2, 10) {
@Override
public String getLangName(int innerIndex, int index) {
return "我是第" + index + "個(gè)循環(huán)字段,第" + innerIndex + "個(gè)屬性";
}
});
service.doExport();
}
/**
* 導(dǎo)出一個(gè)縱表(課程表之類的)
* @param workbook
* @throws Exception
*/
public static void testTableExport(Workbook workbook) throws Exception {
Sheet sheet = workbook.createSheet("testTableExport");
TableBean tableBean = new TableBean(3, 3);
Collection<CellBean> cellBeans = new HashSet<CellBean>();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
CellBean cellBean = new CellBean(i * 3 + j + "", i, j);
cellBeans.add(cellBean);
}
}
tableBean.setCellBeans(cellBeans);
ExportTableService tableService = new ExportTableService(sheet, tableBean);
tableService.doExport();
}
/**
* 從List<Vo>中入
* @param workbook
* @throws Exception
*/
public static void testSimpleVoImport(Workbook workbook) throws Exception {
Sheet sheet = workbook.getSheet("testSimpleVoExport");
ImportExcelService service = new ImportExcelService(SchoolCourse.class, sheet);
service.addDic("KCLX", "1", "國家課程").addDic("KCLX", "2", "學(xué)校課程");//設(shè)置數(shù)據(jù)字典
List list = service.doImport();
System.out.println("成功導(dǎo)入:" + list.size() + "條數(shù)據(jù)");
}
/**
* 從List<Vo>,vo中還有簡單循環(huán)節(jié)中導(dǎo)入
* @param workbook
* @throws Exception
*/
public static void testVoHasListImport(Workbook workbook) throws Exception {
Sheet sheet = workbook.getSheet("testVoHasListExport");
ImportExcelService service = new ImportExcelService(SchoolCourse.class, sheet);
service.addDic("KCLX", "1", "國家課程").addDic("KCLX", "2", "學(xué)校課程");//設(shè)置數(shù)據(jù)字典
List list = service.doImport();
System.out.println("成功導(dǎo)入:" + list.size() + "條數(shù)據(jù)");
}
/**
* 從List<Vo>,vo中還有復(fù)雜循環(huán)節(jié)中導(dǎo)入
* @param workbook
* @throws Exception
*/
public static void testVoHasListVoImport(Workbook workbook) throws Exception {
Sheet sheet = workbook.getSheet("testVoHasListVoExport");
ImportExcelService service = new ImportExcelService(SchoolCourse.class, sheet);
service.addDic("KCLX", "1", "國家課程").addDic("KCLX", "2", "學(xué)校課程");//設(shè)置數(shù)據(jù)字典
List list = service.doImport();
System.out.println("成功導(dǎo)入:" + list.size() + "條數(shù)據(jù)");
}
public static void main(String[] args) throws Exception {
Workbook workbook = new HSSFWorkbook();
testSimpleMapExport(workbook);
testSimpleVoExport(workbook);
testVoHasListExport(workbook);
testVoHasListVoExport(workbook);
testTableExport(workbook);
testSimpleVoImport(workbook);
testVoHasListImport(workbook);
testVoHasListVoImport(workbook);
workbook.write(new FileOutputStream("D:\\excel.xls"));
}
}
評(píng)論
圖片
表情
