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

struts2 利用 POI 导出 excel

2016-04-12 19:16 435 查看

第一种方式:在action中设置header等

1. 根据数据生成 HSSFWorkbook, 并将 HSSFWorkbook 写入到输出流

private void createExcel(List<String[]> data) throws Exception{
HttpServletResponse response = ServletActionContext.getResponse();
//excel 文件的 MIME 类型
response.setContentType("application/vnd.ms-excel");
String fileName = "导出数据";//需要使用<span style="font-family: Arial, Helvetica, sans-serif;">URLEncoder编码一下, 否则中文名字不会显示</span>
response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName, "utf-8") +".xls");

HSSFWorkbook wb = new HSSFWorkbook();
if(data == null || data.size() < 1){
return;
}

String sheetTitle = "sheet1";
HSSFSheet sheet = wb.createSheet();
wb.setSheetName(0,sheetTitle);

HSSFRow row=null;
HSSFCell cell = null;

for(int i=0; i<data.size(); i++){
row = sheet.createRow(i);
for(int j=0; j<data.get(i).length; j++){

cell = row.createCell(j);
cell.setCellValue(data.get(i)[j]);
}
}

OutputStream os = ServletActionContext.getResponse().getOutputStream();

wb.write(os);

if(os != null){
os.flush();
os.close();
}
}

2. 在 action 中调用该方法即可

public String export() throws Exception{

oauthInfoService.getPageInfoData(startDate, endDate, clientId, pageInfo, true);

List<String[]> data = new ArrayList<String[]>();
List<OauthInfoBean> result = pageInfo.getSearchResult();
String[] rowName = new String[]{"请求来源","授权时间","请求时间","手机号"};
data.add(rowName);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

for(OauthInfoBean bean : result){
rowName = new String[4];
rowName[0] = bean.getReqSource() == null?"":bean.getReqSource();
if(bean.getAuthTime() != null){
rowName[1] = sdf.format(bean.getAuthTime());
}else{
rowName[1] = "";
}
rowName[2] = bean.getReqTime()==null?"":bean.getReqTime();
rowName[3] = bean.getPhone()==null?"":bean.getPhone();
data.add(rowName);
}

this.createExcel(data);

return NONE;
}


3. struts2的配置文件中的配置方式

<action name="export" class="oauthInfoJsonAction" method="export">

</action>



第二种方式,在struts2的配置文件中配置

代码
/**
*
* @return
* @throws Exception
*/
public String export() throws Exception{
HttpServletResponse response = ServletActionContext.getResponse();
//excel 文件的 MIME 类型
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=授权信息数据.xls");
exportFileName = "导出数据";

oauthInfoService.getPageInfoData(startDate, endDate, clientId, pageInfo, true);

List<String[]> data = new ArrayList<String[]>();
List<OauthInfoBean> result = pageInfo.getSearchResult();
String[] rowName = new String[]{"请求来源","授权时间","请求时间","手机号"};
data.add(rowName);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
for(OauthInfoBean bean : result){
rowName = new String[4];
rowName[0] = bean.getReqSource() == null?"":bean.getReqSource();
if(bean.getAuthTime() != null){
//rowName[1] = sdf.format(bean.getAuthTime());
}else{
rowName[1] = "";
}
rowName[2] = bean.getReqTime()==null?"":bean.getReqTime();
rowName[3] = bean.getPhone()==null?"":bean.getPhone();
data.add(rowName);
}

this.createExcel(data);

return SUCCESS;
}

private void createExcel(List<String[]> data) throws Exception{
HSSFWorkbook wb = new HSSFWorkbook();
if(data == null || data.size() < 1){
return;
}

String sheetTitle = "sheet1";
HSSFSheet sheet = wb.createSheet();
wb.setSheetName(0,sheetTitle);

HSSFRow row=null;
HSSFCell cell = null;

for(int i=0; i<data.size(); i++){
row = sheet.createRow(i);
for(int j=0; j<data.get(i).length; j++){

cell = row.createCell(j);
cell.setCellValue(data.get(i)[j]);
}
}

this.exportFileName="导出数据";//设置fileName
ByteArrayOutputStream baos = new ByteArrayOutputStream();
wb.write(baos);
baos.flush();
byte[]aa=baos.toByteArray();
this.excelFileStream = new ByteArrayInputStream(aa,0,aa.length);
baos.close();
}


xml配置文件
<action name="export" class="oauthInfoJsonAction" method="export">
<result type="stream">

<param name="contentType">application/vnd.ms-excel</param>

<param name="contentDisposition">attachment;filename=123.xls</param>

<param name="bufferSize">1024</param>

<param name="inputName">excelFileStream</param>
</result>
</action>
当然在action中必须要有excelFileStream这个属性和set,get方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: