查询数据写入excel并下载

private XSSFWorkbook export(HttpServletRequest request, HttpServletResponse response, 

String fileName, String[] excelHeader, List<Map<String, String>> findList) throws Exception {

//fileName为文件名

//excelHeader为excel列表标头第一行内容

//findList为查询出的数据



// 设置请求
response.setContentType("application/application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xls", "UTF-8"));
// 创建一个Workbook,对应一个Excel文件
XSSFWorkbook wb = new XSSFWorkbook();
// 设置标题样式
XSSFCellStyle titleStyle = wb.createCellStyle();
// 设置单元格边框样式
titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框 细边线
titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 下边框 细边线
titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框 细边线
titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框 细边线
// 设置单元格对齐方式
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中
// 设置字体样式
Font titleFont = wb.createFont();
titleFont.setFontHeightInPoints((short) 15); // 字体高度
titleFont.setFontName("黑体"); // 字体样式
titleStyle.setFont(titleFont);
// 在Workbook中添加一个sheet,对应Excel文件中的sheet
XSSFSheet sheet = wb.createSheet(fileName);
// 标题数组
String[] titleArray = new String[excelHeader.length];
// 字段名数组
String[] fieldArray = new String[excelHeader.length];
for (int i = 0; i < excelHeader.length; i++) {
String[] tempArray = excelHeader[i].split(",");// 临时数组 分割#
titleArray[i] = tempArray[0];
fieldArray[i] = tempArray[0];
}
// 在sheet中添加标题行
XSSFRow row = sheet.createRow((int) 0);// 行数从0开始
// 为标题行赋值
for (int i = 0; i < titleArray.length; i++) {
XSSFCell titleCell = row.createCell(i);
titleCell.setCellValue(titleArray[i]);
titleCell.setCellStyle(titleStyle);
// sheet.autoSizeColumn(i + 1);// 0号位被序号占用,所以需+1
}
// 数据样式 因为标题和数据样式不同 需要分开设置 不然会覆盖
XSSFCellStyle dataStyle = wb.createCellStyle();
// 设置数据边框
dataStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
dataStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
dataStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
dataStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
// 设置居中样式
dataStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
dataStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中
// 设置数据字体
Font dataFont = wb.createFont();
dataFont.setFontHeightInPoints((short) 12); // 字体高度
dataFont.setFontName("宋体"); // 字体
dataStyle.setFont(dataFont);

for (int i = 0; i < fieldArray.length; i++) {
XSSFCell sequenceCellValue = row.createCell(i);
sequenceCellValue.setCellValue(fieldArray[i]);
sequenceCellValue.setCellStyle(dataStyle);
}

for (int j = 1; j <= findList.size(); j++) {
row = sheet.createRow(j);
// 为序号赋值
XSSFCell sequenceCellValue = row.createCell(0);// 序号值永远是第0列
sequenceCellValue.setCellValue(j);
sequenceCellValue.setCellStyle(dataStyle);


Map<String, String> su = (Map<String, String>) findList.get(j-1);
for(int i = 0; i < fieldArray.length; i++){
if( i ==0){
XSSFCell dataCell = row.createCell(i);
dataCell.setCellStyle(dataStyle);
sheet.autoSizeColumn(j);
dataCell.setCellValue(j);// 为当前列赋值
}else{
XSSFCell dataCell = row.createCell(i);
dataCell.setCellStyle(dataStyle);
sheet.autoSizeColumn(j);
if(i==1)
dataCell.setCellValue(su.get("name"));// 为当前列赋值
if(i==2)
dataCell.setCellValue(su.get("mobile"));// 为当前列赋值
if(i==3)
dataCell.setCellValue(su.get("company"));// 为当前列赋值
if(i==4)
dataCell.setCellValue(su.get("companyTel"));// 为当前列赋值
if(i==5)
dataCell.setCellValue(su.get("position"));// 为当前列赋值
if(i==6)
dataCell.setCellValue(su.get("accommodation"));// 为当前列赋值
if(i==7)
dataCell.setCellValue(su.get("gender"));// 为当前列赋值
if(i==8)
dataCell.setCellValue(su.get("driving"));// 为当前列赋值
if(i==9)
dataCell.setCellValue(su.get("flight"));// 为当前列赋值
if(i==10)
dataCell.setCellValue(su.get("remarks"));// 为当前列赋值
}
}
}


OutputStream outputStream = response.getOutputStream();// 打开流
wb.write(outputStream);// HSSFWorkbook写入流
outputStream.flush();// 刷新流
outputStream.close();// 关闭流
return wb;
}
posted @   silentmuh  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
Live2D
欢迎阅读『查询数据写入excel并下载』
  1. 1 Walk Thru Fire Vicetone
  2. 2 爱你 王心凌
  3. 3 Inspire Capo Productions - Serenity
  4. 4 Welcome Home Radical Face
  5. 5 粉红色的回忆 李玲玉
Welcome Home - Radical Face
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Ben P. Cooper

作曲 : Cooper

Sleep don't visit, so I choke on sun

And the days blur into one

And the backs of my eyes hum with things I've never done

Sheets are swaying from an old clothesline

Was never much but we've made the most

Welcome home

Ships are launching from my chest

Some have names but most do not

If you find one,please let me know what piece I've lost

Heal the scars from off my back

I don't need them anymore

You can throw them out or keep them in your mason jars

I've come home

All my nightmares escaped my head

Bar the door, please don't let them in

You were never supposed to leave

Now my head's splitting at the seams

And I don't know if I can

Here, beneath my lungs

I feel your thumbs press into my skin again

点击右上角即可分享
微信分享提示