springMVC中导出excel案例

原博文地址:http://my.oschina.net/xiaoxiangdaizi/blog/491932?p=1#OSC_h3_4



数据模型

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
  * Created by lgq on 2015/8/13.
  */
public  class  Student {
 
     private  long  id;
 
     private  String name;
 
     private  int  age;
 
     private  boolean  sex;
 
     private  Date birthday;
 
     public  long  getId() {
         return  id;
     }
 
     public  void  setId( long  id) {
         this .id = id;
     }
 
     public  String getName() {
         return  name;
     }
 
     public  void  setName(String name) {
         this .name = name;
     }
 
     public  int  getAge() {
         return  age;
     }
 
     public  void  setAge( int  age) {
         this .age = age;
     }
 
     public  boolean  isSex() {
         return  sex;
     }
 
     public  void  setSex( boolean  sex) {
         this .sex = sex;
     }
 
     public  Date getBirthday() {
         return  birthday;
     }
 
     public  void  setBirthday(Date birthday) {
         this .birthday = birthday;
     }
}


编写excel视图实现,继承springMVC中的AbstractExcelView类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
  * 学生excel导出类, 继承springMVC中的AbstractExcelView类,
  * 并实现buildExcelDocument()方法
  * Created by lgq on 2015/8/13.
  */
public  class  StudentExcelView  extends  AbstractExcelView {
 
 
     @Override
     protected  void  buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response)  throws  Exception {
 
         List<Student> studentList = (List<Student>) model.get( "dataSet" );
         HSSFSheet sheet = workbook.createSheet();
         sheet.setDefaultColumnWidth( 12 );
 
         HSSFCell cell = getCell(sheet, 0 , 0 );
         setText(cell,  "ID" );
         cell = getCell(sheet, 0 , 1 );
         setText(cell,  "姓名" );
         cell = getCell(sheet, 0 , 2 );
         setText(cell,  "年龄" );
         cell = getCell(sheet, 0 , 3 );
         setText(cell,  "性别" );
         cell = getCell(sheet, 0 , 4 );
         setText(cell,  "生日" );
 
         for  ( int  i =  0 ; i < studentList.size(); i++) {
             HSSFRow row = sheet.createRow(i+ 1 );
             Student student = studentList.get(i);
             // 处理列
             row.createCell( 0 ).setCellValue(student.getId());
             row.createCell( 1 ).setCellValue(student.getName());
             row.createCell( 2 ).setCellValue(student.getAge());
             if (student.isSex()) {
                 row.createCell( 3 ).setCellValue( "男" );
             else  {
                 row.createCell( 3 ).setCellValue( "女" );
             }
             row.createCell( 4 ).setCellValue( new  SimpleDateFormat( "yyyy-MM-dd" ).format(student.getBirthday()));
         }
         String fileName =  "学生信息.xls" ;
         // 处理中文文件名
         response.setContentType( "application/vnd.ms-excel" );
         response.setHeader( "Content-disposition" "attachment;filename="  + fileName);
         OutputStream outputStream = response.getOutputStream();
         workbook.write(outputStream);
         outputStream.flush();
         outputStream.close();
 
     }
}


编写controller类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Controller
@RequestMapping (value =  "/controller/student" )
public  class  StudentController {
 
 
     @RequestMapping (value =  "/export" )
     public  ModelAndView export(ModelMap model)  throws  ParseException {
         List<Student> dataSet =  new  ArrayList<Student>();
         for  ( int  i =  0 ; i <  20 ; i++) {
             Student student =  new  Student();
             student.setId(i);
             student.setName( "lgq" +i);
             student.setAge( 20 );
             student.setSex( false );
             student.setBirthday( new  Date());
             dataSet.add(student);
         }
         StudentExcelView studentExcelView =  new  StudentExcelView();
         model.put( "dataSet" , dataSet);
         return  new  ModelAndView(studentExcelView, model);
     }
 
}


导出数据展示


数据模型

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
  * Created by lgq on 2015/8/13.
  */
public  class  Student {
 
     private  long  id;
 
     private  String name;
 
     private  int  age;
 
     private  boolean  sex;
 
     private  Date birthday;
 
     public  long  getId() {
         return  id;
     }
 
     public  void  setId( long  id) {
         this .id = id;
     }
 
     public  String getName() {
         return  name;
     }
 
     public  void  setName(String name) {
         this .name = name;
     }
 
     public  int  getAge() {
         return  age;
     }
 
     public  void  setAge( int  age) {
         this .age = age;
     }
 
     public  boolean  isSex() {
         return  sex;
     }
 
     public  void  setSex( boolean  sex) {
         this .sex = sex;
     }
 
     public  Date getBirthday() {
         return  birthday;
     }
 
     public  void  setBirthday(Date birthday) {
         this .birthday = birthday;
     }
}


编写excel视图实现,继承springMVC中的AbstractExcelView类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
  * 学生excel导出类, 继承springMVC中的AbstractExcelView类,
  * 并实现buildExcelDocument()方法
  * Created by lgq on 2015/8/13.
  */
public  class  StudentExcelView  extends  AbstractExcelView {
 
 
     @Override
     protected  void  buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response)  throws  Exception {
 
         List<Student> studentList = (List<Student>) model.get( "dataSet" );
         HSSFSheet sheet = workbook.createSheet();
         sheet.setDefaultColumnWidth( 12 );
 
         HSSFCell cell = getCell(sheet, 0 , 0 );
         setText(cell,  "ID" );
         cell = getCell(sheet, 0 , 1 );
         setText(cell,  "姓名" );
         cell = getCell(sheet, 0 , 2 );
         setText(cell,  "年龄" );
         cell = getCell(sheet, 0 , 3 );
         setText(cell,  "性别" );
         cell = getCell(sheet, 0 , 4 );
         setText(cell,  "生日" );
 
         for  ( int  i =  0 ; i < studentList.size(); i++) {
             HSSFRow row = sheet.createRow(i+ 1 );
             Student student = studentList.get(i);
             // 处理列
             row.createCell( 0 ).setCellValue(student.getId());
             row.createCell( 1 ).setCellValue(student.getName());
             row.createCell( 2 ).setCellValue(student.getAge());
             if (student.isSex()) {
                 row.createCell( 3 ).setCellValue( "男" );
             else  {
                 row.createCell( 3 ).setCellValue( "女" );
             }
             row.createCell( 4 ).setCellValue( new  SimpleDateFormat( "yyyy-MM-dd" ).format(student.getBirthday()));
         }
         String fileName =  "学生信息.xls" ;
         // 处理中文文件名
         response.setContentType( "application/vnd.ms-excel" );
         response.setHeader( "Content-disposition" "attachment;filename="  + fileName);
         OutputStream outputStream = response.getOutputStream();
         workbook.write(outputStream);
         outputStream.flush();
         outputStream.close();
 
     }
}


编写controller类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Controller
@RequestMapping (value =  "/controller/student" )
public  class  StudentController {
 
 
     @RequestMapping (value =  "/export" )
     public  ModelAndView export(ModelMap model)  throws  ParseException {
         List<Student> dataSet =  new  ArrayList<Student>();
         for  ( int  i =  0 ; i <  20 ; i++) {
             Student student =  new  Student();
             student.setId(i);
             student.setName( "lgq" +i);
             student.setAge( 20 );
             student.setSex( false );
             student.setBirthday( new  Date());
             dataSet.add(student);
         }
         StudentExcelView studentExcelView =  new  StudentExcelView();
         model.put( "dataSet" , dataSet);
         return  new  ModelAndView(studentExcelView, model);
     }
 
}


导出数据展示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值