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;
* 导出充电桩故障率统计
*/
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;
相关文章推荐
- POI导出大量数据的简单解决方案(附源码)-Java-POI导出大量数据,导出Excel文件,压缩ZIP(转载自iteye.com)
- JAVA利用POI导出Excel模板文件
- java 利用org.apache.poi.hssf包编写excel导出文件
- Java程序员从笨鸟到菜鸟之(一百零五)java操作office和pdf文件(三)利用jxl实现数据导出excel报表以及与POI的区别
- java poi导出EXCEL xls文件代码
- Java 使用POI导出Excel文件
- Java生成excel导出文件(使用poi+JXL)
- java poi 导出.xls的Excel报表 IE11导出文件名称后缀是.action,待修复
- Java程序员从笨鸟到菜鸟之(一百零四)java操作office和pdf文件(二)利用POI实现数据导出excel报表
- java poi导出excel文件
- java poi Excel文件导出工具类
- 【JavaWeb开发】使用java实现简单的Excel文件的导入与导出(POI)
- Java程序员从笨鸟到菜鸟之(一百零五)java操作office和pdf文件(三)利用jxl实现数据导出excel报表以及与POI的区别
- Java程序员从笨鸟到菜鸟之(一百零四)java操作office和pdf文件(二)利用POI实现数据导出excel报表
- java学习之--导入导出excel文件 通过poi
- 使用POI实现在java程序中导入导出Excel文件数据
- java poi 导出excel
- 【转】POI导出Excel文件与JVM内存的关系
- java 导出excel文件
- Java导出Excel文件实例