首先生成Excel
import java.io.IOException;
import java.io.OutputStream;
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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import com.acbee.model.User;
//生成Excel并输出的类
public class CreateExcel {
@SuppressWarnings("deprecation")
public String Create()
{
String filename="student";
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("学生表一");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("学号");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("年龄");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("生日");
cell.setCellStyle(style);
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
//List list = CreateSimpleExcelToDisk.getStudent();
// for (int i = 0; i < list.size(); i++)
// {
// row = sheet.createRow((int) i + 1);
// User user = (User) list.get(i);
// row.createCell((short) 0).setCellValue((double) user.getUser_pk());
// row.createCell((short) 1).setCellValue(user.getUser_name());
// row.createCell((short) 2).setCellValue(user.getUser_number());
// cell = row.createCell((short) 3);
// cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(user.getUser_birthday()));
// }
for (int i = 0; i < 3; i++)
{
row = sheet.createRow((int) i + 1);
//User user = (User) list.get(i);
row.createCell((short) 0).setCellValue(1);
row.createCell((short) 1).setCellValue(2);
row.createCell((short) 2).setCellValue(3);
row.createCell((short) 3).setCellValue(3);
}
// 第六步,将文件通过 HttpServletResponse输出
/*
* 下面的可以不用编写,直接拷贝
*/
HttpServletResponse response = null;// 创建一个HttpServletResponse对象
OutputStream out = null;// 创建一个输出流对象
try {
System.out.println("io");
response = ServletActionContext.getResponse();// 初始化HttpServletResponse对象
out = response.getOutputStream();//
response.setHeader("Content-disposition", "attachment; filename=" + filename+".xls");
//filename是下载的xls的名
response.setContentType("application/msexcel;charset=UTF-8");// 设置类型
response.setHeader("Pragma", "No-cache");// 设置头
response.setHeader("Cache-Control", "no-cache");// 设置头
response.setDateHeader("Expires", 0);// 设置日期头
wb.write(out);
out.flush();
wb.write(out);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
System.out.println("out is close");
//out.close();
}
}
return null;
}
}
在Action中调用生成excel 的方法
CreateExcel excel=new CreateExcel();
public String export()
{
System.out.println("导出");
url=excel.Create();
//return SUCCESS;
System.out.println(url);
return null;//此处必须是null否则报错
}
ExtJs中的调用代码
//导出按钮的事件
var export_excel=function(btn)
{
//Ext.ajax省略
//调用export.action导出excel
//此处必须这种写法,不然无法弹出保存excel的框
window.location="export.action";
}
struts.xml中的配置
<package name="extjs" extends="json-default" namespace="/">
<action name="export" class="userAction" method="export">
<result type="json">
</result>
</action>
</package>
或者
<package name="default" namespace="/" extends="struts-default">
<action name="export" class="userAction" method="export">
<result name="success" type="stream">
</result>
</action>
</package>
经验证,上述两种方式都可以,主要是在ExtJS中调用action时注意写法,就可以成功导出excel