package com.dogoserver.tools;
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 java.util.List;
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.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import com.dogoserver.user.beans.UserAble;
public class UserExeclUtil {
public static void createUserAbleExecl(List<UserAble> listUserAble,HttpServletResponse response) throws IOException{
// 创建excel工作簿
Workbook wb = new HSSFWorkbook();
// 创建第一个sheet(页)
Sheet sheet = (Sheet) wb.createSheet();
//创建第行
Row row = sheet.createRow((short) 0);
Cell serialNo = row.createCell(0);
serialNo.setCellValue("序号");
Cell userName = row.createCell(1);
userName.setCellValue("用户姓名");
Cell SCP = row.createCell(2);
SCP.setCellValue("送检人");
Cell focus = row.createCell(3);
focus.setCellValue("专注能力");
Cell selfControl = row.createCell(4);
selfControl.setCellValue("自控能力");
Cell language = row.createCell(5);
language.setCellValue("语言能力");
Cell music = row.createCell(6);
music.setCellValue("音乐辨识能力");
Cell memory = row.createCell(7);
memory.setCellValue("情景记忆能力");
Cell eq = row.createCell(8);
eq.setCellValue("情绪控制能力");
Cell learn = row.createCell(9);
learn.setCellValue("学习能力");
Cell create = row.createCell(10);
create.setCellValue("创造能力");
Cell math = row.createCell(11);
math.setCellValue("数学能力");
Cell sport = row.createCell(12);
sport.setCellValue("运动能力");
Cell wit = row.createCell(13);
wit.setCellValue("智力");
int rowNo = 1;
for(UserAble userAble: listUserAble){
Row rowInfo = sheet.createRow(rowNo);
Cell serialNoInfo = row.createCell(0);
serialNoInfo.setCellValue(rowNo);
Cell userNameInfo = rowInfo.createCell(1);
userNameInfo.setCellValue(userAble.getU_name());
Cell SCPInfo = row.createCell(2);
SCPInfo.setCellValue(userAble.getSend_check_person());
Cell focusInfo = row.createCell(3);
focusInfo.setCellValue(userAble.getFocus());
Cell selfControlInfo = row.createCell(4);
selfControlInfo.setCellValue(userAble.getSelf_control());
Cell languageInfo = row.createCell(5);
languageInfo.setCellValue(userAble.getLanguage());
Cell musicInfo = row.createCell(6);
musicInfo.setCellValue(userAble.getMusic());
Cell memoryInfo = row.createCell(7);
memoryInfo.setCellValue(userAble.getScene_memory());
Cell eqInfo = row.createCell(8);
eqInfo.setCellValue(userAble.getEmotions_control());
Cell learnInfo = row.createCell(9);
learnInfo.setCellValue(userAble.getLearn_ab());
Cell createInfo = row.createCell(10);
createInfo.setCellValue(userAble.getCreate_ab());
Cell mathInfo = row.createCell(11);
mathInfo.setCellValue(userAble.getMath());
Cell sportInfo = row.createCell(12);
sportInfo.setCellValue(userAble.getSport());
Cell witInfo = row.createCell(13);
witInfo.setCellValue(userAble.getWit());
rowNo++;
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
wb.write(os);
} catch (IOException e) {
e.printStackTrace();
}
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
// 设置response参数,可以打开下载页面
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" +
new String(("aaa" + ".xls").getBytes(), "iso-8859-1"));
response.setContentType("application/octet-stream;charset=UTF-8");
ServletOutputStream out = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
// Simple read/write loop.
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (final IOException e) {
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}
}