67,513
社区成员
发帖
与我相关
我的任务
分享
package com.boxun.action;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.boxun.bean.Userinfo;
import com.boxun.biz.IUserBiz;
import com.opensymphony.xwork2.ActionSupport;
public class OutAction extends ActionSupport implements ServletResponseAware{
/**
*
*/
private static final long serialVersionUID = 1L;
private IUserBiz biz ;
public void setBiz(IUserBiz biz) {
this.biz = biz;
}
private String format = "xls";
private HttpServletResponse response;
private String fileName;
public String execute(){
setResponseHeader();
try {
exportExcel(response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/** 设置响应头*/
public void setResponseHeader(){
response.setContentType("application/octet-stream;charset=iso-8859-1");
try{
response.setHeader("Content-Disposition", "attachment;filename="
+java.net.URLEncoder.encode(this.fileName, "UTF-8"));
//客户端不缓存
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
}catch(Exception ex){
ex.printStackTrace();
}
}
/**导出数据*/
private void exportExcel(OutputStream os) throws IOException{
Workbook book = new HSSFWorkbook();
Sheet sheet = book.createSheet("导出信息");
Row row = sheet.createRow(0);
row.createCell(0).setCellValue("编号");
row.createCell(1).setCellValue("用户名");
row.createCell(2).setCellValue("密码");
row.createCell(3).setCellValue("真实姓名");
row.createCell(4).setCellValue("地址");
row.createCell(5).setCellValue("备注");
CellStyle sty = book.createCellStyle();
List<Userinfo> list = biz.getAll();
for (int i = 1; i < list.size(); i++) {
Userinfo user = list.get(i-1);
row = sheet.createRow(i);
row.createCell(0).setCellValue(user.getId());
row.createCell(1).setCellValue(user.getName());
row.createCell(2).setCellValue(user.getPass());
row.createCell(3).setCellValue(user.getLastname());
row.createCell(4).setCellValue(user.getAddres());
row.createCell(5).setCellValue(user.getRemark());
}
try{
book.write(os);
}catch(Exception ex){
ex.printStackTrace();
}
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
this.fileName = "导出数据.xls";
}
public HttpServletResponse getResponse() {
return response;
}
public void setResponse(HttpServletResponse response) {
this.response = response;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public void setServletResponse(HttpServletResponse arg0) {
}
}
response.setContentType("application/vnd.ms-excel;");//可以!
response.setContentType("application/octet-stream;charset=iso-8859-1");//也可以
//Struts2 的 Aware方式第一次用~~~所以~~~o(︶︿︶)o 唉!!!
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public ActionForward doExcel(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Date date = new Date();
SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
response.setContentType("application/octet-stream; charset=UTF-8");
String s = "attachment;filename=" + time.format(date) + ".xls";
response.setHeader("Content-Disposition", s);
// 导出Execl表
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet1");
sheet.setColumnWidth((short) 0, (short) 6000);// 给工作表列定义列宽
sheet.setColumnWidth((short) 1, (short) 3000);// 给工作表列定义列宽
HSSFRow row1 = sheet.createRow(0);
HSSFCellStyle cellStyle = wb.createCellStyle(); // 创建单元格样式
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐
HSSFFont font = wb.createFont(); // 设置单元格字体
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontName("宋体");
font.setFontHeight((short) 200);
cellStyle.setFont(font);
//设置表头
List<String> btList = new ArrayList<String>();
btList.add("录入人");
btList.add("录入时间");
btList.add("责任车间");
btList.add("责任班组");
btList.add("故障类型");
btList.add("故障部位");
btList.add("问题内容");
HSSFCell cell = null;
for (int i = 0; i < btList.size(); i++) {// 设置表头
cell = row1.createCell((short) i);
cell.setCellStyle(cellStyle); // 设置表头样式
String str = (String) btList.get(i);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell编码解决中文高位字节截断
cell.setCellValue(str);
}
//设置内容
List<Aqzlwt> list = aqzlwtService.getAllAqzlwtByQueren();
for (int i = 0; i < list.size(); i++) {
Aqzlwt aq = list.get(i);
HSSFRow row = sheet.createRow(i + 1);
// HSSFFont font2 = wb.createFont();
// font2.setColor(HSSFColor.RED.index);
// HSSFCellStyle cellStyle2 = wb.createCellStyle();// 创建单元格显示样式;
// cellStyle2.setFont(font2);
HSSFCell dataCell3 = row.createCell((short) 0);
dataCell3.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell编码解决中文高位字节截断
dataCell3.setCellValue(aq.getLrr());
HSSFCell dataCell4 = row.createCell((short) 1);
dataCell4.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell编码解决中文高位字节截断
dataCell4.setCellValue(aq.getLrrqToPage());
HSSFCell dataCell5 = row.createCell((short) 2);
dataCell5.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell编码解决中文高位字节截断
dataCell5.setCellValue(aq.getZrcjmc());
HSSFCell dataCell6 = row.createCell((short) 3);
dataCell6.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell编码解决中文高位字节截断
dataCell6.setCellValue(aq.getZrbzmc());
HSSFCell dataCell7 = row.createCell((short) 4);
dataCell7.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell编码解决中文高位字节截断
dataCell7.setCellValue(aq.getGzlxmc());
HSSFCell dataCell8 = row.createCell((short) 5);
dataCell8.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell编码解决中文高位字节截断
dataCell8.setCellValue(aq.getGzbwmc());
HSSFCell dataCell9 = row.createCell((short) 6);
dataCell9.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell编码解决中文高位字节截断
dataCell9.setCellValue(aq.getWtms());
// dataCell9.setCellStyle(cellStyle2);
}
//页面输出下载
try {
wb.write(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
<action name="downloadExcel" method="WriteExcelFile" class="com.wl.upload.action.uploadAction">
<param name="savePath">/upload</param>
<result name="success" type="stream">
<param name="contentType">text/plain</param>
<param name="inputname">inputStream</param>
<param name="contentDisposition">attachment;filename="export.xls"</param>
<param name="bufferSize">4096</param>
</result>
<result name="login">/Error.jsp</result>
</action>