准备工作:须要有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<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>
分享到:
相关推荐
1、java解析读取excel文件中的数据,并写入数据库。 2、java读取数据库数据,并导出为excel文件。 3、README.md中有详细的操作步骤示例。 使用说明: 1. 先使用postman导入:other/excel相关.postman_collection....
纯C++ 代码保存数据到excel文件
这是我在公司实习的时候研究的课题,主要将大量数据从数据库导出到Excel文件。(一般是值Excel2007及以后版本)因为Excel2003及以前的版本中能保持的数据量是有限制的。Excel2007及以后版本中的数据能保持100万条...
波士顿房价数据EXCEL
1、下载资源后请先看readme文档,README.md中有项目的介绍和具体的使用流程说明和易碰到的问题及解决方案。 2、若各位项目需求与本资源的样式相符度低,例如业务有渲染单元格颜色的,或者要求字体加粗,或者写入到多...
Visual C++源代码 134 如何以Excel文件数据作为数据源Visual C++源代码 134 如何以Excel文件数据作为数据源Visual C++源代码 134 如何以Excel文件数据作为数据源Visual C++源代码 134 如何以Excel文件数据作为数据源...
JS数据导出Excel案例代码,通过JS数组形式把数据导出成Excel文件格式
使用Excel数据分析案例数据.zip
C#操作EXCEL EXCEL类库 Excel模板处理 将DataTable数据写入Excel文件 C#数据写入EXCEL EXCEL样式管理帮助类库
如果您需要把把Oracle里的数据导入Excel,请查看此方法,无需写程序,无需敲代码,几步搞定。
excel文件里面的数据
这几个程序实例,都是我参考各种资料,自己写的,都运行过,能把40万数据导入到Excel文件中,而且性能也挺好的,这几个是程序代码,我把这几个程序的文档总结也上传了,如果要下载的话,可以去我的下载空间。
保存需要的数据到EXCEL中,然后导出至表格。 保存的数据为字符串数组。
java jxl 通过excel模板创建新文件并将数据导入excel,实现按行、列插入数据
页面数据可以自定义选择字段,选择数据量,然后生成表格下载
1952-2021年全国各地区GDP及各产业增长值、占比、人均GDP数据Excel文档。遇到问题可加文件中QQ联系。
省市区县街道数据-Excel.zip
java向EXCEL中写入数据。里面有一个ExcelWrite.java的文件和一个jxl的jar包。
用EXT进行数据导出,把数据保存在EXCEL里.
Visual C++源代码 150 如何以数据源方式导出Excel文件Visual C++源代码 150 如何以数据源方式导出Excel文件Visual C++源代码 150 如何以数据源方式导出Excel文件Visual C++源代码 150 如何以数据源方式导出Excel文件...