用Excel导出数据(二)

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

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值