`

poi ,Struts2 导出Excel运用(二)

 
阅读更多

在我的poi Struts2运用(一)中,很明显看到 ,这样只能操作一般的简单的报表,如果是要导出的Excel的机构很复杂的话,那代码可想而知。

所以在这里我得用到了Excel模板

意味着我们首先将要到处的Excel 模板话,我们的代码只负责 向模板中塞值即可!

首先要说的Struts2的配置文件还是不变,

以我的会员信息导出Excel为例:

<action name="memberToExcel" class="memberToExcelAction" method="memberToExcel">

 

 

<result name="success" type="stream">

 

 

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

 

 

<param name="inputName">excelStream</param>

 

 

<param name="contentDisposition">attachment;filename="${downloadFileName}"</param>

 

 

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

 

 

</result>

 

 

<result name="error">/comm/error.jsp</result>

 

 

</action>

对应的memberToExcelAction.java

我只写关键代码:

 

private Integer from1Id;//这是我从js 从跳转的action中获取的参数值

private InputStream excelStream; //输入流变量

 

 

 

 

 

 

public String memberToExcel()throws Exception{

 

Integer id=from1Id;

 

 

MemberDto dto=memberService.getMember(id);

 

 

AccountDto accountDto = (AccountDto)session.get(Constants.USER_SESSION_KEY);

 

 

String operatorName=accountDto.getName();

 

 

String filePath=ServletActionContext.getServletContext().getRealPath("/download/登记表.xls"); //获得绝对路径 主要作用是向Service实现类中传入 模板的路径

 

 

excelStream=memberToExcelService.memberToExcel(dto,filePath,operatorName);

 

 

return SUCCESS;

 

 

}

 

 

 

 

 

 

 

public String getDownloadFileName() {

 

 

SimpleDateFormat sf = new SimpleDateFormat( "yyyy-MM-dd ");

 

 

String downloadFileName= (sf.format(new Date()).toString())+"登记表.xls";

 

 

try{

 

 

downloadFileName=new String(downloadFileName.getBytes(),"ISO8859-1");

 

 

}catch(UnsupportedEncodingException e){

 

 

e.printStackTrace();

 

 

}

 

 

return downloadFileName;

 

 

 

 

}

 

 

public InputStream getExcelStream() {

 

 

return excelStream;

 

 

}

 

publicvoidsetExcelStream(InputStream excelStream) {

 

 

this.excelStream= excelStream;

 

 

}

 

 

 

 

public Integer getFrom1Id() {

 

 

return from1Id;

 

 

}

 

 

 

 

 

public void setFrom1Id(Integer from1Id) {

 

 

this.from1Id = from1Id;

 

 

}

 

 

 

 

我的Service实现类MemberToExcelServiceImpl

 

 

 

 

public InputStream memberToExcel(MemberDto dto,String filePath,String operatorName) throws ServiceException{

 

 

String fileToBeRead =filePath;

 

 

InputStream excelStream=null;

 

 

File file=new File(fileToBeRead);

 

 

if(file.exists()){

 

 

try{

 

 

// 创建对Excel工作簿文件的引用

 

 

InputStream is=new FileInputStream(file);//注意了这里引用了模板

 

 

try{//这个捕捉IOException异常

 

 

// 创建一个HSSFWorkbook

 

 

HSSFWorkbook wb = new HSSFWorkbook(is);

 

 

//HSSFWorkbook获取第一个HSSFSheet

 

 

HSSFSheet sheet = wb.getSheetAt(0);

 

 

// HSSFSheet创建HSSFRow

 

 

HSSFRow row = sheet.createRow((short)2);

 

 

 

//填充第一行

HSSFCell cell = row.createCell((short)2);

 

 

cell = row.createCell((short)1);

 

 

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

 

 

cell.setCellValue(dto.getCardNo().toString());

 

 

 

 

cell = row.createCell((short)4);

 

 

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

 

 

cell.setCellValue(dto.getEnterDate()==null?"":getDateToString(dto.getEnterDate()));

 

 

 

 

cell = row.createCell((short)6);

 

 

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

 

 

cell.setCellValue(getCheckboxContents(dto.getRemind(),Constants.DIC_TYPE_REMINDS));

 

 

 

 

cell = row.createCell((short)7);

 

 

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

 

 

cell.setCellValue(getCheckboxContents(dto.getInterest(),Constants.DIC_TYPE_INTERESTS));

 

 

 

 

//填充第二行

row = sheet.createRow((short)3);

 

 

cell = row.createCell((short)1);

 

 

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

 

 

 

 

MemberLevel entity=memberLevelDAO.getMemberLevel(dto.getLevelId());

 

 

cell.setCellValue(entity.getName().toString());

 

 

 

 

cell = row.createCell((short)4);

 

 

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

 

 

cell.setCellValue(dto.getVisitPeriod().toString()+"");

 

 

 

 

 

//填充第三行

row = sheet.createRow((short)5);

 

 

 

 

cell = row.createCell((short)1);

 

 

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

 

 

DictionaryDto dictionaryDto1=dictionaryDAO.getDictionary(dto.getCustomer().getLicenceType(), Constants.DIC_TYPE_LICENCETYPES);

 

 

cell.setCellValue(dictionaryDto1.getCodeName().toString());

 

 

 

 

cell = row.createCell((short)3);

 

 

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

 

 

cell.setCellValue(dto.getCustomer().getNextDate()==null?"":getDateToString(dto.getCustomer().getNextDate()));

 

 

 

 

cell = row.createCell((short)5);

 

 

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

 

 

cell.setCellValue(dto.getCustomer().getValidatePeriod().toString()+"年度");

 

 

 

 

//填充第四行

 

 

row = sheet.createRow((short)6);

 

 

cell = row.createCell((short)1);

 

 

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

 

 

cell.setCellValue(dto.getCustomer().getName().toString());

 

 

 

 

cell = row.createCell((short)3);

 

 

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

 

 

DictionaryDto

dictionaryDto2=dictionaryDAO.getDictionary(dto.getCustomer().getLicenceType(), Constants.DIC_TYPE_GENDORS);

cell.setCellValue(dictionaryDto2.getCodeName().toString());

 

 

 

 

cell = row.createCell((short)5);

 

 

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

 

 

cell.setCellValue(dto.getCustomer().getBirthday()==null?"":getDateToString(dto.getCustomer().getBirthday()));

 

 

//填充第五行

row = sheet.createRow((short)7);

 

 

cell = row.createCell((short)1);

 

 

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

 

 

cell.setCellValue(dto.getCustomer().getMobile().toString());

 

 

 

 

cell = row.createCell((short)3);

 

 

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

 

 

cell.setCellValue(dto.getCustomer().getPhone().toString());

 

 

 

 

cell = row.createCell((short)5);

 

 

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

 

 

cell.setCellValue(dto.getCustomer().getFax().toString());

。。。。。。。。(代码一样都是填充省略)。

//使用apachecommons-lang.jar产生随机的字符串作为文件名

String fileName=RandomStringUtils.randomAlphanumeric(10);

 

 

//生成xls文件名必须要是随机的,确保每个线程访问都产生不同的文件

 

 

StringBuffer sb=new StringBuffer(fileName);

 

 

final File fileRandom = new File(sb.append(".xls").toString());

 

 

try {

 

 

OutputStream os=new FileOutputStream(fileRandom);

 

 

try {

 

 

wb.write(os);

 

 

os.close();

 

 

} catch (IOException e) {

 

 

e.printStackTrace();

 

 

}

 

 

} catch (FileNotFoundException e) {

 

 

e.printStackTrace();

 

 

}

 

 

 

 

try {

 

 

excelStream=new FileInputStream(fileRandom);

 

 

} catch (FileNotFoundException e) {

 

 

e.printStackTrace();

 

 

}

 

 

}

 

 

catch(IOException ex){

 

 

ex.printStackTrace();

 

 

}

 

 

}

 

 

catch(FileNotFoundException e) {

 

 

e.printStackTrace();

 

 

}

 

 

}

 

 

return excelStream;

 

 

}

 

 

 

 

/**

 

 

* deal with checkbox我这里是由于数据库含有复选框的值 格式x,x,x所以我得转化成数组 在遍历

 

 

* @param str

 

 

* @param constants

 

 

* @return String

 

 

*/

 

 

 

 

public String getCheckboxContents(String str,String constants){

 

 

String MixContents="";

 

 

String array[]=str.split(",");

 

 

if(array[0].equals("")==false){

 

 

for(int i=0;i<array.length;i++){

 

 

Integer codeId=Integer.parseInt(array[i]);

 

 

DictionaryDto dto=dictionaryDAO.getDictionary(codeId, constants);

 

 

MixContents+=dto.getCodeName()+",";

 

 

}

 

 

}

 

 

return MixContents;

 

 

}

 

 

 

 

/**

 

 

* change date to String

 

 

* @param date

 

 

* @return String

 

 

*/

 

 

 

 

public String getDateToString(Date date) {

 

 

SimpleDateFormat sf = new SimpleDateFormat( "yyyy-MM-dd ");

 

 

String date2="";

 

 

try{

 

 

date2= sf.format(date).toString();

 

 

}catch(Exception e){

 

 

e.printStackTrace();

 

 

}

 

 

return date2;

 

 

}

 

 

 

我的member.jsp

<a href="#" onClick="Redirect(${member.memberId});">

 

 

<img src="<%=ctxPath%>/image/admin/upload.gif"/>导出Excel</a>

 

 

 

 

<div>

 

 

<s:form id="form1" name="form1" >

 

 

<input type="hidden" name="from1Id" id="from1Id"/>//作用是将要传递的参数放到form的元素中,这样在action可以获取到。

</s:form>

 

 

</div>

 

 

我的membe.js(负责form跳转到另一个action)

 

 

function Redirect(id){

 

 

document.form1.from1Id.value=id;//参数

 

 

document.form1.action= "memberToExcel.do";

 

 

document.form1.submit();

 

 

 

 


 

 

 

 

分享到:
评论

相关推荐

    struts2 poi导出到excel

    接下来,我们创建一个Struts2的动作类(Action),在这个类中定义导出Excel的方法。这个方法通常会接收一些参数,如查询条件,然后根据这些条件从数据库或其他数据源获取数据。例如: ```java public class ...

    Java Struts2+poi插件 实现导出Excel

    这个项目"Java Struts2+poi插件 实现导出Excel"就是结合这两个工具,为用户提供一个功能,能够将数据导出到Excel格式的文件中。 首先,让我们详细了解一下Struts2。Struts2的核心是Action,它负责接收请求、处理...

    POI+struts2导出Excel

    标题中的“POI+struts2导出Excel”是指使用Apache POI库与Struts2框架结合,实现在Web应用程序中导出数据到Excel的功能。Apache POI是Java平台上的一个开源项目,它允许开发者创建、修改和显示Microsoft Office格式...

    使用poi从数据库导出excel表的示例

    总结起来,"使用poi从数据库导出excel表的示例"是一个结合了Struts1 MVC框架和Apache POI库的Java编程任务,它涉及数据库连接、SQL查询、Excel文件生成以及Web应用响应。这个过程不仅有助于数据的高效管理和分享,也...

    struts2中利用poi导出excel

    本篇文章将深入探讨如何在Struts2框架中使用POI库来导出Excel文件。 首先,我们需要在项目中引入Apache POI库。可以通过Maven或Gradle将其添加到构建文件中。对于Maven,可以在pom.xml文件中添加以下依赖: ```xml...

    struts2导出Excel poi

    在Struts2中,Action可以通过Result返回不同的视图,比如JSP、XML或流文件,这正是我们导出Excel所需的。 接下来,我们引入Apache POI。POI提供了读写Microsoft Office文件的API,对于Excel,主要使用HSSFWorkbook...

    struts2+poi实现导出Excel文件

    Struts2 和 Apache POI 的结合使用主要集中在创建 Web 应用程序中导出 Excel 文件的功能上。Apache POI 是一个 Java 库,允许开发者创建、修改和显示 Microsoft Office 格式的文件,其中包括 Excel。而 Struts2 是一...

    Struts2 poi动态导入导出Excel源码示例

    首先,让我们深入理解一下如何使用Struts2和POI来实现Excel的导入导出。 1. **Struts2的Action配置**: 在Struts2中,你需要创建一个Action类来处理用户请求。这个Action类会有一个方法,专门处理导入或导出Excel...

    struts2+poi导出excel表格完整例子

    在这个“struts2+poi导出excel表格完整例子”中,我们将深入探讨这两个工具如何协同工作,实现从Web应用导出数据到Excel电子表格的功能。 首先,让我们了解一下Struts2的工作原理。Struts2基于拦截器(Interceptor...

    struts2实现excel导出

    在Struts2框架中,实现Excel导出的功能主要依赖于Apache POI库,这是一个用于读写Microsoft Office格式档案的Java库。以下将详细介绍如何利用Struts2和POI实现Excel导出。 首先,你需要在项目中引入Apache POI库。...

    基于struts2 spring ibatis poi开发的导出Excel实例

    【基于Struts2 Spring iBatis POI开发的导出Excel实例详解】 在现代Web应用程序中,导出数据到Excel格式是一种常见的需求,这有助于用户分析、存储或共享信息。本实例将详细介绍如何利用Struts2、Spring和iBatis...

    struts2导出excel笔记

    通过以上步骤,我们可以实现一个简单的Struts2应用,该应用能够根据用户请求,使用Apache POI库动态生成并导出Excel文件。在实际项目中,可能还需要考虑数据过滤、排序、分页等功能,以及与数据库的交互,这些都可以...

    struts2导出excel.rar

    本压缩包"struts2导出excel.rar"提供了关于如何在Struts2框架中实现Excel导出功能的相关资源。 1. **Struts2框架基础** Struts2是一个基于MVC(Model-View-Controller)设计模式的Java框架,它简化了MVC的实现,...

    struts,poi导出excel

    在 Java 开发中,如果你需要导出数据到 Excel,Apache POI 提供了高效的 API 来实现这一功能。在本文中,我们将深入探讨如何使用 POI 组件来创建和操作 Excel 文档。 1. **创建 Excel 文档对象** 创建一个 Excel ...

    Poi动态导入导出Excel基于Struts2 Hibernate Spring源码工程实例

    1.本动态导入导出Excel工程导入Eclipse即可用行,支持所有版本的Excel导入导出。...2.程序使用Struts2+POI+SQLSever(SSH即Struts2+Hibernate+Spring)实现Excel动态数据导入和导出,压缩包内含程序源码文件。

    POI + Struts2 实现Excel导出工具包

    4. **配置Struts2 Action**:创建一个Struts2 Action类,声明一个方法用于导出Excel,该方法的返回类型应配置为一个特定的Result类型,如stream或者excel,这会使得Struts2将结果流直接发送到浏览器。 5. **设置...

    struts2+poi实现excel表格导出

    通过以上讲解,我们可以看到,结合Struts2和Apache POI,我们可以轻松实现动态生成和导出Excel表格的功能,满足企业级Web应用的需求。提供的文档和网页资源应该会进一步细化这一过程,帮助开发者更好地理解和实践。

    struts2+poi 实现Excel导出

    在导出Excel的场景中,Action通常会负责接收用户请求,处理业务逻辑,并将结果返回给用户。 2. **Apache POI库**:Apache POI提供了HSSF(Horrible Spreadsheet Format)和XSSF(Extensible Spreadsheet Format)两...

    Struts1.2导出Excel表

    在这个“Struts1.2导出Excel表完美版”中,我们将深入探讨如何利用Struts1.2框架来实现这一功能。 首先,导出Excel表的核心技术是Apache POI库,这是一个用于读写Microsoft Office格式档案的Java库。在Struts1.2...

Global site tag (gtag.js) - Google Analytics