ExtJS Grid导出excel文件,
需下载POI:链接:http://pan.baidu.com/s/1i3lkPhF 密码:rqbg
1、将Grid表格数据连同表格列名传到后台
2、后台导出excel文件,返回相对路径
3、根据相对路径,下载excel文件
JS代码,按钮点击事件
1 /**
2 *点击“导出”按钮触发事件
3 *
4 */
5 onClickExport:function(btn){
6 // 获取表格数据
7 var conditionForm = btn.up('form');
8 var grid = conditionForm.nextSibling('grid');
9 var store = grid.getStore();
10 var obj = {};
11 obj.projectIncomes = this.tools.storeToArray(store);
12 if(obj.projectIncomes.length != 0){
13 obj.projectIncomes.splice(0, 0, this.tools.getColumnName(grid));
14 this.exportExcel(obj)
15 }else{
16 Ext.Msg.alert('提示', '表格中无数据,请先执行查询操作!');
17 }
18
19 },
20
21 /**
22 *导出excel
23 *
24 */
25 exportExcel : function(obj) {
26 var httpParams = this.tools.objectToHttpOgnlParams(obj,null,3);
27 Ext.Ajax.request({
28 url : 'projectIncomeToExcel.action',
29 params : httpParams,
30 method : 'post',
31 waitMsg:'稍安勿躁...',
32 success : function(xmlHttpRequest) {
33 var serverFilePathRelativeToWebappRoot = Ext.decode(xmlHttpRequest.responseText).fileServerPaths;
34 window.open(serverFilePathRelativeToWebappRoot);
35 },
36 failure : function() {
37 Ext.Msg.alert('悲剧', '数据导出失败,请重试');
38 }
39 });
40 },
41
42 privates : {
43 tools : Ext.create('MyApp.Tools')
44 }
tools工具
1 /**
2 * storeToArray
3 */
4 storeToArray:function(store){
5 var arr = Ext.pluck(store.data.items, 'data');
6 return arr;
7 },
8
9 /**
10 * 获取grid的所有列
11 * 返回对象columnName
12 * 属性为列dataIndex,值为列text
13 */
14 getColumnName:function(grid){
15 var columnName = {};
16 for(var i = 0; i < grid.columns.length; i++){
17 columnName[grid.columns[i].dataIndex] = grid.columns[i].text;
18 };
19 return columnName;
20 },
后台ation文件
1 public class ProjectIncomeToExcelAction extends AbstractAction{
2
3 private static final long serialVersionUID = 1L;
4
5 private List<ProjectIncomeStatistics> projectIncomes;
6
7 private String fileServerPath;
8
9 @Override
10 protected void doAction() {
11 // 生成excel表格
12 fileServerPath = ExportExcelUtils.<ProjectIncomeStatistics>getExcelFileServerPath(projectIncomes,"项目收入明细表");
13 // 清空返回值projectIncomes
14 projectIncomes.clear();
15 }
16
17 public List<ProjectIncomeStatistics> getProjectIncomes() {
18 return projectIncomes;
19 }
20
21 public void setProjectIncomes(List<ProjectIncomeStatistics> projectIncomes) {
22 this.projectIncomes = projectIncomes;
23 }
24
25 public String getFileServerPaths() {
26 return fileServerPath;
27 }
28
29 public void setFileServerPaths(String fileServerPath) {
30 this.fileServerPath = fileServerPath;
31 }
32
33 }
导出excel工具类
1 public class ExportExcelUtils {
2
3 /**
4 * webappRoot绝对路径
5 */
6 public static String ROOT = System.getProperty("td_oa.root")+File.separator ;
7
8
9 /**
10 * 生成并导出
11 *
12 * @param List<O> objs 对象List,O 对象类型
13 * @param String name 文件名
14 * @return String[] fileServerPath 文件相对路径
15 */
16 // @SuppressWarnings("deprecation")
17 public static <O> String getExcelFileServerPath(List<O> objs,String name){
18
19
20 HSSFWorkbook wb = new HSSFWorkbook();
21 HSSFSheet sheet = wb.createSheet(name);
22 HSSFRow row = sheet.createRow((int) 0);
23 HSSFCellStyle style = wb.createCellStyle();
24 // 设置单元格居中
25 style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
26
27 // 获取对象属性名,返回值filedName第一个为自带属性,不是我们要的
28 String[] filedName = getFiledName(objs.get(0));
29 // 每一行
30 for(int i=0;i<objs.size();i++){
31 O obj = objs.get(i);
32 row = sheet.createRow((int) i );
33 // 该行的每一列,返回值filedName第一个为自带属性,不是我们要的,所以从j=1开始
34 for(int j = 1 ; j < filedName.length ; j++){
35 HSSFCell cell = row.createCell((short) j - 1);
36 cell.setCellValue(getFieldValueByName(filedName[j], obj));
37 cell.setCellStyle(style);
38 }
39 }
40 // 保存文件并返回相对路径
41 String fileServerPath = getFileServerPath( wb, name);
42
43 return fileServerPath;
44 }
45
46 /**
47 * 获取对象属性,返回一个字符串数组
48 *
49 * @param o 对象
50 * @return String[] 字符串数组
51 */
52 private static String[] getFiledName(Object o){
53 try {
54 Field[] fields = o.getClass().getDeclaredFields();
55 String[] fieldNames = new String[fields.length];
56 for (int i=0; i < fields.length; i++){
57 fieldNames[i] = fields[i].getName();
58 }
59 return fieldNames;
60 } catch (SecurityException e){
61 e.printStackTrace();
62 System.out.println(e.toString());
63 }
64 return null;
65 }
66
67 /**
68 * 使用反射根据属性名称获取属性值
69 *
70 * @param fieldName 属性名称
71 * @param o 操作对象
72 * @return String 属性值
73 */
74
75 private static String getFieldValueByName(String fieldName, Object o){
76 try {
77 String firstLetter = fieldName.substring(0, 1).toUpperCase();
78 String getter = "get" + firstLetter + fieldName.substring(1);
79 Method method = o.getClass().getMethod(getter, new Class[] {});
80 Object value = method.invoke(o, new Object[] {});
81 return (String) value;
82 } catch (Exception e) {
83 System.out.println("属性不存在");
84 return null;
85 }
86 }
87
88
89 /**
90 * 保存excel文件,并返回相对路径
91 *
92 * @param HSSFWorkbook wb 需保存的excel文件
93 * @param String name 文件名
94 * @return String fileServerPath返回相对路径
95 */
96 private static String getFileServerPath(HSSFWorkbook wb,String name){
97 String fileServerPath = "";
98 try{
99 fileServerPath = "files\\export\\";
100 // 绝对路径
101 fileServerPath = getAbsolutePath(fileServerPath);
102 // 校验路径是否存在,不存在则新建
103 mkDirs(fileServerPath);
104 // 路径中添加文件名
105 fileServerPath = fileServerPath + name + ".xls" ;
106 // 保存文件
107 FileOutputStream fout = new FileOutputStream(fileServerPath);
108 wb.write(fout);
109 fout.close();
110 // 获取相对路径
111 fileServerPath = getPathRelativeToWebappRoot(fileServerPath);
112 }catch(Exception err){
113 err.printStackTrace();
114 }
115 return fileServerPath;
116 }
117
118
119 /**
120 * 根据相对路径获取绝对路径
121 * @param serverFilePathRelativeToWebappRoot
122 */
123 private static String getAbsolutePath(String serverFilePathRelativeToWebappRoot){
124 return ROOT + serverFilePathRelativeToWebappRoot;
125 }
126
127 /**
128 * 根据绝对路径获取相对于webapp的路径
129 */
130 private static String getPathRelativeToWebappRoot(String absolutePath){
131 return StringUtils.removeStart(absolutePath, ROOT);
132 }
133
134
135 /**
136 * 校验路径是否存在,不存在则新建
137 *
138 * @param String path 绝对路径
139 */
140 private static void mkDirs(String path) {
141 File dir = new File(path);
142 if (!dir.exists()) {
143 dir.mkdirs();
144 }
145 }
146
147 }
最终效果
来源:https://www.cnblogs.com/sucia-panda/p/4958520.html