如何把数据以Excel文档下载

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

 

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

public class IStudentServiceImpl {

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<Student> students = studentDAO.finStudents();

for(int i=0; i<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文件的配置

 <action name="xsl" class="DownloadAction">

         <result type="stream">

             <param name="contentType">application/vnd.ms-excel</param>

<param name="contentDisposition">attachment;filename="AllUsers.xls"</param>

<param name="inputName">downloadFile</param>

            </result>

 </action>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值