---
title: "如何把数据以Excel文档下载"
author: "mywebcode"
date: "2010-05-08T12:06:00Z"
tags: []
slug: "excel"
draft: false
lastmod: "2013-05-08T10:39:12Z"
id: 1863318
url: "https://mywebcode.iteye.com/blog/1863318"
---

准备工作:须要有spring的poi包支持

第一步:定一个方法将数据写入到Excel文件中,以输入流的形式返回给Action进行接受

public classIStudentServiceImpl {

public InputStream getInputStream(){

// HSSFWorkbook hw = new HSSFWorkbook();

HSSFWorkbook hw = new HSSFWorkbook();

// HSSFSheet sheet = hw.createSheet("sheet1");

HSSFSheet sheet = hw.createSheet("sheet1");

//创建一行

HSSFRow row = sheet.createRow(0);

//创建第一列

HSSFCell cell = row.createCell((short)0);

//进行编码设置

cell.setEncoding(HSSFCell.ENCODING\_UTF\_16);

//对第一列赋值

cell.setCellValue("编号");

//创建第二列

cell = row.createCell((short)1);

cell.setEncoding(HSSFCell.ENCODING\_UTF\_16);

cell.setCellValue("姓名");

//创建第三列

cell = row.createCell((short)2);

cell.setEncoding(HSSFCell.ENCODING\_UTF\_16);

cell.setCellValue("年龄");

//创建第四列

cell = row.createCell((short)3);

cell.setEncoding(HSSFCell.ENCODING\_UTF\_16);

cell.setCellValue("性别");

//创建第四列

cell = row.createCell((short)4);

cell.setEncoding(HSSFCell.ENCODING\_UTF\_16);

cell.setCellValue("学校");

cell = row.createCell((short)5);

cell.setEncoding(HSSFCell.ENCODING\_UTF\_16);

cell.setCellValue("日期");

List&lt;Student&gt; students = studentDAO.finStudents();

for(int i=0; i&lt;students.size(); i++){

//创建一行

row = sheet.createRow(i+1);

//创建一列

cell = row.createCell((short)0);

cell.setEncoding(HSSFCell.ENCODING\_UTF\_16);

cell.setCellValue(students.get(i).getId());

//创建第二列

cell = row.createCell((short)1);

cell.setEncoding(HSSFCell.ENCODING\_UTF\_16);

cell.setCellValue(students.get(i).getName());

//创建第三列

cell = row.createCell((short)2);

cell.setEncoding(HSSFCell.ENCODING\_UTF\_16);

cell.setCellValue(students.get(i).getAge());

//创建第四列

cell = row.createCell((short)3);

cell.setEncoding(HSSFCell.ENCODING\_UTF\_16);

cell.setCellValue(students.get(i).getSex());

//创建第五列

cell = row.createCell((short)4);

cell.setEncoding(HSSFCell.ENCODING\_UTF\_16);

cell.setCellValue(students.get(i).getSchool());

//创建第六列

cell = row.createCell((short)5);

cell.setEncoding(HSSFCell.ENCODING\_UTF\_16);

cell.setCellValue(students.get(i).getDate());

}

// ByteArrayOutputStream baos = new ByteArrayOutputStream();

//

// //将文件写入到字节输出流中

// try {

// hw.write(baos);

// } catch (IOException e) {

// e.printStackTrace();

// }

//

// //将字节输出流变为输入流

//

// InputStream is = new ByteArrayInputStream(baos.toByteArray());

// return is;

//以文件的形象提供给用户下载,当用户下载完后使用线程把文件删除

String fileName = CharacterUtil.getRandomString(10);

final File out = new File(fileName+".xsl");

//把内容写入文件输出当中

try {

OutputStream output = new FileOutputStream(out);

hw.write(output);

output.close();

} catch (Exception e) {

e.printStackTrace();

}

InputStream input = null;

try {

input = new FileInputStream(out);

} catch (Exception e) {

e.printStackTrace();

}

new Thread(new Runnable() {

public void run() {

try {

Thread.sleep(15000);

} catch (InterruptedException e) {

e.printStackTrace();

}

out.delete();

}

}).start();

return input ;

}

}

第二部:在Action中定一个文件输入流的方法返回给客户端进行下载

import java.io.InputStream;

import org.cxg.service.impl.IStudentServiceImpl;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport {

public IStudentServiceImpl service;

public InputStream getDownloadFile(){

return service.getInputStream();

}

@Override

public String execute() throws Exception {

return SUCCESS;

}

public IStudentServiceImpl getService() {

return service;

}

public void setService(IStudentServiceImpl service) {

this.service = service;

}

}

Struts.xml文件的配置

&lt;action name="xsl" class="DownloadAction"&gt;

&lt;result type="stream"&gt;

&lt;param name="contentType"&gt;application/vnd.ms-excel&lt;/param&gt;

&lt;param name="contentDisposition"&gt;attachment;filename="AllUsers.xls"&lt;/param&gt;

&lt;param name="inputName"&gt;downloadFile&lt;/param&gt;

&lt;/result&gt;

&lt;/action&gt;