/**
* 设置excel的字段
* @param context 上下文
* @param classmateList 传入的beanlist对象
* @Param sheetName 一个sheet的表名
* @param fileName 导出excel的文件名
* @param head excel第一行的头
* @return
*/
public static void excelUtil(RoutingContext context,
JSONArray jsonArray,
LinkedHashMap<String,String> map,
String sheetName,
String fileName){
JsonResult jsonResult = new JsonResult();
//如果上下文为空则返回
if(context==null){
System.out.println("传入的RoutingContext为空");
return;
}
//如果传入的json数组为空则返回空
if(jsonArray == null||jsonArray.size()<=0){
System.out.println("传入的json数组为空");
context.response().end("");
return;
}
//如果传入sheetName的参数为空或者“”
if(sheetName == null||sheetName.equals(""))
{
//设置sheet名称
sheetName="sheet1";
}
//如果传入fileName的参数为空或者“”
if(fileName == null||fileName.equals(""))
{
//指定默认的文件名为时间
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
fileName=format.format(date);
}
HSSFWorkbook workbook = new HSSFWorkbook();
//创建sheet表名
HSSFSheet sheet = workbook.createSheet(sheetName);
HSSFCellStyle style = workbook.createCellStyle();
// 设置这些样式
style.setAlignment(HorizontalAlignment.CENTER);//水平居中
style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
// 设置边框
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
//创建首行
HSSFRow tableHead = sheet.createRow(0);
//获取第一个json数组中的第一个json对象
JSONObject object = jsonArray.getJSONObject(0);
//创键excel表头中列数
int cell = 0;
//设置表头
for(String value:map.values()){
//设置excel头部的每一列
tableHead.createCell(cell).setCellValue(value);
//将json中键的集合放入list中
//cell自增
cell++;
}
for(int i=0;i<jsonArray.size();i++){
//获取body JSON数组中的json
JSONObject body = jsonArray.getJSONObject(i);
//创建excel中的每一个行
HSSFRow row = sheet.createRow(i+1);
int cell2 = 0;
//将对应键的值插入到每一列的excel中
for(String key:map.keySet())
{
Object obj = body.get(key);
//如果obj为空则赋值空字符串
if(obj==null)
obj = "";
row.createCell(cell2).setCellValue(obj.toString());
cell2++;
}
}
// 创建字节输入流
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try{
//将excel表的数据写入到字节流中
workbook.write(bos);
}catch(IOException e){
throw new RuntimeException("export excel error");
}
//设置缓冲区
Buffer buffer = Buffer.buffer();
//将流中的内容写入到缓冲区中
buffer.appendBytes(bos.toByteArray());
context = setContext(context,fileName);
context.response().end(buffer);
}
/**
* //设置传输的格式
* @param context 上下文
* @param buffer 传输的buffer
* @param fileName 文件名
* @return
*/
public static RoutingContext setContext(RoutingContext context,String fileName){
//设置传输的格式
context.response().putHeader("content-type", "application/octet-stream;charset=UTF-8");
context.response().putHeader("Content-Disposition", "attachment;filename="+fileName+".xls");
context.response().putHeader("Pargam", "no-cache");
context.response().putHeader("Cache-Control", "no-cache");
return context;
}
vertx框架 Excel导出(工具类)
最新推荐文章于 2025-11-05 00:24:55 发布
本文介绍了一种使用Java实现的Excel导出方法,通过RoutingContext、JSONArray和LinkedHashMap等对象,实现了从json数据到Excel表格的转换。文章详细解释了如何设置Excel的字段,包括表头、边框样式及对齐方式,以及如何处理空值和默认参数。
1448

被折叠的 条评论
为什么被折叠?



