码迷,mamicode.com
首页 > 其他好文 > 详细

数据使用Excel导出下载

时间:2019-06-19 23:33:58      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:leo   利用   set   表名   vat   char   i++   tps   单元格   

此方法导出格式为xls

前端使用a标签链接实现

使用button按钮利用ajax传递请求时会报错,这里需要注意,此问题后期探讨,具体实现方式后期更新再议

Excel导出工具类

public class ExcelUploadUtil {

static final short borderpx = 1;

/**
* 导出excel表格
* @param head 表头
* @param body 数据信息
* @return
*/
public static HSSFWorkbook expExcel(List<String> head, List<List<String>> body) {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet1");
HSSFRow row = sheet.createRow(0);
HSSFCell cell= null;
HSSFCellStyle cellStyle = workbook.createCellStyle();
setBorderStyle(cellStyle, borderpx);
cellStyle.setFont(setFontStyle(workbook, "黑体", (short) 14));
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
sheet.createFreezePane(0,1,0,1);
for (int i = 0; i<head.size(); i++) {
cell = row.createCell(i);
cell.setCellValue(head.get(i));
cell.setCellStyle(cellStyle);
}
HSSFCellStyle cellStyle2 = workbook.createCellStyle();
setBorderStyle(cellStyle2, borderpx);
cellStyle2.setFont(setFontStyle(workbook, "宋体", (short) 12));
cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
for (int i = 0; i < body.size(); i++) {
row = sheet.createRow(i + 1);
List<String> paramList = body.get(i);
for (int p = 0; p < paramList.size(); p++) {
cell = row.createCell(p);
cell.setCellValue(paramList.get(p));
cell.setCellStyle(cellStyle2);
}
}
for (int i = 0, isize = head.size(); i < isize; i++) {
sheet.autoSizeColumn(i);
}
return workbook;
}

/**
* 文件输出
* @author xwang
* @param workbook 填充好的workbook
* @param path 存放的位置
*/
public static void outFile(HSSFWorkbook workbook,String path,HttpServletResponse response) {
SimpleDateFormat fdate=new SimpleDateFormat("yyyyMMdd-HH点mm分");
path = path.substring(0, path.lastIndexOf(".")) + fdate.format(new Date()) + path.substring(path.lastIndexOf("."));
OutputStream os=null;
File file = null;
try {
file = new File(path);
String filename = file.getName();
os = new FileOutputStream(file);
response.addHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(filename, "UTF-8"));
os= new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/vnd.ms-excel;charset=utf-8");
workbook.write(os);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.flush();
os.close();
System.gc();
System.out.println(file.delete());
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 设置字体样式
* @author xwang
* @param workbook 工作簿
* @param name 字体类型
* @param height 字体大小
* @return HSSFFont
*/
private static HSSFFont setFontStyle(HSSFWorkbook workbook, String name, short height) {
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints(height);
font.setFontName(name);
return font;
}

/**
* 设置单元格样式
* @author xwang
* @param cellStyle 工作簿
* @param border border样式
*/
private static void setBorderStyle(HSSFCellStyle cellStyle, short border) {
cellStyle.setBorderBottom(border); // 下边框
cellStyle.setBorderLeft(border);// 左边框
cellStyle.setBorderTop(border);// 上边框
cellStyle.setBorderRight(border);// 右边框
}

}

Controller类

@RequestMapping("/excelUpload")
public void testExcel2(HttpServletResponse response){
//调用service层方法查询出需要导出的数据
List<Student> lisx=studentService.selectList(null);
for (Student s:lisx
) {
System.err.println("excelUpload:"+s);
}
//创建表数据头
List<String> head = new ArrayList<>();
head.add("学号");
head.add("姓名");
head.add("学校");
head.add("考场");
//创建表体
List<List<String>> body = new ArrayList<>();
for (Student stu : lisx) {
List<String> bodyValue = new ArrayList<>();
bodyValue.add(String.valueOf(stu.getId()));
bodyValue.add(stu.getName());
bodyValue.add(stu.getSchoolName());
bodyValue.add(String.valueOf(stu.getSeatNumber()));
//将数据添加到表体中
body.add(bodyValue);
}

//Excel表格表名
String fileName = "学生信息统计.xls";
HSSFWorkbook excel = ExcelUploadUtil.expExcel(head,body);
ExcelUploadUtil.outFile(excel,"./"+fileName,response);

}

数据使用Excel导出下载

标签:leo   利用   set   表名   vat   char   i++   tps   单元格   

原文地址:https://www.cnblogs.com/xiaowangxiao/p/11055572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!