- 3
- 0
- 约8.02千字
- 约 15页
- 2017-08-20 发布于浙江
- 举报
java实现导出excel的
采用Spring mvc架构:Controller层代码如下
Java代码
@Controller
public class StudentExportController{
@Autowired
private StudentExportService studentExportService;
@RequestMapping(value = /excel/export)
public void exportExcel(HttpServletRequest request, HttpServletResponse response)
throws Exception {
ListStudent list = new ArrayListStudent();
list.add(new Student(1000,zhangsan,20));
list.add(new Student(1001,lisi,23));
list.add(new Student(1002,wangwu,25));
HSSFWorkbook wb = studentExportService.export(list);
response.setContentType(application/vnd.ms-excel);
response.setHeader(Content-disposition, attachment;filename=student.xls);
OutputStream ouputStream = response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();
}
}
Service层代码如下:
Java代码
@Service
public class StudentExportService {
String[] excelHeader = { Sno, Name, Age};
public HSSFWorkbook export(ListCampaign list) {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(Campaign);
HSSFRow row = sheet.createRow((int) 0);
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
for (int i = 0; i excelHeader.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellValue(excelHeader[i]);
cell.setCellStyle(style);
sheet.autoSizeColumn(i);
}
for (int i = 0; i list.size(); i++) {
row = sheet.createRow(i + 1);
Student student = list.get(i);
row.createCell(0).setCellValue(student.getSno());
row.createCell(1).setCellValue(student.getName());
row.createCell(2).setCellValue(student.getAge());
}
return wb;
}
}
前台的js代码如下:
Javascript代码
script
function exportExcel(){
location.href=excel/export;
!--这里不能用ajax请求,ajax请求无法弹出下载保存对话框--
}
/script
//------------------------------------------------------------------------
//文件流信息
private InputStream excelFile;
//文件名称
private String downloadFileName;
//getter setter
public String getDownloadFileName() {
SimpleDateFormat sf = new Simple
您可能关注的文档
- Hibernate面试题汇椎能.doc
- HP dm1 v1.0(20110的111).ppt
- hp laserjet p1007吹尿印机故障处理办法.doc
- HP LaserJet3015 30的30和3380AiO系列产品诊断传真代码和踪迹报告.doc
- hp-unix主机操作系偷某加固规范.pdf
- HP1280打印机在win7的上无法使用解决方法.doc
- HPM1213nfMFP网络打的印机安装说明及常见问题解决.doc
- HP_1606DN网络接口吹尿印机的设置.doc
- HP刻录机驱动安装说的明.doc
- HP打印机脱机打印测的试页.doc
- 中国国家标准 GB/T 6675.9-2025玩具安全 第9部分:化学及类似活动的实验玩具.pdf
- 《GB/T 6675.9-2025玩具安全 第9部分:化学及类似活动的实验玩具》.pdf
- GB/T 46975-2025婴童用品 便携式婴儿睡篮技术要求.pdf
- 中国国家标准 GB/T 46975-2025婴童用品 便携式婴儿睡篮技术要求.pdf
- 《GB/T 46975-2025婴童用品 便携式婴儿睡篮技术要求》.pdf
- 《GB/T 6675.14-2025玩具安全 第14部分:指画颜料要求和测试方法》.pdf
- GB/T 6675.14-2025玩具安全 第14部分:指画颜料要求和测试方法.pdf
- 中国国家标准 GB/T 6675.14-2025玩具安全 第14部分:指画颜料要求和测试方法.pdf
- 中国国家标准 GB/T 33772.3-2025质量评定体系 第3部分:印制板及层压板最终产品检验及过程监督用抽样方案的选择和使用.pdf
- 《GB/T 33772.3-2025质量评定体系 第3部分:印制板及层压板最终产品检验及过程监督用抽样方案的选择和使用》.pdf
原创力文档

文档评论(0)