/**
* 方法说明:批量导出通用方法
* 创建时间:2018年8月24日 ***
* @param filePath 文件地址
* @param sheetName 分页名称
* @param title Excel文件表头(各个属性中文名称)
* @param propertys 导出数据对象实体类的属性名称,首字母大写
* @param conList 导出的数据集合
* @return
*/
public static boolean exportExcel(String filePath, String sheetName, String[] title, String[] propertys, List<Object> conList) {

try {

if (title != null && title.length > 0 && sheetName != null && !"".equals(sheetName)) {

// 导出excel标题
// String[] title = { "基础基地课堂教学质量评价计算", "序号", "姓名", "职务", "系部", "教研室", "领导评价成绩", "同行评价成绩", "专家评价成绩", "学员评价成绩", "最终成绩", "评价档次"};

// 创建成成excel工具类
WritableWorkbook wwb;

// 获取输出流
OutputStream os = new FileOutputStream(filePath);
wwb = Workbook.createWorkbook(os);

// 生成excel文件wwb.createSheet(excel文件名, 第几页);
WritableSheet sheet = wwb.createSheet(sheetName, 0);

// Label是单元格内容的Model
Label label = null;

// jxl 是专门操作excel的工具类
jxl.write.WritableFont wfont = new jxl.write.WritableFont(WritableFont.createFont("微软雅黑"), 16);

// 生成一种字体
WritableCellFormat font = new WritableCellFormat(wfont);

// 字体水平居中
font.setAlignment(jxl.format.Alignment.CENTRE);

// 字体垂直居中
font.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);

// 自动换行
font.setWrap(true);

// sheet操作列的属性
// 合并单元格 sheet.mergeCells(第几类,第几格,到第几列,到第几格)
sheet.mergeCells(0, 0, title.length-2, 0);

// 单元格行高
sheet.setRowView(0, 750);

// 创建label,给其内容
label = new Label(0, 0, title[0], font);

// 写进excel
sheet.addCell(label);

for (int i = 1; i < title.length; i++) {

sheet.setRowView(i, 800);
sheet.setColumnView(0, 8);
sheet.setColumnView(i, 24);

label = new Label(i - 1, 1, title[i], font);
sheet.addCell(label);

}

wfont = new jxl.write.WritableFont(WritableFont.createFont("楷体"), 12);
font = new WritableCellFormat(wfont);

font.setAlignment(jxl.format.Alignment.CENTRE);
font.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
font.setWrap(true);

if (conList != null && conList.size() > 0) {

int j = 2;

for (int i = 0; i < conList.size(); i++) {

Object tr = conList.get(i);

if (tr != null) {

sheet.setRowView(j, 800);
sheet.setColumnView(0, 8);
sheet.setColumnView(i + 1, 24);

label = new Label(0, j, j - 1 + "", font);
sheet.addCell(label);

for (int l = 0; l < propertys.length; l++) {

label = new Label(l+1, j, (String) tr.getClass().getMethod("get" + propertys[l]).invoke(tr), font);
sheet.addCell(label);
}

j++;
}
}
}

SheetSettings setting = sheet.getSettings();
// 页边距start
setting.setTopMargin(0.0);
setting.setLeftMargin(0.5);
setting.setRightMargin(0.0);
setting.setBottomMargin(0.0);
// 页边距end

// 写入数据
wwb.write();
// 关闭文件
wwb.close();

return true;
}

} catch (Exception e) {

System.out.println("writeExcelForDoc1---出现异常---");
e.printStackTrace();
}
return false;
}

/**
*
* 方法说明:
* 创建时间:2018年8月24日 (***)
* @param excelPath 文件地址
* @param obj 要导入的数据实体类
* @param propertys 要导入的字段 字符串名称数组 首字母大写
* @param nameCodeMap Map<导入字段,Map<导入内容,转义信息>> 例: Map<性别,Map<男,"1">>
* @param outMap 非必填字段
* @return
*/
public static List<Object> uploadExcel(String excelPath, Object obj, String[] propertys, Map<String,Map<String,String>> nameCodeMap, Map<String,String> outMap) {

try {

File file = new File(excelPath);
Workbook book = Workbook.getWorkbook(file);

List<Object> tempList = new ArrayList<Object>();
Sheet[] sheets = book.getSheets();

for (Sheet sheet : sheets) {
// 获得第一个工作表对象
// Sheet sheet = book.getSheet(0);
if (sheet!= null && obj!=null) {

int columnum = sheet.getColumns();// 得到列数
int rownum = sheet.getRows();// 得到行数

for (int i = 2; i < rownum; i++) {

boolean bRet3 = false;

Cell cellFirst = sheet.getCell(0, i);
String resultFirst = cellFirst.getContents();

if(resultFirst==null || "".equals(resultFirst)){

continue;
}

for (int j = 1; j < columnum; j++) {
// 得到第一列第一行的单元格
Cell cell1 = sheet.getCell(j, i);
String result = cell1.getContents();
result = result.trim();

if ("".equals(result) || result==null) {

if("1".equals(outMap.get(propertys[(j-1)]))){

continue;

}else{

break;
}

}else{

bRet3 = true;

if(nameCodeMap!=null && nameCodeMap.size()>0 && nameCodeMap.get(propertys[(j-1)])!=null && !"".equals(nameCodeMap.get(propertys[(j-1)]))){

String value = nameCodeMap.get(propertys[(j-1)]).get(result);

if(value!=null && !"".equals(value)){

obj.getClass().getMethod("set"+propertys[(j-1)],String.class).invoke(obj, value);
}
}else if("-".equals(result) || "无".equals(result) || "空".equals(result)){

obj.getClass().getMethod("set"+propertys[(j-1)],String.class).invoke(obj, "");
}else{

obj.getClass().getMethod("set"+propertys[(j-1)],String.class).invoke(obj, result);
}
}
}

if (bRet3) {

tempList.add(obj);
obj = obj.getClass().getMethod("initThisObj").invoke(obj);

}else{

book.close();
return null;
}
}
}
}

book.close();
file = null;
// System.out.println("tempList:"+tempList);
// Excel里面有数据才会加到集合里,没有返回空集合
return tempList;

} catch (Exception e) {

e.printStackTrace();

}

return null;
}

posted on 2018-08-24 10:38  维也纳的忧伤  阅读(657)  评论(0编辑  收藏  举报