java下载excel文件源码(可选择保存文件路径)
来源:互联网 发布:免费cf手游刷枪软件 编辑:程序博客网 时间:2024/06/08 09:37
{
// 创建Excel的工作书册 Workbook,对应到一个excel文档
HSSFWorkbook wb = new HSSFWorkbook();
try {
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setHeader("Cache-Control", "private");
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Type", "application/force-download");
String title = "XXX";
title = this.processFileName(request, title);
response.setHeader("Content-disposition", "attachment;filename=" + title + ".xls");
// 创建Excel的工作sheet,对应到一个excel文档的tab
HSSFSheet sheet = wb.createSheet("sheet1");
//设置列宽
sheet.setColumnWidth(0, 13 * 256);
sheet.setColumnWidth(1, 13 * 256);
sheet.setColumnWidth(2, 40 * 256);
sheet.setColumnWidth(3, 13 * 256);
sheet.setColumnWidth(4, 13 * 256);
sheet.setColumnWidth(5, 13 * 256);
sheet.setColumnWidth(6, 13 * 256);
sheet.setColumnWidth(7, 13 * 256);
sheet.setColumnWidth(8, 13 * 256);
//加粗字体
org.apache.poi.ss.usermodel.Font boldFont =wb.createFont();
// boldFont.setItalic(true);
boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
HSSFCellStyle headerStyle = wb.createCellStyle();
headerStyle.setFont(boldFont);
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
headerStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
HSSFCellStyle headerStyle1 = wb.createCellStyle();
headerStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
headerStyle1.setBorderTop(HSSFCellStyle.BORDER_THIN);
headerStyle1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//学校不居中
HSSFCellStyle headerStyle2 = wb.createCellStyle();
headerStyle1.setBorderTop(HSSFCellStyle.BORDER_THIN);
headerStyle1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
// 创建Excel的sheet的一行
HSSFRow row = sheet.createRow(0);
//设置行高
sheet.createRow(0).setHeightInPoints(22);
// row.setHeight((short) 15.625);
row.createCell(0).setCellValue("A");
row.getCell(0).setCellStyle(headerStyle);
row.createCell(1, HSSFCell.CELL_TYPE_STRING).setCellValue("B");
row.getCell(1).setCellStyle(headerStyle);
row.createCell(2, HSSFCell.CELL_TYPE_STRING).setCellValue("C");
int count = list.size();
LOGGER.info("count------------->"+ count);
for(int i=0;i<count;i++){
SchoolScore vo = list.get(i) ;
row = sheet.createRow(i + 1);
sheet.createRow(i+1).setHeightInPoints(22);
// row.setHeight((short) 15.625);
row.createCell(0, HSSFCell.CELL_TYPE_STRING).setCellValue(vo.getSsID().getSubject().getName());
row.getCell(0).setCellStyle(headerStyle1);
row.createCell(1, HSSFCell.CELL_TYPE_STRING).setCellValue(vo.getSchoolCode());
row.getCell(1).setCellStyle(headerStyle1);
row.createCell(2, HSSFCell.CELL_TYPE_STRING).setCellValue(vo.getSchoolName());
row.getCell(2).setCellStyle(headerStyle2);
}
try {
OutputStream out = response.getOutputStream();
wb.write(out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return;
}
// 创建Excel的工作书册 Workbook,对应到一个excel文档
HSSFWorkbook wb = new HSSFWorkbook();
try {
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setHeader("Cache-Control", "private");
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Type", "application/force-download");
String title = "XXX";
title = this.processFileName(request, title);
response.setHeader("Content-disposition", "attachment;filename=" + title + ".xls");
// 创建Excel的工作sheet,对应到一个excel文档的tab
HSSFSheet sheet = wb.createSheet("sheet1");
//设置列宽
sheet.setColumnWidth(0, 13 * 256);
sheet.setColumnWidth(1, 13 * 256);
sheet.setColumnWidth(2, 40 * 256);
sheet.setColumnWidth(3, 13 * 256);
sheet.setColumnWidth(4, 13 * 256);
sheet.setColumnWidth(5, 13 * 256);
sheet.setColumnWidth(6, 13 * 256);
sheet.setColumnWidth(7, 13 * 256);
sheet.setColumnWidth(8, 13 * 256);
//加粗字体
org.apache.poi.ss.usermodel.Font boldFont =wb.createFont();
// boldFont.setItalic(true);
boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
HSSFCellStyle headerStyle = wb.createCellStyle();
headerStyle.setFont(boldFont);
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
headerStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
HSSFCellStyle headerStyle1 = wb.createCellStyle();
headerStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
headerStyle1.setBorderTop(HSSFCellStyle.BORDER_THIN);
headerStyle1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//学校不居中
HSSFCellStyle headerStyle2 = wb.createCellStyle();
headerStyle1.setBorderTop(HSSFCellStyle.BORDER_THIN);
headerStyle1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
// 创建Excel的sheet的一行
HSSFRow row = sheet.createRow(0);
//设置行高
sheet.createRow(0).setHeightInPoints(22);
// row.setHeight((short) 15.625);
row.createCell(0).setCellValue("A");
row.getCell(0).setCellStyle(headerStyle);
row.createCell(1, HSSFCell.CELL_TYPE_STRING).setCellValue("B");
row.getCell(1).setCellStyle(headerStyle);
row.createCell(2, HSSFCell.CELL_TYPE_STRING).setCellValue("C");
int count = list.size();
LOGGER.info("count------------->"+ count);
for(int i=0;i<count;i++){
SchoolScore vo = list.get(i) ;
row = sheet.createRow(i + 1);
sheet.createRow(i+1).setHeightInPoints(22);
// row.setHeight((short) 15.625);
row.createCell(0, HSSFCell.CELL_TYPE_STRING).setCellValue(vo.getSsID().getSubject().getName());
row.getCell(0).setCellStyle(headerStyle1);
row.createCell(1, HSSFCell.CELL_TYPE_STRING).setCellValue(vo.getSchoolCode());
row.getCell(1).setCellStyle(headerStyle1);
row.createCell(2, HSSFCell.CELL_TYPE_STRING).setCellValue(vo.getSchoolName());
row.getCell(2).setCellStyle(headerStyle2);
}
try {
OutputStream out = response.getOutputStream();
wb.write(out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return;
}
0 0
- java下载excel文件源码(可选择保存文件路径)
- java从服务器下载文件,可选择保存路径
- java读取本地图片和下载可选择路径保存图片
- java 中 excel生成并文件下载保存到本地
- Java Excel文件下载
- Java下载Excel文件
- Android下载文件保存到路径
- Unity商店下载的文件保存路径
- java 下载样板Excel文件
- java 下载文件 例:excel
- Java文件下载(excel)
- java导出excel 下载文件
- java web下载 Excel文件
- Java通过文件路径下载文件
- C# 直接打印指定路径文件 + 可选择指定打印机
- java swing 设置文件保存路径
- java选择保存文件的路径
- java 中 excel生成并文件下载保存到本地(三)
- 使用电脑远程调试安卓手机上浏览器的JS
- 招人不易留人更难——创业团队要闯哪些关?
- Memcached之客户端-xmemcached(5)
- 二分图算法 知识点详解
- 机房收费系统——起步阶段需要做的工作,怎么附加数据库?怎么注册控件?
- java下载excel文件源码(可选择保存文件路径)
- 搭建从java到android的开发环境
- 虚函数的机制
- 《算法导论》中的快速排序
- 【笔试】22、替换字符串的空格
- OFDM之卷积码编器
- 指针数组和数组指针
- 例子一 360搭建
- 链表操作,理解却不能及时的应用