package com.wkrj.info.controller;
import java.io.*;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import static com.wkrj.info.controller.ExprotCellStyle.*;
/**
* 导出用户数据
* @author LJH
*
*/
public class ExportUserUtils {
/**
* 把数据导出到path
*/
@SuppressWarnings("deprecation")
public static void exportD(List<Map<String ,Object>> datalist, HttpServletRequest request, HttpServletResponse response) {
//创建工作簿
HSSFWorkbook workbook=new HSSFWorkbook();
//从工作簿里面创建sheet
// workbook.createSheet();
HSSFSheet sheet = workbook.createSheet("用户列表");
//设置sheet
//sheet.setColumnWidth(2, 100);//设置某一个下标的列宽
//sheet.setDefaultColumnStyle(column, style);//设置某一列的默认样式
sheet.setDefaultColumnWidth(20);//设置所有列的列宽
// sheet.setColumnHidden(columnIndex, hidden);//设置某一列是否隐藏
//sheet.setDefaultRowHeight((short)(30*20));//设置行高60
//sheet.setDefaultRowHeightInPoints(30);//设置行高30
//的sheet上创建行
int rownum=0;
/*HSSFRow row01 = sheet.createRow(rownum);
//在row01上创建单元格
HSSFCell cell_row01 = row01.createCell(0);
//向cell_row01写东西
cell_row01.setCellValue("用户数据");
//设置标题样式
HSSFCellStyle titleStyle=createTitleCellStyle(workbook);
cell_row01.setCellStyle(titleStyle);
//第二行
rownum++;*/
HSSFRow row03 = sheet.createRow(rownum);
//员工姓名 公司名称 性别 手机号 办公楼宇 办公室 体温 扫码地点 扫码时间
String[] titles={"员工姓名","公司名称","性别","手机号","办公楼宇","办公室","体温","扫码地点","扫码时间"};
//得到表头的样式
HSSFCellStyle tableTitleStyle=createTableTitleStyle(workbook);
for (int i = 0; i < titles.length; i++) {
HSSFCell cell = row03.createCell(i);
cell.setCellValue(titles[i]);
cell.setCellStyle(tableTitleStyle);
}
HSSFCellStyle tableBodyStyle=setRowCellCenter(workbook);
for (int i = 0; i < datalist.size(); i++) {
//${item.staff_name} ${item.enterprise_name} ${item.staff_sex} ${item.staff_tel} ${item.staff_addr} ${item.addr_roomno} ${item.info_tem} ${item.addr_name} ${item.time}
rownum++;
HSSFRow row = sheet.createRow(rownum);
Map<String,Object> entity =datalist.get(i);
//创建idcell
HSSFCell staff_nameCell = row.createCell(0);
staff_nameCell.setCellValue(entity.get("staff_name").toString());
staff_nameCell.setCellStyle(tableBodyStyle);
//创建namecell
HSSFCell enterprise_nameCell = row.createCell(1);
enterprise_nameCell.setCellValue(entity.get("enterprise_name").toString());
enterprise_nameCell.setCellStyle(tableBodyStyle);
//创建addresscell
HSSFCell staff_sexCell = row.createCell(2);
staff_sexCell.setCellValue(entity.get("staff_sex").toString());
staff_sexCell.setCellStyle(tableBodyStyle);
HSSFCell staff_telCell = row.createCell(3);
staff_telCell.setCellValue(entity.get("staff_tel").toString());
staff_telCell.setCellStyle(tableBodyStyle);
HSSFCell staff_addrCell = row.createCell(4);
staff_addrCell.setCellValue(entity.get("staff_addr").toString());
staff_addrCell.setCellStyle(tableBodyStyle);
HSSFCell addr_roomnoCell = row.createCell(5);
addr_roomnoCell.setCellValue(entity.get("addr_roomno").toString());
addr_roomnoCell.setCellStyle(tableBodyStyle);
HSSFCell info_temCell = row.createCell(6);
info_temCell.setCellValue(entity.get("info_tem").toString());
info_temCell.setCellStyle(tableBodyStyle);
HSSFCell addr_nameCell = row.createCell(7);
addr_nameCell.setCellValue(entity.get("addr_name").toString());
addr_nameCell.setCellStyle(tableBodyStyle);
HSSFCell timeCell = row.createCell(8);
timeCell.setCellValue(entity.get("time").toString());
timeCell.setCellStyle(tableBodyStyle);
}
//导出数据
//workbook.write(new File(path));
String fileName = System.currentTimeMillis() + ".xls";
/*String path = getClass().getClassLoader().getResource("/").getPath()*/
String path =request.getSession().getServletContext().getRealPath("/")
+ "upload/" + fileName;
//System.out.println(path);
File file = new File(path);
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
OutputStream fOut = new FileOutputStream(file);
// 设置response参数,可以打开下载页面
response.reset();
//设置响应文本格式
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition",
"attachment;filename=" + new String(("扫码记录" + ".xls").getBytes(), "iso-8859-1"));
//将文件输出到页面
ServletOutputStream out = response.getOutputStream();
workbook.write(out);
fOut.flush();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("导出完成");
}
}