[size=medium][b][color=blue]废话不多说
先把poi.jar文件引入到项目中,[url=http://poi.apache.org/download.html#POI-3.9]下载链接[/url]
html页面
[/color][/b][/size]
[size=medium][color=blue][b]struts.xml配置[/b][/color][/size]
[size=medium][b]<param name="inputName">execlStream</param>中的execlStream要和java文件中的get方法一致,如:getExeclStream,否则报错
fileName也要在java文件中对应,这个是excel文件名称
[/b][/size]
[size=medium][color=blue][b]java文件[/b][/color][/size]
先把poi.jar文件引入到项目中,[url=http://poi.apache.org/download.html#POI-3.9]下载链接[/url]
html页面
[/color][/b][/size]
<a href="actions/export">下载execl</a>
[size=medium][color=blue][b]struts.xml配置[/b][/color][/size]
<package name="struts2" namespace="/actions" extends="json-default">
<action name="export" class="com.truth.action.ExportAction">
<result type="stream">
<param name="encode">true</param>
<param name="contentType">application/vnd.ms-excelcharset=GBK</param>
<param name="inputName">execlStream</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>
[size=medium][b]<param name="inputName">execlStream</param>中的execlStream要和java文件中的get方法一致,如:getExeclStream,否则报错
fileName也要在java文件中对应,这个是excel文件名称
[/b][/size]
[size=medium][color=blue][b]java文件[/b][/color][/size]
package com.truth.action;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.log4j.Logger;
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 com.opensymphony.xwork2.ActionSupport;
public class ExportAction extends ActionSupport {
private static final long serialVersionUID = -7721390106400928567L;
private static final Logger log = Logger.getLogger(ExportAction.class);
private String fileName = "什sdsd.xls";
@SuppressWarnings("deprecation")
public InputStream getExeclStream() {
try {
fileName = new String(fileName.getBytes(),"ISO8859-1");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("sheet1");
{
// 创建表头
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("id");
cell = row.createCell((short) 1);
cell.setCellValue("姓");
cell = row.createCell((short) 2);
cell.setCellValue("名");
cell = row.createCell((short) 3);
cell.setCellValue("年龄");
// 创建数据
// 第一行
row = sheet.createRow(1);
cell = row.createCell((short) 0);
cell.setCellValue("1");
cell = row.createCell((short) 1);
cell.setCellValue("张");
cell = row.createCell((short) 2);
cell.setCellValue("四");
cell = row.createCell((short) 3);
cell.setCellValue("23");
// 第二行
row = sheet.createRow(2);
cell = row.createCell((short) 0);
cell.setCellValue("2");
cell = row.createCell((short) 1);
cell.setCellValue("李");
cell = row.createCell((short) 2);
cell.setCellValue("六");
cell = row.createCell((short) 3);
cell.setCellValue("30");
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
workbook.write(baos);
} catch (IOException e) {
e.printStackTrace();
}
byte[] ba = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(ba);
return bais;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}