---
title: "用Excel导出数据（二）"
author: "Illy"
date: "2012-08-03T11:38:42Z"
tags: []
categories: ["编程语言"]
slug: "excel"
draft: false
lastmod: "2012-08-03T11:38:42Z"
id: 1618055
url: "https://illy.iteye.com/blog/1618055"
---

该项目是基于Struts2框架所设计
导入包文件：

```java

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;

public void listExcel() {
		HttpServletResponse response = ServletActionContext.getResponse();
		List<BasServeSatisfaction> bssInfo = new ArrayList<BasServeSatisfaction>();
		bssInfo = serviceSatisfactionService.getServiceSatisfaction(tmNameStr, startTimeStr, endTimeStr, isSurveyStr);	
		BufferedInputStream bis = null;  
		BufferedOutputStream bos = null; 
		if(bssInfo!=null && bssInfo.size()>0){
			response.reset();  
			response.setContentType("application/vnd.ms-excel;charset=utf-8");
			ServletOutputStream outputStream = null;
			try {
				outputStream = response.getOutputStream();			
			    response.setHeader("Content-Disposition", "attachment;filename=" + new String("queryServeSatisfaction.xls".getBytes(), "utf-8"));  
			    bis = new BufferedInputStream(this.getJjrExcel(bssInfo));  
				bos = new BufferedOutputStream(outputStream);  
	            byte[] buff = new byte[2048];  
	            int bytesRead;  
	            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
		             bos.write(buff, 0, bytesRead);  
	            }  
		      } catch (Exception e) {  
		          e.printStackTrace(); 
		      } finally {
		          try {
					if (bis != null)
						  bis.close();  
					  if (bos != null)
						  bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}  
	         }
		}
	}

 public InputStream getJjrExcel(List<BasServeSatisfaction> result){		
			Workbook workbook = new HSSFWorkbook();
			Sheet sheet = workbook.createSheet("服务满意度调查报告");
			// 创建单元格样式  
			CellStyle style = workbook.createCellStyle();//创建样式 
			// 指定单元格居中对齐  
			style.setAlignment(CellStyle.ALIGN_CENTER); 
	        // 指定单元格垂直居中对齐  
			style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
			Row row = sheet.createRow(0);		
			Cell cell = row.createCell((short) 0);
			cell = row.createCell((short) 0);
			cell.setCellValue("客户编号");
			cell = row.createCell((short) 1);
			cell.setCellValue("客户姓名");
			cell = row.createCell((short) 2);
			cell.setCellValue("被调查人员姓名");
			cell = row.createCell((short) 3);
			cell.setCellValue("调查时间");
			cell = row.createCell((short) 4);
			cell.setCellValue("满意度");
			cell = row.createCell((short) 5);
			cell.setCellValue("调查方式");
			cell = row.createCell((short) 6);
			cell.setCellValue("客户反应情况");
			cell = row.createCell((short) 7);
			cell.setCellValue("没参加调查原因");
//			sheet.addMergedRegion(new CellRangeAddress(1, 1 , 2, 4));
//			sheet.addMergedRegion(new CellRangeAddress(1, 1 , 5, 7));
//			sheet.addMergedRegion(new CellRangeAddress(1, 1 , 8, 10));
//			sheet.addMergedRegion(new CellRangeAddress(1, 1 , 11, 13));
//			sheet.addMergedRegion(new CellRangeAddress(1, 1 , 14, 16));
//			sheet.addMergedRegion(new CellRangeAddress(1, 1 , 17, 19));
//			sheet.addMergedRegion(new CellRangeAddress(1, 1 , 20, 22));
//			sheet.addMergedRegion(new CellRangeAddress(1, 1 , 23, 25));
//			sheet.addMergedRegion(new CellRangeAddress(1, 1 , 26, 28));
//			sheet.addMergedRegion(new CellRangeAddress(1, 1 , 29, 31));
//			sheet.addMergedRegion(new CellRangeAddress(1, 1 , 32, 34));
//			sheet.addMergedRegion(new CellRangeAddress(1, 1 , 35, 37));
//			sheet.addMergedRegion(new CellRangeAddress(1, 2 , 0, 0));
			String surveyType = "";
			String serverSatisfaction = "";
			if(result!=null){
				for(int i=0;i<result.size();i++){
					BasServeSatisfaction bssResult =(BasServeSatisfaction) result.get(i);
					
					surveyType = bssResult.getSurveyType();
					serverSatisfaction = bssResult.getServerSatisfaction() + "";
					String surveyTypeName = "";
					String serverSatisfactionName = "";
					if("1".equals(surveyType)) {
						surveyTypeName = "电话";
					}else if("2".equals(surveyType)) {
						surveyTypeName = "邮件";
					}else if("3".equals(surveyType)) {
						surveyTypeName = "上门";
					}
					
					if("0".equals(serverSatisfaction)) {
						serverSatisfactionName = "非常满意";
					}else if("2".equals(serverSatisfaction)) {
						serverSatisfactionName = "满意";
					}else if("3".equals(serverSatisfaction)) {
						serverSatisfactionName = "不太满意";
					}else if("4".equals(serverSatisfaction)) {
						serverSatisfactionName = "非常不满意";
					}
					Row row2 = sheet.createRow(i+1);
					cell = row2.createCell((short) 0);
					cell.setCellValue(bssResult.getTmId());
					cell = row2.createCell((short) 1);
					cell.setCellValue(bssResult.getTmName());
					cell = row2.createCell((short) 2);
					cell.setCellValue(bssResult.getBeSurveyName());
					cell = row2.createCell((short) 3);
					cell.setCellValue((bssResult.getSurveyDate() + "").substring(0, 10));
					cell = row2.createCell((short) 4);
					cell.setCellValue(serverSatisfactionName);
					cell = row2.createCell((short) 5);
					cell.setCellValue(surveyTypeName);
					cell = row2.createCell((short) 6);
					cell.setCellValue(bssResult.getCustomerReactionCase());
					cell = row2.createCell((short) 7);
					cell.setCellValue(bssResult.getNoSurveyReason());
				}
			}
			ByteArrayOutputStream os = new ByteArrayOutputStream();  
			try {  
				workbook.write(os);  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	        byte[] content = os.toByteArray();  
	        InputStream is = new ByteArrayInputStream(content);  
	        return is;
		}

```