1 在项目中插入poi插件的jar包
路径为:项目名\WebRoot\WEB-INF\lib
2 项目添加struts2的支持
3 新建一个jsp页面 内容为
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML >
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<a href="excel.action">下载文件</a>
</body>
</html>
4 新建一个action,用来实现导出功能
(这里没有连接数据库,并非动态获取数据s)
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
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;
//以上是导入的包的地址
@SuppressWarnings("serial")
public class ExcelDownloadAction extends ActionSupport {
public InputStream getExcelFile() {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("sheet1");
{
// 创建表头
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("编号");
cell = row.createCell((short) 1);
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue("姓名");
cell = row.createCell((short) 2);
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue("性别");
cell = row.createCell((short) 3);
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue("年龄");
// 创建数据
// 第一行
row = sheet.createRow(1);
cell = row.createCell((short) 0);
cell.setCellValue("1");
cell = row.createCell((short) 1);
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue("张三");
cell = row.createCell((short) 2);
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue("男");
cell = row.createCell((short) 3);
cell.setCellValue("21");
// 第二行
row = sheet.createRow(2);
cell = row.createCell((short) 0);
cell.setCellValue("2");
cell = row.createCell((short) 1);
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue("李四");
cell = row.createCell((short) 2);
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue("女");
cell = row.createCell((short) 3);
cell.setCellValue("21");
// 第三行
row = sheet.createRow(3);
cell = row.createCell((short) 0);
cell.setCellValue("3");
cell = row.createCell((short) 1);
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue("王五");
cell = row.createCell((short) 2);
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellValue("男");
cell = row.createCell((short) 3);
cell.setCellValue("15");
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
workbook.write(baos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] ba = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(ba);
return bais;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return super.execute();
}
}
小伙伴们可能已经发现了,创建数据的过程完全可以通过循环来实现,在这里小编就给大家提供一个思路,其他的不过多介绍了:
如果想要动态获取数据的话,只需要写一个 findall()方法,最好是返回的结果类型是数组,
然后在生成标题后,采用循环的方法,来给表单数据进行赋值。
5最后一步,配置struts.xml文件。
<struts>
<package name="default" extends="struts-default">
<action name="excel" class="ExcelDownloadAction">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename="AllUsers.xls"</param>
<param name="inputName">excelFile</param>
</result>
</action>
</package>
</struts>
⒈在这里,要保证class与 你编辑的action名字一致。
2.contentType,是指处理excel文件的方式处理发过来的内容。
3attachment ,是指 再点击导出excel的时候是否给与提示。
4 filename 这个,顾名思义就是 导出的文件名字。