使用Struts和POI实现Excel导出下载功能
网上找了很多关于Excel导出的功能,发现有很多问题,有些导出Excel里面是空白的,并没有数据,有些是通过直接保存在D盘上,很明显这些并不是我们想要的结果,通过东凑西凑终于把导出的功能实现了,为了防止更多的人少走弯路,我把代码贴出来
1.我使用的是Struts和POI实现导出的,所以要保证你已经安装好了Struts环境和Poi的jar包导入 我下载的的POI地址是:http://mirror.bit.edu.cn/apache/poi/dev/bin/poi-bin-3.16-beta2-20170202.tar.gz
2.我的功能实现是完整的,分为action、service、ServiceImpi,具体代码如下
首先 action中downTable类的代码
package cn.muke.ssh.action;
import java.io.File;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import com.opensymphony.xwork2.ActionSupport;
import cn.muke.ssh.service.IExcelService;
import cn.muke.ssh.service.impi.IExcelServiceimpl;
public class downTable extends ActionSupport {
InputStream excelStream;
private String downloadFileName; //下载的文件名
@Override
public String execute() throws Exception {
IExcelServiceimpl ess = new IExcelServiceimpl();
excelStream = ess.getExcelInputStream();
System.out.println("4");
return "excel";
}
public InputStream getExcelStream() {
return excelStream;
}
/**
* 设置文件名称,downloadFileName要和Struts.xml中的filename对应,
* @return
*/
public String getDownloadFileName() {
String filePath="";
Date dt = new Date();
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String date = df.format(dt).toString();
filePath = "NetDevice" + date + ".xls";
return filePath;
}
}
3、Service中父类IExcelService的接口代码
package cn.muke.ssh.service;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public interface IExcelService {
InputStream getExcelInputStream() throws FileNotFoundException, IOException;
}
4、ServiceImpI中实现类的代码
package cn.muke.ssh.service.impi;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import cn.muke.ssh.service.IExcelService;
public class IExcelServiceimpl implements IExcelService {
@Override
public InputStream getExcelInputStream() throws IOException {
InputStream excelFile;
String[] title = {"id","name","sex"};
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow row = sheet.createRow(0);
HSSFCell cell = null;
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
}
for (int i = 1; i <= 10; i++) {
HSSFRow nextrow = sheet.createRow(i);
HSSFCell cell2 = nextrow.createCell(0);
cell2.setCellValue("a" + i);
cell2 = nextrow.createCell(1);
cell2.setCellValue("user" + i);
cell2 = nextrow.createCell(2);
cell2.setCellValue("钟成明"+i);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
workbook.write(out);
excelFile = new ByteArrayInputStream(out.toByteArray());
excelFile.close();
return excelFile;
}
}
5、Struts.xml的配置
<action name="excelAction" class="cn.muke.ssh.action.downTable">
<result name="excel" 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>
</action>
总结:把三个类建完之后,配置Struts的代码然后就可以了,浏览器中输入localhost:8080/mydemohibernate/excelAction就可以跳出下载框