Strurts2下导出Excel文件(含下载弹出框)
2015-08-25 21:27
666 查看
项目是基于S2SH框架下做的一个导出Excel数据表的功能,包含有下载弹出框供选择下载存放路径。
前期因为跟的一个项目用户要求导出数据库中的数据到Excel表格中,参考了网上的资料研究一番之后,整理如下文:
下面是代码的部分:
struts2.xml代码部分:
<action name="*$*" class="com.lmc.action.{1}Action" method="{2}"><span style="font-family: Arial, Helvetica, sans-serif;"><!-- 通用的请求路径配置,{1}是请求的Action类,{2}是请求的方法名 --></span>
<result name="success">${URL}</result>
<!-- excel导出 -->
<result name="excel" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
<param name="bufferSize">1024</param>
<param name="inputName">excelFile</param>
</result>
</action>Action类:
package com.lmc.action;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
public class OutputExcelAction extends BaseAction {
private static final long serialVersionUID = 1L;
private InputStream excelFile;
private String downloadFileName;
//构造导出excel的名称
public String getDownloadFileName() {
String downloadFileName = System.currentTimeMillis() + ".xls";
try {
downloadFileName = new String(downloadFileName.getBytes(),"ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downloadFileName;
}
public void setDownloadFileName(String downloadFileName) {
this.downloadFileName = downloadFileName;
}
public InputStream getExcelFile() {
return excelFile;
}
public void setExcelFile(InputStream excelFile) {
this.excelFile = excelFile;
}
// 关键代码:导出
public String outputExcel() throws Exception {
// HttpServletResponse response = ServletActionContext.getResponse();/* 这里我注释的4行代码是从数据裤中获取数据的方法,返回的是List集合 */
// List<FrontProjectList> dataList = projectApplyBaseService
// .query_ProjectApply3(pqc, 0, projectApplyBaseService
// .count_queryTotalProject_consumption(pqc) + 1);
// HSSFWorkbook workbook = exportExcel(dataList);/* 把List集合传进去,我改成没有传数据,最后返回的是一个只有 */
HSSFWorkbook workbook = exportExcel();
ByteArrayOutputStream output = new ByteArrayOutputStream();
workbook.write(output);
byte[] ba = output.toByteArray();
excelFile = new ByteArrayInputStream(ba);
output.flush();
output.close();
return "excel";
}
//创建excel并填充数据方法
// public HSSFWorkbook exportExcel(List dataList) throws Exception {
public HSSFWorkbook exportExcel() throws Exception {
HSSFWorkbook workbook = null;
try {
// 这里的数据即是你要从后台取得的数据
// 创建工作簿实例
workbook = new HSSFWorkbook();
// 创建工作表实例
HSSFSheet sheet = workbook.createSheet("TscExcel");
// 设置列宽
this.setSheetColumnWidth(sheet);
// 获取样式
HSSFCellStyle style = this.createTitleStyle(workbook);
// if (dataList != null && dataList.size() > 0) {
if (true) {
// 创建第一行标题,标题名字的本地信息通过resources从资源文件中获取
HSSFRow row = sheet.createRow((short) 0);// 建立新行,创建表头
this.createCell(row, 0, style, HSSFCell.CELL_TYPE_STRING, "第一列");
this.createCell(row, 1, style, HSSFCell.CELL_TYPE_STRING, "<span style="font-family: Arial, Helvetica, sans-serif;">第二列</span>");
this.createCell(row, 2, style, HSSFCell.CELL_TYPE_STRING, "第三列");
this.createCell(row, 3, style, HSSFCell.CELL_TYPE_STRING, "第四列");
this.createCell(row, 4, style, HSSFCell.CELL_TYPE_STRING, "第五列");
this.createCell(row, 5, style, HSSFCell.CELL_TYPE_STRING, "第六列");
this.createCell(row, 6, style, HSSFCell.CELL_TYPE_STRING, "第七列");
this.createCell(row, 7, style, HSSFCell.CELL_TYPE_STRING, "第八列");
// 给excel填充数据
// for (int i = 0; i < dataList.size(); i++) {
//
// // 将dataList里面的数据取出来,假设这里取出来的是Model,也就是某个javaBean的意思啦
//
// FrontProjectList model = (FrontProjectList) dataList.get(i);
//
// HSSFRow row1 = sheet.createRow((short) (i + 1));// 建立新行
//
// this.createCell(row1, 0, style, HSSFCell.CELL_TYPE_STRING, i + 1);
//
// if (model.getXmmc() != null)
// this.createCell(row1, 1, style, HSSFCell.CELL_TYPE_STRING, model.getXmmc());
// if (model.getXmlb() != null)
// this.createCell(row1, 2, style, HSSFCell.CELL_TYPE_STRING, model.getXmlb());
// if (model.getXmdw() != null)
// this.createCell(row1, 3, style, HSSFCell.CELL_TYPE_STRING, model.getXmdw());
// if (model.getZrbm() != null)
// this.createCell(row1, 4, style, HSSFCell.CELL_TYPE_STRING, model.getZrbm());
// if (model.getJsqsn() != null)
// this.createCell(row1, 5, style, HSSFCell.CELL_TYPE_STRING, model.getJsqsn());
// if (model.getZtz() != null)
// this.createCell(row1, 6, style, HSSFCell.CELL_TYPE_STRING, model.getZtz());
// if (model.getSbsj() != null)
// this.createCell(row1, 7, style, HSSFCell.CELL_TYPE_STRING, model.getSbsj());
// }
} else {
this.createCell(sheet.createRow(0), 0, style, HSSFCell.CELL_TYPE_STRING, "查无资料");
}
} catch (Exception e) {
e.printStackTrace();
}
return workbook;
}
//设置列宽与列数
private void setSheetColumnWidth(HSSFSheet sheet) {
// 根据你数据里面的记录有多少列,就设置多少列
sheet.setColumnWidth(0, 3000);
sheet.setColumnWidth(1, 8000);
sheet.setColumnWidth(2, 3000);
sheet.setColumnWidth(3, 8000);
sheet.setColumnWidth(4, 8000);
sheet.setColumnWidth(5, 5000);
sheet.setColumnWidth(6, 5000);
sheet.setColumnWidth(7, 5000);
}
// 设置excel的title样式
private HSSFCellStyle createTitleStyle(HSSFWorkbook wb) {
HSSFFont boldFont = wb.createFont();
boldFont.setFontHeight((short) 200);
HSSFCellStyle style = wb.createCellStyle();
style.setFont(boldFont);
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("###,##0.00"));
return style;
}
// 创建Excel单元格
private void createCell(HSSFRow row, int column, HSSFCellSty
98b7
le style, int cellType, Object value) {
HSSFCell cell = row.createCell(column);
if (style != null) {
cell.setCellStyle(style);
}
switch (cellType) {
case HSSFCell.CELL_TYPE_BLANK: {
}
break;
case HSSFCell.CELL_TYPE_STRING: {
cell.setCellValue(value.toString());
}
break;
case HSSFCell.CELL_TYPE_NUMERIC: {
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(Double.parseDouble(value.toString()));
}
break;
default:
break;
}
}
}
效果如下:
前期因为跟的一个项目用户要求导出数据库中的数据到Excel表格中,参考了网上的资料研究一番之后,整理如下文:
下面是代码的部分:
struts2.xml代码部分:
<action name="*$*" class="com.lmc.action.{1}Action" method="{2}"><span style="font-family: Arial, Helvetica, sans-serif;"><!-- 通用的请求路径配置,{1}是请求的Action类,{2}是请求的方法名 --></span>
<result name="success">${URL}</result>
<!-- excel导出 -->
<result name="excel" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
<param name="bufferSize">1024</param>
<param name="inputName">excelFile</param>
</result>
</action>Action类:
package com.lmc.action;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
public class OutputExcelAction extends BaseAction {
private static final long serialVersionUID = 1L;
private InputStream excelFile;
private String downloadFileName;
//构造导出excel的名称
public String getDownloadFileName() {
String downloadFileName = System.currentTimeMillis() + ".xls";
try {
downloadFileName = new String(downloadFileName.getBytes(),"ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downloadFileName;
}
public void setDownloadFileName(String downloadFileName) {
this.downloadFileName = downloadFileName;
}
public InputStream getExcelFile() {
return excelFile;
}
public void setExcelFile(InputStream excelFile) {
this.excelFile = excelFile;
}
// 关键代码:导出
public String outputExcel() throws Exception {
// HttpServletResponse response = ServletActionContext.getResponse();/* 这里我注释的4行代码是从数据裤中获取数据的方法,返回的是List集合 */
// List<FrontProjectList> dataList = projectApplyBaseService
// .query_ProjectApply3(pqc, 0, projectApplyBaseService
// .count_queryTotalProject_consumption(pqc) + 1);
// HSSFWorkbook workbook = exportExcel(dataList);/* 把List集合传进去,我改成没有传数据,最后返回的是一个只有 */
HSSFWorkbook workbook = exportExcel();
ByteArrayOutputStream output = new ByteArrayOutputStream();
workbook.write(output);
byte[] ba = output.toByteArray();
excelFile = new ByteArrayInputStream(ba);
output.flush();
output.close();
return "excel";
}
//创建excel并填充数据方法
// public HSSFWorkbook exportExcel(List dataList) throws Exception {
public HSSFWorkbook exportExcel() throws Exception {
HSSFWorkbook workbook = null;
try {
// 这里的数据即是你要从后台取得的数据
// 创建工作簿实例
workbook = new HSSFWorkbook();
// 创建工作表实例
HSSFSheet sheet = workbook.createSheet("TscExcel");
// 设置列宽
this.setSheetColumnWidth(sheet);
// 获取样式
HSSFCellStyle style = this.createTitleStyle(workbook);
// if (dataList != null && dataList.size() > 0) {
if (true) {
// 创建第一行标题,标题名字的本地信息通过resources从资源文件中获取
HSSFRow row = sheet.createRow((short) 0);// 建立新行,创建表头
this.createCell(row, 0, style, HSSFCell.CELL_TYPE_STRING, "第一列");
this.createCell(row, 1, style, HSSFCell.CELL_TYPE_STRING, "<span style="font-family: Arial, Helvetica, sans-serif;">第二列</span>");
this.createCell(row, 2, style, HSSFCell.CELL_TYPE_STRING, "第三列");
this.createCell(row, 3, style, HSSFCell.CELL_TYPE_STRING, "第四列");
this.createCell(row, 4, style, HSSFCell.CELL_TYPE_STRING, "第五列");
this.createCell(row, 5, style, HSSFCell.CELL_TYPE_STRING, "第六列");
this.createCell(row, 6, style, HSSFCell.CELL_TYPE_STRING, "第七列");
this.createCell(row, 7, style, HSSFCell.CELL_TYPE_STRING, "第八列");
// 给excel填充数据
// for (int i = 0; i < dataList.size(); i++) {
//
// // 将dataList里面的数据取出来,假设这里取出来的是Model,也就是某个javaBean的意思啦
//
// FrontProjectList model = (FrontProjectList) dataList.get(i);
//
// HSSFRow row1 = sheet.createRow((short) (i + 1));// 建立新行
//
// this.createCell(row1, 0, style, HSSFCell.CELL_TYPE_STRING, i + 1);
//
// if (model.getXmmc() != null)
// this.createCell(row1, 1, style, HSSFCell.CELL_TYPE_STRING, model.getXmmc());
// if (model.getXmlb() != null)
// this.createCell(row1, 2, style, HSSFCell.CELL_TYPE_STRING, model.getXmlb());
// if (model.getXmdw() != null)
// this.createCell(row1, 3, style, HSSFCell.CELL_TYPE_STRING, model.getXmdw());
// if (model.getZrbm() != null)
// this.createCell(row1, 4, style, HSSFCell.CELL_TYPE_STRING, model.getZrbm());
// if (model.getJsqsn() != null)
// this.createCell(row1, 5, style, HSSFCell.CELL_TYPE_STRING, model.getJsqsn());
// if (model.getZtz() != null)
// this.createCell(row1, 6, style, HSSFCell.CELL_TYPE_STRING, model.getZtz());
// if (model.getSbsj() != null)
// this.createCell(row1, 7, style, HSSFCell.CELL_TYPE_STRING, model.getSbsj());
// }
} else {
this.createCell(sheet.createRow(0), 0, style, HSSFCell.CELL_TYPE_STRING, "查无资料");
}
} catch (Exception e) {
e.printStackTrace();
}
return workbook;
}
//设置列宽与列数
private void setSheetColumnWidth(HSSFSheet sheet) {
// 根据你数据里面的记录有多少列,就设置多少列
sheet.setColumnWidth(0, 3000);
sheet.setColumnWidth(1, 8000);
sheet.setColumnWidth(2, 3000);
sheet.setColumnWidth(3, 8000);
sheet.setColumnWidth(4, 8000);
sheet.setColumnWidth(5, 5000);
sheet.setColumnWidth(6, 5000);
sheet.setColumnWidth(7, 5000);
}
// 设置excel的title样式
private HSSFCellStyle createTitleStyle(HSSFWorkbook wb) {
HSSFFont boldFont = wb.createFont();
boldFont.setFontHeight((short) 200);
HSSFCellStyle style = wb.createCellStyle();
style.setFont(boldFont);
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("###,##0.00"));
return style;
}
// 创建Excel单元格
private void createCell(HSSFRow row, int column, HSSFCellSty
98b7
le style, int cellType, Object value) {
HSSFCell cell = row.createCell(column);
if (style != null) {
cell.setCellStyle(style);
}
switch (cellType) {
case HSSFCell.CELL_TYPE_BLANK: {
}
break;
case HSSFCell.CELL_TYPE_STRING: {
cell.setCellValue(value.toString());
}
break;
case HSSFCell.CELL_TYPE_NUMERIC: {
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(Double.parseDouble(value.toString()));
}
break;
default:
break;
}
}
}
效果如下:
相关文章推荐
- java实现excel的导入导出(poi详解)
- 用myeclipse打包工程为jar包
- C# Excel导出
- 导入导出Excel工具类ExcelUtil
- 导出Excel技术模板
- jxl导出excel(合并单元格)
- jxl excel 导出
- Excel导入导出
- thinkphp之Excel的导出
- ACCESS,文件下载,DLL操作类,EXCEL导出方法备忘
- POI 的简单版本 EasyPoi性能测试 系列一
- Excel 转成 Html Excel 预览
- 一个用poi导出excel的例子
- jsp页面表格数据导出到excel
- 强大的web打印插件--Lodop
- C#WinForm 直接导出DataGridView数据到Excel表格的二种方法对比
- 使用icexls进行java Excel导入导出
- Excel导出实现
- js 导出excel
- Java-Maven-POI 简单导入导出Excel通用工具,默认使用基于poi实现