1. 程式人生 > >java中的excel表格的匯出,實際專案開發

java中的excel表格的匯出,實際專案開發

java中匯出excel格式的資料,實際專案 經歷

首先 先整出一個表格模板,這是一個標準的格式:

public class ExcelExport { private static final String[] PART_METASIGNIN = {“姓名”,"年齡 ",“性別”}; private static final String[] PART_SIGNIN_LISTS = {“name”,“age”,“gender”};

public static void exportOutList(HttpServletRequest request, HttpServletResponse response, String actionName, List lists, int OnePageMax) throws IOException { writeExcel(request, response, lists, actionName, PART_METASIGNIN, PART_SIGNIN_LISTS, OnePageMax); } public static void writeExcel(HttpServletRequest request, HttpServletResponse response, List lists, String actionName, String[] title, String[] columns, int OnePageMax) throws IOException { File f = new File(actionName + “.xls”); try { boolean b = f.createNewFile(); Excel.writeExcel(new FileOutputStream(f), title, lists, columns, OnePageMax); } catch (Exception e) { e.printStackTrace(); } Files.exportFile(response, request, f, true); } } 然後開始 寫控制器方法 @RequestMapping("/u-excelOut") public void excelOut(HttpServletRequest request, HttpServletResponse response ,Student student) throws IOException {

List<Map<String, Object>> list = studentService.sudentList(student);        //獲取要匯出的資料
String name="學生資訊";
ExcelExport.exportOutList(request, response, name, list,  AppConst.excelMaxByPage);

} 物件類 get set我這裡就不多寫了。就三個屬性 name,age,gender 呼叫資料庫 中的資料 列表 可以直接通過studentService呼叫 jsp頁面需要

' + getValueByKey("name", itemData) + '' + '' + '