java poi生成excel 有下载提示
2014-08-07 11:32
351 查看
java poi 两种export excel: (缺失包的,自行引入)
一:直接导出到制定的路径中。
@Service
public class ExportExcelServiceImpl implements ExportExcelService {
public static String filePrex = "/home/echo/桌面/";
public Result<String> exportToExcel(String ids, HttpServletResponse response){
List<Integer> billIds = new ArrayList<>();
String[] stringArr = ids.split(",");
for (String id : stringArr){
billIds.add(Integer.parseInt(id));
}
List<DispatchBill> bills = billDao.selectByIds(billIds);
List<DispatchBillEntry> billEntries = entryDao.selectByDispatchBill(billIds);
Workbook wb = new HSSFWorkbook();
makeSheet(wb, bills,billEntries);
Date date=new Date();
SimpleDateFormat sdf1=new SimpleDateFormat("YYYY-MM-dd");
SimpleDateFormat sdf2=new SimpleDateFormat("hh-mm-ss");
String time1=sdf1.format(date);
String time2=sdf2.format(date);
String filename = time2+".xls";
String path = filePrex +time1+ "/"+time2+".xls";
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
if (!file.getParentFile().exists()) {
file.getParentFile().mkdir();
}
//输出流形式 导出excel
FileOutputStream fileOut = new FileOutputStream(file);
wb.write(fileOut);
} catch (Exception e) {
e.printStackTrace();
}
return Result.wrapSuccessfulResult(null);
}
}
二:页面下载方式导出。
@Service
public class ExportExcelServiceImpl implements ExportExcelService {
public static String filePrex = "/home/echo/桌面/";
public Result<String> exportToExcel(String ids, HttpServletResponse response){
List<Integer> billIds = new ArrayList<>();
String[] stringArr = ids.split(",");
for (String id : stringArr){
billIds.add(Integer.parseInt(id));
}
List<DispatchBill> bills = billDao.selectByIds(billIds);
List<DispatchBillEntry> billEntries = entryDao.selectByDispatchBill(billIds);
Workbook wb = new HSSFWorkbook();
makeSheet(wb, bills,billEntries);
Date date=new Date();
SimpleDateFormat sdf1=new SimpleDateFormat("YYYY-MM-dd");
SimpleDateFormat sdf2=new SimpleDateFormat("hh-mm-ss");
String time2=sdf2.format(date);
String filename = time2+".xls";
try {
//页面 download
// 清空response
response.reset();
// 设置response的Header
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
ServletOutputStream sos = null;
sos = response.getOutputStream();
wb.write(sos);
sos.flush();
sos.close();
} catch (Exception e) {
e.printStackTrace();
}
return Result.wrapSuccessfulResult(null);
}
public void makeSheet(Workbook wb,List<DispatchBill> bills,List<DispatchBillEntry> billEntries){
Sheet sheet = wb.createSheet(" DispatchBill export to Excel Demo");
Row firstRow = sheet.createRow(0);
firstRow.createCell(0).setCellValue("序号");
firstRow.createCell(2).setCellValue("车牌号");
firstRow.createCell(3).setCellValue("司机");
}
}
一:直接导出到制定的路径中。
@Service
public class ExportExcelServiceImpl implements ExportExcelService {
public static String filePrex = "/home/echo/桌面/";
public Result<String> exportToExcel(String ids, HttpServletResponse response){
List<Integer> billIds = new ArrayList<>();
String[] stringArr = ids.split(",");
for (String id : stringArr){
billIds.add(Integer.parseInt(id));
}
List<DispatchBill> bills = billDao.selectByIds(billIds);
List<DispatchBillEntry> billEntries = entryDao.selectByDispatchBill(billIds);
Workbook wb = new HSSFWorkbook();
makeSheet(wb, bills,billEntries);
Date date=new Date();
SimpleDateFormat sdf1=new SimpleDateFormat("YYYY-MM-dd");
SimpleDateFormat sdf2=new SimpleDateFormat("hh-mm-ss");
String time1=sdf1.format(date);
String time2=sdf2.format(date);
String filename = time2+".xls";
String path = filePrex +time1+ "/"+time2+".xls";
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
if (!file.getParentFile().exists()) {
file.getParentFile().mkdir();
}
//输出流形式 导出excel
FileOutputStream fileOut = new FileOutputStream(file);
wb.write(fileOut);
} catch (Exception e) {
e.printStackTrace();
}
return Result.wrapSuccessfulResult(null);
}
}
二:页面下载方式导出。
@Service
public class ExportExcelServiceImpl implements ExportExcelService {
public static String filePrex = "/home/echo/桌面/";
public Result<String> exportToExcel(String ids, HttpServletResponse response){
List<Integer> billIds = new ArrayList<>();
String[] stringArr = ids.split(",");
for (String id : stringArr){
billIds.add(Integer.parseInt(id));
}
List<DispatchBill> bills = billDao.selectByIds(billIds);
List<DispatchBillEntry> billEntries = entryDao.selectByDispatchBill(billIds);
Workbook wb = new HSSFWorkbook();
makeSheet(wb, bills,billEntries);
Date date=new Date();
SimpleDateFormat sdf1=new SimpleDateFormat("YYYY-MM-dd");
SimpleDateFormat sdf2=new SimpleDateFormat("hh-mm-ss");
String time2=sdf2.format(date);
String filename = time2+".xls";
try {
//页面 download
// 清空response
response.reset();
// 设置response的Header
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
ServletOutputStream sos = null;
sos = response.getOutputStream();
wb.write(sos);
sos.flush();
sos.close();
} catch (Exception e) {
e.printStackTrace();
}
return Result.wrapSuccessfulResult(null);
}
public void makeSheet(Workbook wb,List<DispatchBill> bills,List<DispatchBillEntry> billEntries){
Sheet sheet = wb.createSheet(" DispatchBill export to Excel Demo");
Row firstRow = sheet.createRow(0);
firstRow.createCell(0).setCellValue("序号");
firstRow.createCell(2).setCellValue("车牌号");
firstRow.createCell(3).setCellValue("司机");
}
}
相关文章推荐
- java利用poi生成excel文件后下载本地
- java鬼混笔记:用Spring的ResponseEntity和poi进行excel生成和下载
- Java POI 3.17导出EXCEL并下载(带进度条提示)
- java生成excel并下载(poi)
- jsp利用POI直接生成Excel并在页面提示打开下载
- java 利用 poi 生成 Excel文件与spring使用文件流形式下载文件
- java生成excel及下载 poi
- 简单OA项目笔记(5):生成、下载excel格式的动态表格(POI)
- Java 使用 poi生成 Excel的方法
- 利用POI生成Excel并通过Servlet下载示例
- java action jxl 生成并下载excel
- 【原创文章】JAVA-POI实现EXCEL下拉框的级联(POI生成Excel中带有可级联的下拉框)
- Java Excel API jxl入门及jxl.jar下载(java生成excel文件)
- java action jxl 生成并下载excel
- apache POI3.2 java操作excel 设置数据有效性,实现excel单元格列表和提示
- Java利用POI生成Excel强制换行 (转载)
- 使用jxl动态生成excel文件并提示下载
- OA (ssh) 基本实现(poi 生成 Excel , struts2动态下载 mysql数据库文件) 学习笔记(含源代码)
- JXL生成的excel文件+JAVA文件下载功能
- JAVA JXL -----excel文件生成、在线打开和下载