POI导出excel的简单demo

目前使用过两种导出excel的方式,一种是如题所示的使用POI的方式进行数据的导出,这种方式一般只有在处理比较多的数据或者说需要导出的excel表格中有图片之类的需要特殊处理的文件的时候使用;还有一种方式是直接在HTML文件中加上.xls的后缀,直接导出excel,一般比较适用于数据较少且没有特殊文件的情况下;再其次就是各种插件,不过适用插件进行导出的时候导出的图片一般无法展示;

使用POI导出excel:

package com.controller;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.pojo.Person;


@Controller
@RequestMapping("/exportController")
public class ExportController {
       @RequestMapping("/export")
	public void export(HttpServletResponse response){
                //适用List集合造一些数据作为要导出的数据
                List<Person> list = new ArrayList<Person>();
		list.add(new Person("亚索", "男", "800"));
		list.add(new Person("亚索1", "男", "801"));
		list.add(new Person("亚索2", "男", "802"));
                //调用方法创建HSSFWorkbook工作簿对象
		HSSFWorkbook wb = ExportController.export_text(list);
                try {
                        //定义导出文件的名称,看不懂的同学可以先行了解一下文件下载
			String fileName = new String("personRelation.xls".getBytes("UTF-8"),"ISO-8859-1");
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-Disposition","attachment; filename="+fileName);
			OutputStream os = response.getOutputStream();

                        //将工作薄写入到输出流中
			wb.write(os);
			os.close();
		} catch (Exception e) {
			e.getStackTrace();
		}
        }  

        //创建HSSFWorkbook工作薄对象
        public static HSSFWorkbook export_text(List<Person> list){
                 try {
                        //创建工作薄对象
			HSSFWorkbook wb = new HSSFWorkbook();
                        //创建标题行样式
			HSSFCellStyle headStyle = headStyle(wb);
                        //创建内容行样式
			HSSFCellStyle contentStyle = contentStyle(wb);
			
                        //创建表
			HSSFSheet sheet_1 =  wb.createSheet("人员信息");
                        //设置表的默认列宽
			sheet_1.setDefaultColumnWidth(30);
			
                        //创建标题行
			HSSFRow headRow = sheet_1.createRow(0);
			HSSFCell head_cell_1 = headRow.createCell(0);           //创建标题行第一列
			head_cell_1.setCellValue("姓名");                        //第一列内容
			head_cell_1.setCellStyle(headStyle);                    //将标题行样式添加
			
			HSSFCell head_cell_2 = headRow.createCell(1);
			head_cell_2.setCellValue("性别");
			head_cell_2.setCellStyle(headStyle);
			
			HSSFCell head_cell_3 = headRow.createCell(2);
			head_cell_3.setCellValue("年龄");
			head_cell_3.setCellStyle(headStyle);
			
                        //为内容行添加数据和样式
			for (int i = 1; i <= list.size(); i++) {
				HSSFRow contentRow = sheet_1.createRow(i);
				HSSFCell content_cell_1 = contentRow.createCell(0);
				content_cell_1.setCellValue(list.get(i-1).getName());
				content_cell_1.setCellStyle(contentStyle);
				
				HSSFCell content_cell_2 = contentRow.createCell(1);
				content_cell_2.setCellValue(list.get(i-1).getSex());
				content_cell_2.setCellStyle(contentStyle);
				
				HSSFCell content_cell_3 = contentRow.createCell(2);
				content_cell_3.setCellValue(list.get(i-1).getAge());
				content_cell_3.setCellStyle(contentStyle);
			}
			return wb;
		} catch (Exception e) {
			e.getStackTrace();
		}
		
		return null;
        }


        /**
	 * 创建标题行样式
	 * @param wb
	 * @return
	 */
	public static HSSFCellStyle headStyle(HSSFWorkbook wb){
                HSSFCellStyle headStyle = wb.createCellStyle();                       //创建样式对象
		HSSFFont headFont = wb.createFont();                                  //创建字体
		headFont.setFontName("微软雅黑");
		headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		headFont.setColor(HSSFFont.COLOR_RED);
		
		headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		headStyle.setFont(headFont);
		return headStyle;
        }

        /**
	 * 创建内容行样式
	 * @param wb
	 * @return
	 */
	public static HSSFCellStyle contentStyle(HSSFWorkbook wb){
                HSSFCellStyle contentStyle = wb.createCellStyle();
		HSSFFont contentFont = wb.createFont();
		contentFont.setFontName("微软雅黑");
		contentFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
		contentFont.setColor(HSSFFont.COLOR_NORMAL);
		
		contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		contentStyle.setFont(contentFont);
		return contentStyle;
        }

}    

  

 

posted @ 2016-09-07 21:03  西风恶  阅读(3473)  评论(0编辑  收藏  举报