您的位置:首页 > 编程语言 > Java开发

java导出文件之Excel(Poi)上

2017-03-23 14:04 671 查看
/**
* 导出充电桩故障率统计
*/
public void export() {
Map<String, Object> param = FrameUtils.beanToMap(data);

// Json json = new Json();
param.put("userId", getUser().getId());
param.put("areacode", areacode);
HttpServletResponse getResponse=getResponse();
serviceGridStation.queryExport(param,getResponse);

// json.setSuccess(true);此处不能再次写入流

// writeJson(json);

}

---------------------------------------------------------------------------

/**

* 导出充电桩故障率统计
*/
@Override
public void queryExport(Map<String, Object> param,HttpServletResponse getResponse) {
List<StationRealInfo> list=gridStationMapper.queryExport(param);
 
String exportname="export";
getResponse.reset();
OutputStream out = null;
 
try {
exportname+=".xlsx";  
getResponse.setHeader("Content-disposition", "attachment; filename="+java.net.URLEncoder.encode(exportname, "UTF-8")+"");
getResponse.setContentType("application/msexcel;charset=utf-8"); 

 
// 声明一个工作薄
XSSFWorkbook workbook = new XSSFWorkbook();
// 生成一个表格
XSSFSheet sheet = workbook.createSheet();
sheet.setColumnWidth(0, 6000);
sheet.setColumnWidth(5, 6000);
sheet.setColumnWidth(6, 6000);
String[] headers = new String[] { "区域", "网点", "编号", "总数", "上线", "离线", "故障", "维修", "备注" };
// 产生表格标题行
XSSFRow row = sheet.createRow(0);
for (short i = 0; i < headers.length; i++) {
XSSFCell cell = row.createCell(i);
cell.setCellValue(headers[i]);
}
for (int i = 0; i < list.size(); i++) {
StationRealInfo temp =list.get(i);
XSSFRow tempR = sheet.createRow(i+1);
XSSFCell cell0 = tempR.createCell(0);
XSSFCell cell1 = tempR.createCell(1);
XSSFCell cell2 = tempR.createCell(2);
XSSFCell cell3 = tempR.createCell(3);
XSSFCell cell4 = tempR.createCell(4);
XSSFCell cell5 = tempR.createCell(5);
XSSFCell cell6 = tempR.createCell(6);
XSSFCell cell7 = tempR.createCell(7);

cell0.setCellValue(temp.getAreaName());
cell1.setCellValue(temp.getGridStationName());
cell2.setCellValue(temp.getStationNo());
cell3.setCellValue(temp.getStakeCount());
cell4.setCellValue(temp.getOkStackNum());
cell5.setCellValue(temp.getOfflineStackNum());
cell6.setCellValue(temp.getNfaultStackNum());
cell7.setCellValue(temp.getRemark());
}

try {  
               out = getResponse.getOutputStream();  //字节流
               workbook.write(out);
               out.flush();//缓存清空输出
               out.close();  
               out=null;  
               getResponse.flushBuffer();
           } catch (IOException e) {  
               // TODO Auto-generated catch block  
               e.printStackTrace();  
           }  
} catch (UnsupportedEncodingException e2) {
e2.printStackTrace();

}  

=========js========================

var data=sy.serializeObject($('#searchForm'));
if(data==null){
parent.alertify.alert("提示","请输入查询的区域!").set('label', '确定');
return;
}
DownLoad({
url : sy.contextPath + '/charge/grid-station!export.sy',
data:data
});

var DownLoad = function (options) {

    var config = $.extend(true, { method: 'post' }, options);

    var $iframe = $('<iframe id="down-file-iframe" />');

    var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');

    $form.attr('action', config.url);

    for (var key in config.data) {

        $form.append('<input type="text" name="' + key + '" value="' + config.data[key] + '" />');

    }

    $form.append('<input type="hidden" name="hfs" id="export" >');

    $iframe.append($form);

    $(document.body).append($iframe);

    

    $("#export").val(config.data["hf"]);

    $form[0].submit();

    $iframe.remove();

}

-------------------------oss上传分布式--------------------------------

/*OutputStream out = null;
String fileName = null;
try {
File updateFile = File.createTempFile("result", ".xlsx");
out = new FileOutputStream(updateFile);
workbook.write(out);
fileName = "excel/result/" + ServletActionContext.getRequest().getSession().getId() + "-充电桩状况导出结果.xlsx";
ossUtil.uploadFile(updateFile, fileName);

// result.setUrl( filePath + fileName);

// getResponse().setContentType("text/html;charset=utf-8");

// getResponse().getWriter().write("{\"url\":\"" + filePath + fileName + "\",\"status\":\"Succes\"}");

// getResponse().getWriter().flush();

// getResponse().getWriter().close();

System.err.println("-=-=-=-=-=over-=-=-=-=-=");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}*/

-------------------------------------上传本地服务器----(分布式不行)------------------------------------

ServletRequest request = ServletActionContext.getRequest();
@SuppressWarnings("deprecation")
String uploadFilePath = new File(request.getRealPath("/"))
.getPath();
String savePdfPath = uploadFilePath + "/uploadExcel";
File up = new File(savePdfPath);
if (!up.exists()) {
up.mkdirs();
}

String sysdate = IsvCom.getSystemDate(IsvCom.DATE_TYPE3);

FileOutputStream exportFile = new FileOutputStream(savePdfPath + "/" + sysdate + ".xls");
wb.write(exportFile);
exportFile.close();

----------------js----------------

var newTab = window.open('about:blank');

newTab.location.href = data.msg;





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  excel