数据使用Excel导出下载

Posted xiaowangxiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据使用Excel导出下载相关的知识,希望对你有一定的参考价值。

此方法导出格式为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导出下载的主要内容,如果未能解决你的问题,请参考以下文章

excel的导出和下载

如何用JAVA导出Excel?(使用POI)

Vue.js使用Blob的方式实现excel表格的下载(流文件下载)

excel表格如何导出

求java导出大量数据到excel解决方法

如何导出excel表格?