导出Excle java
今天小編又為大家帶來了好用的工具類啦 那就是導出Excle
public class Excel {
public ?final static <T> String exportExcel(String fileName,String sheetName,String[] Title, List<T> listContent,HttpServletRequest request,HttpServletResponse response){
String result="系統提示:Excel文件導出成功!";?
//輸出到excel
try {
OutputStream os= new BufferedOutputStream(response.getOutputStream()); ?
? response.reset();// 清空輸出流 ??
? final String userAgent = request.getHeader("USER-AGENT"); ?
? String finalFileName = null; ?
if(StringUtils.contains(userAgent, "MSIE")){//IE瀏覽器 ?
finalFileName = URLEncoder.encode(fileName,"UTF8"); ?
}else if(StringUtils.contains(userAgent, "Mozilla")){//google,火狐瀏覽器 ?
finalFileName = new String(fileName.getBytes(), "ISO8859-1"); ?
}else{ ?
finalFileName = URLEncoder.encode(fileName,"UTF8");//其他瀏覽器 ?
} ?
response.setHeader("Content-disposition", "p_w_upload; filename=\""+ finalFileName+"\"");
// 設定輸出文件頭 ? ? ?
response.setContentType("application/msexcel");// 定義輸出類型
//** **********創建工作簿************ *//
?HSSFWorkbook workbook = new HSSFWorkbook();
?
?//創建工作表
?HSSFSheet sheet = workbook.createSheet(sheetName);
?//設置行標題
?HSSFRow titleRow = sheet.createRow(0);
//設置excel表的表頭
?for(int i=0;i<Title.length;i++){
?sheet.setColumnWidth(i, 4000);
?//設置單元格中的數據
?titleRow.createCell(i).setCellValue(Title[i]);
?}
?//設置excel表中的剩余的單元格
?//獲取傳遞過來的正文的數據listContent
?for(int j=0;j<listContent.size();j++){
?//list中傳遞過來的是數據表中的正文部分
?List<String> cellValues = getValue(listContent.get(j));
?HSSFRow contentRow = sheet.createRow(j+1);
?//設置表中的數據
?for(int k=0;k<cellValues.size();k++){
?contentRow.createCell(k).setCellValue(cellValues.get(k));
?}
}
?//將生成的excel表寫入到流中去
?workbook.write(os);
?os.flush();
?os.close();
??
} catch (Exception e) {
result="系統提示:Excel文件導出失敗,原因:"+ e.toString();
? System.out.println(result);?
e.printStackTrace();
}
//返回結果
return result;
}
//獲得實體類中的屬性和值
public static List<String> getValue(Object object) throws Exception{
Field[] fields = object.getClass().getDeclaredFields();//獲取屬性名稱數組 ?
List<String> stringList = new ArrayList<>();
for (int i = 0; i < fields.length; i++) { ?
Object valueObj = getFieldValue(object,fields[i].getName());//獲取屬性值 ?
if (valueObj != null) {
String name = fields[i].getName(); // 獲取屬性的名字
name = name.substring(0, 1).toUpperCase() + name.substring(1); // 將屬性的首字符大寫,方便構造get,set方法
String type = fields[i].getGenericType().toString(); // 獲取屬性的類型
if (type.equals("class java.util.Date")) {
SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
valueObj = sim.format(valueObj);
? ? ? ? ? ? ? ? }
stringList.add(valueObj.toString());
}
}?
return stringList;
}
/**?
? ?* 通過反射,用屬性名稱獲得屬性值?
? ?* @param thisClass 需要獲取屬性值的類?
? ?* @param fieldName 該類的屬性名稱?
? ?* @return?
? ?*/ ?
? ?public static Object getFieldValue(Object thisClass, String fieldName) ?
? ?{ ?
? ? ? ?Object value = new Object(); ?
? ? ? ?Method method = null; ?
? ? ? ?try { ?
? ? ? ? ? ?String methodName = fieldName.substring(0, 1).toUpperCase()+ fieldName.substring(1); ?
? ? ? ? ? ?method = thisClass.getClass().getMethod("get" + methodName); ?
? ? ? ? ? ?value = method.invoke(thisClass); ?
? ? ? ?} catch (Exception e) { ?
? ? ? ? ? ?e.printStackTrace(); ?
? ? ? ?} ? ?
? ? ? ?return value; ?
? ?} ??
}
轉載于:https://blog.51cto.com/wangfoye/1899903
總結
以上是生活随笔為你收集整理的导出Excle java的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用easyui框架form控件,单选按
- 下一篇: JS作用域理解(声明提升)