//导出列表基本表
@Override
public String exportdata(HttpServletResponse response,HttpServletRequest request,String starttime, String endtime, String flowstate) throws Exception {//导出数据
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss");
String fileName ="故障汇总表"+sdf2.format(System.currentTimeMillis())+".xls"; //文件名 故障汇总表
boolean result=true;
//将文件存到指定位置
String url = "http://" + PropertyLoader.ip + ":" + request.getLocalPort() + request.getContextPath()
+ "/download/"+ fileName;
// this.setResponseHeader(response, fileName);
// OutputStream os = response.getOutputStream();
String path = request.getServletContext().getRealPath("download");
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
//String path="C:/Users/Administrator/Desktop/"+fileName;
String path1=request.getServletContext().getRealPath("download"+ File.separator + fileName);
//导出地址
OutputStream os=new FileOutputStream(path1);
String sheetName = "网络故障与客服报障汇总表";//sheet名
String []title = new String[]{"序号","故障发生日期(xx月 xx日)","故障发生时间","故障发现时间(监控人员发现并告知值班长的时间)",
"信息发布时间","值班原始团队","监控专业","最终故障级别","故障网元","故障现象","对应的故障工单号","联系专业支撑时间支撑到位时间支撑未到位原因",
"具体故障原因","专业室或分公司反馈故障恢复信息的时间(若是监控主动询问,则填写主动询问的时间)","故障解决办法及恢复时间","受影响的业务/范围",
"恢复短信发送时间","发现手段","故障发现人","记录人","短信下发内容","故障责任归属(地市/省公司)","是否人为"};//标题
List<FaultEventBasics> list=faultEventBasicsDao.chartlist(starttime,endtime,flowstate);//内容list
log.info("导出数据:"+list.size()+"条");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
SimpleDateFormat sdf3 = new SimpleDateFormat("HH:mm");
String [][]values = new String[list.size()][];
Map<String, String> mapcode=QueryByDictCode("faultLevel");
Map<String, String> ascription=QueryByDictCode("ascription");
for(int i=0;i<list.size();i++){
values[i] = new String[title.length];
//将对象内容转换成string
FaultEventBasics obj = list.get(i);
FaultTypeInfo faultTypeInfo = null;
try {
faultTypeInfo = faultTypeInfoDAO.getListFti(obj.getId()).get(0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//类型表
values[i][0] = i+1+"";//序号
if(obj.getOccur_date()!=null){
values[i][1] = sdf.format(obj.getOccur_date());//故障发生日期
}else{
values[i][1]="";
}
values[i][2] = obj.getOccur_time();//故障发生时间
values[i][3] = obj.getFind_time();//故障发现时间
if(faultTypeInfo!=null){
values[i][4] = sdf3.format(faultTypeInfo.getMessage_issued_time());//信息发布时间(类型表)
//values[i][20] = faultTypeInfo.getSend_content();//短息下发内容(类型表)
}else{
values[i][4] = "";//信息发布时间(类型表)
//values[i][20] = "";//短息下发内容(类型表)
}
values[i][5] = obj.getZb_team();//值班原始团队
values[i][6] = obj.getMonitor_major();//监控专业
if(mapcode!=null){
values[i][7] =mapcode.get(obj.getFinal_fault_level()+"");//最终故障级别
}else{
values[i][7]=obj.getFinal_fault_level()+"";
}
values[i][8] = obj.getFault_ne();//故障元网
values[i][9] = obj.getFault_pheno();//故障现象
values[i][10] = obj.getFault_job_num();//对应的故障工单号
values[i][11] = obj.getContact();//联系专业支撑时间支撑到位时间支撑未到位原因
values[i][12] = obj.getFault_reason();//具体故障原因
values[i][13] = obj.getFeedback();//反馈信息
values[i][14] = obj.getJjbfjhfsj();//解决办法及恢复时间
values[i][15] = obj.getAffected_business();//受影响的业务/范围
if(obj.getHfdxfs_time()!=null&&!"".equals(obj.getHfdxfs_time())){
values[i][16] = sdf1.format(obj.getHfdxfs_time());//回复短信发送时间
}else{
values[i][16]="";
}
values[i][17] = obj.getDisc_means();//发现手段
values[i][18] = obj.getFault_fxr();//故障发现人
values[i][19] = obj.getNote_taker();//记录人
List<FaultTypeInfo> typeInfoList=faultTypeInfoDAO.getListFti(obj.getId());
String send_content="";
for(FaultTypeInfo typeInfo:typeInfoList){
send_content+=typeInfo.getSend_content()+"\n";
}
values[i][20] = send_content;//短息下发内容(类型表)
if(obj.getAscription()!=null&&!"null".equals(obj.getAscription())&&!"".equals(obj.getAscription())){
String addressnew="";
if(!"".equals(ascription)&&ascription!=null){
String[] str=obj.getAscription().toString().split(",");
for(int j=0;j<str.length;j++){
if(j==str.length-1){
if(ascription.get(str[j])==null||"null".equals(ascription.get(str[j]))){
addressnew+=str[j];
}else{
addressnew+=ascription.get(str[j]);
}
}else{
if(ascription.get(str[j])==null||"null".equals(ascription.get(str[j]))){
addressnew+=str[j]+",";
}else{
addressnew+=ascription.get(str[j])+",";
}
}
}
}else{
addressnew=obj.getAscription().toString();
}
values[i][21] = addressnew;//故障责任归属
}else{
values[i][21] = "";//故障责任归属
}
if(obj.getIs_artificial()==0){
values[i][22]="否";//是否人为
}else if(obj.getIs_artificial()==1){
values[i][22]="是";//是否人为
}else{
values[i][22]="";//是否人为
}
}
HSSFWorkbook wb =getHSSFWorkbook(sheetName, title, values, null);
wb.write(os);
//os.wait();
os.flush();
os.close();
log.info("data export success");
boolean returnFlag=true;
long star = new Date().getTime();
while(returnFlag){
long end=new Date().getTime();
if(end-star>5000){//超出五秒,直接返回导出超时;
log.info(" file is not exist;file path:"+url);
url="";
break;
}
String urlnew = "http://" + PropertyLoader.ip + ":" + request.getLocalPort() + request.getContextPath()+ "/download/";
String str= URLEncoder.encode(fileName, "utf-8");
if(exists(urlnew+str)){
log.info("file is exist");
break;
}
}
return url;
}
public boolean exists(String URLName) {//判断地址是否存在
try {
//设置此类是否应该自动执行 HTTP 重定向(响应代码为 3xx 的请求)。
HttpURLConnection.setFollowRedirects(false);
//到 URL 所引用的远程对象的连接
HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
/* 设置 URL 请求的方法, GET POST HEAD OPTIONS PUT DELETE TRACE 以上方法之一是合法的,具体取决于协议的限制。*/
con.setRequestMethod("POST");
con.setRequestProperty("Content-type", "text/html");
con.setRequestProperty("Accept-Charset", "utf-8");
con.setRequestProperty("contentType", "utf-8");
// 设置 HttpURLConnection的字符编码
//从 HTTP 响应消息获取状态码
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
e.printStackTrace();
log.info(" file is not exist;file path:"+URLName);
return false;
}
}
public HSSFWorkbook getHSSFWorkbook(String sheetName,String []title,String [][]values, HSSFWorkbook wb){
// 第一步,创建一个webbook,对应一个Excel文件
if(wb == null){
wb = new HSSFWorkbook();
}
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// 设置列宽
sheet.setColumnWidth(0, 1500);
sheet.setColumnWidth(1, 6500);
sheet.setColumnWidth(2, 6500);
sheet.setColumnWidth(3, 6500);
sheet.setColumnWidth(4, 6500);
sheet.setColumnWidth(5, 6500);
sheet.setColumnWidth(6, 6500);
sheet.setColumnWidth(7, 6500);
sheet.setColumnWidth(8, 6500);
sheet.setColumnWidth(9, 6500);
sheet.setColumnWidth(10, 6500);
sheet.setColumnWidth(11, 6500);//红色
sheet.setColumnWidth(12, 7500);
sheet.setColumnWidth(13, 7500);
sheet.setColumnWidth(14, 7500);
sheet.setColumnWidth(15, 6500);
sheet.setColumnWidth(16, 6500); //红色
sheet.setColumnWidth(17, 6500);
sheet.setColumnWidth(18, 6500);
sheet.setColumnWidth(19, 6500);
sheet.setColumnWidth(20, 15000);
sheet.setColumnWidth(21, 6500);
sheet.setColumnWidth(22, 6500);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow(0);
row.setHeight((short) 1400);// 行高
//表头生成一个字体
HSSFFont font=wb.createFont();
font.setColor(HSSFColor.WHITE.index);//字体颜色
font.setFontHeightInPoints((short)12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//字体增粗
//表头生成一个字体
HSSFFont font1=wb.createFont();
font1.setColor(HSSFColor.RED.index);//字体颜色
font1.setFontHeightInPoints((short)12);
font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//字体增粗
//内容
HSSFFont fontall=wb.createFont();
fontall.setFontHeightInPoints((short)11);
//内容 特殊
HSSFFont fontall1=wb.createFont();
fontall1.setFontHeightInPoints((short)11);
fontall1.setColor(HSSFColor.RED.index);//字体颜色
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//设置背景颜色
style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
style.setWrapText(true);// 自动换行
style.setFont(font);//把字体应用到当前的样式
// 第四步,创建单元格,并设置值表头 设置表头居中 特殊
HSSFCellStyle style1 = wb.createCellStyle();
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 左右居中
style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//设置背景颜色
style1.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
style1.setWrapText(true);// 自动换行
style1.setFont(font1);//把字体应用到当前的样式
// 第四步,创建单元格,并设置所有
HSSFCellStyle styleall = wb.createCellStyle();
styleall.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 左右居中
styleall.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
styleall.setWrapText(true);// 自动换行
styleall.setFont(fontall);
// 第四步,创建单元格,并设置所有 特殊
HSSFCellStyle styleall1 = wb.createCellStyle();
styleall1.setFont(fontall1);
styleall1.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 左右居中
styleall1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
styleall1.setWrapText(true);// 自动换行
HSSFCell cell = null;
//创建标题
for(int i=0;i<title.length;i++){
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
if(i==11||i==16){
cell.setCellStyle(style1);
}
sheet.setDefaultColumnStyle((short) i, styleall);
}
//创建内容
for(int i=0;i<values.length;i++){
row = sheet.createRow(i + 1);
row.setHeight((short) 2000);// 行高
for(int j=0;j<values[i].length;j++){
row.createCell(j).setCellValue(values[i][j]);
}
row.getCell(11).setCellStyle(styleall1);
row.getCell(16).setCellStyle(styleall1);
}
return wb;
}
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(),"ISO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.reset();
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition","attachment;filename="+fileName);
response.addHeader("Pargam","no-cache");
response.addHeader("Cache-Control","no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}