.
This commit is contained in:
parent
e07d201186
commit
664cc5967b
@ -43,7 +43,7 @@ public class ExcelKit {
|
|||||||
* @author LiangXianYou
|
* @author LiangXianYou
|
||||||
* @date 2015-6-16 下午5:56:56
|
* @date 2015-6-16 下午5:56:56
|
||||||
*/
|
*/
|
||||||
//map:data,sheetName,hds,hdNames,xlsName;
|
//map:data,sheetName,hds,hdNames,
|
||||||
public static <T> Workbook exportExcels(List<Map<String, Object>> list) throws Exception {
|
public static <T> Workbook exportExcels(List<Map<String, Object>> list) throws Exception {
|
||||||
|
|
||||||
Workbook wb = new XSSFWorkbook(); //创建工作薄
|
Workbook wb = new XSSFWorkbook(); //创建工作薄
|
||||||
@ -64,12 +64,7 @@ public class ExcelKit {
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
List<T> data = (List<T>) map.get("data");
|
List<T> data = (List<T>) map.get("data");
|
||||||
String[] hds = (String[]) map.get("hds");
|
String[] hds = (String[]) map.get("hds");
|
||||||
for (int j = 0; j < data.size(); j++) {
|
dataToSheet(sheet, data, hds, 1);
|
||||||
for (int k = 0; k < hds.length; k++) {
|
|
||||||
Object o = getFieldValue(data.get(j), hds[k]); //得到列的值
|
|
||||||
data2Excel(sheet, o, j + 1, k); //将值写入Excel
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return wb;
|
return wb;
|
||||||
@ -100,38 +95,28 @@ public class ExcelKit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 参数说明:
|
* 使用数据构建 excel 工作薄对象
|
||||||
* list:list数据集合
|
* @param list 数据
|
||||||
* hdNames:表头需要显示的名称
|
* @param hdNames 表头
|
||||||
* hds:表头对应的对象属性名称,和 hdNames一一对应
|
* @param hds 每条记录中数据的属性名
|
||||||
* xlsName:导出Excel的预定义名称
|
* @param <T> 数据泛型,支持javaBean 或Map
|
||||||
* request: HttpServletRequest
|
* @return
|
||||||
* response:HttpServletResponse
|
|
||||||
* 通过数据构建Excel
|
|
||||||
*
|
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
* @author LiangXianYou
|
* @author LiangXianYou
|
||||||
* @date 2015-3-13 上午11:00:30
|
* @date 2015-3-13 上午11:00:30
|
||||||
*/
|
*/
|
||||||
public static <T> Workbook exportExcel(List<T> list, String[] hdNames, String[] hds) throws Exception {
|
public static <T> Workbook exportExcel(List<T> list, String[] hdNames, String[] hds) throws Exception {
|
||||||
|
|
||||||
Workbook wb = new XSSFWorkbook(); //创建工作薄
|
Workbook wb = new XSSFWorkbook();
|
||||||
Sheet sheet = wb.createSheet(); //创建工作表
|
Sheet sheet = wb.createSheet();
|
||||||
sheet.autoSizeColumn((short) 0); //自适应宽度
|
|
||||||
//写入表头---Excel的第一行数据
|
//写入表头---Excel的第一行数据
|
||||||
Row nRow = sheet.createRow(0); //创建行
|
Row nRow = sheet.createRow(0);
|
||||||
for (int i = 0; i < hdNames.length; i++) {
|
for (int i = 0; i < hdNames.length; i++) {
|
||||||
Cell nCell = nRow.createCell(i); //创建单元格
|
Cell nCell = nRow.createCell(i);
|
||||||
nCell.setCellValue(hdNames[i]);
|
nCell.setCellValue(hdNames[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
//写入每一行数据---一条记录就是一行数据
|
dataToSheet(sheet, list, hds, 1);
|
||||||
for (int i = 0; i < list.size(); i++) {
|
|
||||||
for (int j = 0; j < hds.length; j++) {
|
|
||||||
Object o = getFieldValue(list.get(i), hds[j]); //得到列的值
|
|
||||||
data2Excel(sheet, o, i + 1, j); //将值写入Excel
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return wb;
|
return wb;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,11 +133,11 @@ public class ExcelKit {
|
|||||||
if (t == null) {
|
if (t == null) {
|
||||||
v = null;
|
v = null;
|
||||||
} else if (t instanceof Map) {
|
} else if (t instanceof Map) {
|
||||||
v = ((Map) t).get(fieldName); //得到列的值
|
v = ((Map) t).get(fieldName);
|
||||||
} else {
|
} else {
|
||||||
Field field = t.getClass().getDeclaredField(fieldName);
|
Field field = t.getClass().getDeclaredField(fieldName);
|
||||||
field.setAccessible(true); //暴力反射
|
field.setAccessible(true);
|
||||||
v = field.get(t); //得到字段数据的值
|
v = field.get(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
@ -164,24 +149,38 @@ public class ExcelKit {
|
|||||||
* @author LiangXianYou
|
* @author LiangXianYou
|
||||||
* @date 2015-3-13 上午10:37:55
|
* @date 2015-3-13 上午10:37:55
|
||||||
*/
|
*/
|
||||||
private static void data2Excel(Sheet sheet, Object o, Integer r, Integer c) {
|
private static <T> void dataToSheet(Sheet sheet, List<T> list, String[] hds, int skipRow) {
|
||||||
//通过获得sheet中的某一列,有得到,没有创建
|
if (list == null || list.size() == 0) return;
|
||||||
Row nRow = sheet.getRow(r);
|
|
||||||
if (nRow == null) {
|
|
||||||
nRow = sheet.createRow(r);
|
|
||||||
}
|
|
||||||
Cell nCell = nRow.createCell(c);
|
|
||||||
|
|
||||||
|
for (int j = 0; j < list.size(); j++) {
|
||||||
|
for (int k = 0; k < hds.length; k++) {
|
||||||
|
Row nRow = sheet.getRow(j + skipRow);
|
||||||
|
if (nRow == null) {
|
||||||
|
nRow = sheet.createRow(j + 1);
|
||||||
|
}
|
||||||
|
Cell cell = nRow.createCell(k);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Object v = getFieldValue(list.get(j), hds[k]); //得到列的值
|
||||||
|
dataToCell(cell, v); //将值写入Excel
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void dataToCell(Cell cell, Object v) {
|
||||||
//根据不同类型进行转化,如有其它类型没有考虑周全的,使用发现的时候添加
|
//根据不同类型进行转化,如有其它类型没有考虑周全的,使用发现的时候添加
|
||||||
if (o instanceof Integer) nCell.setCellValue((Integer) o);
|
if (v instanceof Integer) cell.setCellValue((Integer) v);
|
||||||
else if (o instanceof Double) nCell.setCellValue((Double) o);
|
else if (v instanceof Double) cell.setCellValue((Double) v);
|
||||||
else if (o instanceof Float) nCell.setCellValue((Float) o);
|
else if (v instanceof Float) cell.setCellValue((Float) v);
|
||||||
else if (o instanceof String) nCell.setCellValue((String) o);
|
else if (v instanceof String) cell.setCellValue((String) v);
|
||||||
else if (o instanceof Date) nCell.setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(o));
|
else if (v instanceof Date) cell.setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(v));
|
||||||
else if (o instanceof Calendar) nCell.setCellValue((Calendar) o);
|
else if (v instanceof Calendar) cell.setCellValue((Calendar) v);
|
||||||
else if (o instanceof Boolean) nCell.setCellValue((Boolean) o);
|
else if (v instanceof Boolean) cell.setCellValue((Boolean) v);
|
||||||
else if (o == null) nCell.setCellValue("");
|
else if (v == null) cell.setCellValue("");
|
||||||
else nCell.setCellValue(o + "");
|
else cell.setCellValue(v + "");
|
||||||
}
|
}
|
||||||
|
|
||||||
//======================= 读取excel ===============================
|
//======================= 读取excel ===============================
|
||||||
|
Loading…
Reference in New Issue
Block a user