本文章都是自创,如有类同纯属巧合,转发的朋友请写我的文章链接
1.先写一个跟导出excel对应的字段的list
private ArrayList<String> list = new ArrayList<String>(Arrays.asList(
"Name","Sex","ShenGao","TiZhong","Dept"
));
2.导出的方法
@RequestMapping(value = "export",method = RequestMethod.GET)
public void export(HttpServletRequest request, HttpServletResponse response) throws IOException, InvalidFormatException, ParseException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
String name= request.getParameter("name");
User user= new user();
if (StringUtils.isNotEmpty(name)) {
user.setName(name);
}
List<User> users= userService.selectUserAll(name);
XSSFWorkbook excelbook = new XSSFWorkbook();
XSSFSheet excelSheet = excelbook.createSheet();
createExcelHeader(excelbook,excelSheet);
XSSFCellStyle headerStyle = excelbook.createCellStyle();
headerStyle.setAlignment(XSSFCellStyle.ALIGN_LEFT);
for (int i = 0; i < users.size(); i++) {
XSSFRow row = excelSheet.createRow(i+1);
User user= Users.get(i);
Field[] declaredFields = user.getClass().getDeclaredFields();
Map<String,Integer> map = new HashMap();
for (int i1 = 0; i1 < declaredFields.length; i1++) {
Field declaredField = declaredFields[i1];
String name = declaredField.getName();
name = name.substring(0, 1).toUpperCase() + name.substring(1);
map.put(name, i1);
}
for (int i1 = 0; i1 < list.size(); i1++) {
String s = list.get(i1);
XSSFCell cell = row.createCell(i1);
cell.setCellStyle(headerStyle);
Integer weiZhi = map.get(s);
Field declaredField = declaredFields[weiZhi];
String type = declaredField.getGenericType().toString();
if (type.equals("class java.lang.String")) {
Method m = glxxJGlsdAll.getClass().getMethod("get" + s);
String value = (String) m.invoke(glxxJGlsdAll);
if (value != null) {
cell.setCellValue(value);
continue;
} else {
continue;
}
}
if (type.equals("class java.lang.Integer")) {
Method m = glxxJGlsdAll.getClass().getMethod("get" + s);
Integer value = (Integer) m.invoke(glxxJGlsdAll);
if (value != null) {
cell.setCellValue(value);
continue;
} else {
continue;
}
}
if (type.equals("class java.lang.Short")) {
Method m = glxxJGlsdAll.getClass().getMethod("get" + s);
Short value = (Short) m.invoke(glxxJGlsdAll);
if (value != null) {
cell.setCellValue(value);
continue;
} else {
continue;
}
}
if (type.equals("class java.lang.Double")) {
Method m = glxxJGlsdAll.getClass().getMethod("get" + s);
Double value = (Double) m.invoke(glxxJGlsdAll);
if (value != null) {
cell.setCellValue(value);
continue;
} else {
continue;
}
}
if (type.equals("class java.lang.Boolean")) {
Method m = glxxJGlsdAll.getClass().getMethod("get" + s);
Boolean value = (Boolean) m.invoke(glxxJGlsdAll);
if (value != null) {
cell.setCellValue(value);
continue;
} else {
continue;
}
}
if (type.equals("class java.util.Date")) {
Method m = glxxJGlsdAll.getClass().getMethod("get" + s);
Date value = (Date) m.invoke(glxxJGlsdAll);
if (value != null) {
cell.setCellValue(value);
continue;
} else {
continue;
}
}
}
}
ByteArrayOutputStream fos = null;
byte[] retArr = null;
try {
fos = new ByteArrayOutputStream();
excelbook.write(fos);
retArr = fos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
OutputStream os = response.getOutputStream();
try {
response.reset();
response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode("隧道信息.xlsx","UTF-8"));
response.setContentType("application/octet-stream; charset=utf-8");
os.write(retArr);
os.flush();
} finally {
if (os != null) {
os.close();
}
}
}
3.写标题的方法
void createExcelHeader(XSSFWorkbook workbook,XSSFSheet sheet) {
XSSFRow excelRow = sheet.createRow(0);
XSSFCellStyle headerStyle = workbook.createCellStyle();
headerStyle.setAlignment(XSSFCellStyle.ALIGN_LEFT);
ArrayList<String> list2 = new ArrayList<String>(Arrays.asList(
"名字","性别","身高","体重","部门"));
for (int i = 0; i < list2.size(); i++) {
XSSFCell cell = excelRow.createCell(i);
cell.setCellStyle(headerStyle);
cell.setCellValue(list2.get(i));
}
}