java导出excel
分类: Java 专栏: java 标签: java导出excel
2026-02-24 18:09:30 39浏览
java导出excel
pom.xml
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.13</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.13</version> </dependency>
控制器:
@RequestMapping("/getExcel")
public void getExcel(JdOrders t, HttpServletResponse response, HttpSession session) throws Exception{
String title= "订单表"+DateUtil.DateToString(new Date(),"yyyyMMddHHmmss");
ExcelUtils excelutils=new ExcelUtils();
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(title);
HSSFCellStyle cellStyle = workbook.createCellStyle();
//大标题
CellRangeAddress region = new CellRangeAddress(0, 0, 0,5);
sheet.addMergedRegion(region);
String []big_titles={title};
excelutils.createTitle(workbook,sheet,big_titles);
region = new CellRangeAddress(1, 1, 0,5);
sheet.addMergedRegion(region);
HSSFRow row1 = sheet.createRow(1);
row1.createCell(0).setCellValue("日期:"+DateUtil.getNowDate());
cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT); // 居中
row1.getCell(0).setCellStyle(cellStyle);
cellStyle = workbook.createCellStyle();
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellStyle.setFont(font);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
//设置列
cellStyle = workbook.createCellStyle();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellStyle.setFont(font);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
String []titles={"订单号","房间名称","总费用","实际支付","已退款","顾客","预定时间","实际入住时间","民宿"};
HSSFRow row2 = sheet.createRow(2);
for(int i=0;i<titles.length;i++){
row2.createCell(i).setCellValue(titles[i]);
row2.getCell(i).setCellStyle(cellStyle);
}
int rowNum=3;
List<JdOrders> all_li=ordersService.getlistByJoin(t);
cellStyle = workbook.createCellStyle();
cellStyle.setWrapText(true);
for(JdOrders s:all_li){
HSSFRow row = sheet.createRow(rowNum);
row.createCell(0).setCellValue(s.getOrderno() );
row.createCell(1).setCellValue(s.getRname() );
row.createCell(2).setCellValue(s.getTotalmoney().toString());
row.createCell(3).setCellValue(s.getRealmoney().toString());
row.createCell(4).setCellValue( s.getRmoney()==null?"": s.getRmoney().toString());
row.createCell(5).setCellValue(s.getNickname());
row.createCell(6).setCellValue(s.getIndate()+"到"+s.getOutdate()+"("+s.getDays()+"天)");
row.createCell(7).setCellValue((s.getRealindate()==null?"":s.getRealindate())+"到"+(s.getRealoutdate()==null?"":s.getRealoutdate()));
row.createCell(8).setCellValue(s.getHname());
rowNum++;
}
//设置日期格式
HSSFCellStyle style = workbook.createCellStyle();
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
String fileName = title+".xls";
//生成excel文件
excelutils.buildExcelFile(fileName, workbook);
//浏览器下载excel
excelutils.buildExcelDocument(fileName,workbook,response);
}
工具类:
package com.jf.s.util;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelUtils {
//创建表头
public void createTitle(HSSFWorkbook workbook,HSSFSheet sheet,String title[]){
HSSFRow row = sheet.createRow(0);
//设置列宽,setColumnWidth的第二个参数要乘以256,这个参数的单位是1/256个字符宽度
sheet.setColumnWidth(1,12*256);
sheet.setColumnWidth(3,17*256);
//设置为居中加粗
HSSFCellStyle style = workbook.createCellStyle();
HSSFFont font = workbook.createFont();
font.setBold(true);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setFont(font);
HSSFCell cell;
for(int i=0;i<title.length;i++){
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
}
//生成excel文件
public void buildExcelFile(String filename,HSSFWorkbook workbook) throws Exception{
//String abp="D:/apache-tomcat-8.5.57/wtpwebapps/";
// String abp="/home/xys20813ixoyns42n0h8o1f3/wwwroot/";
FileOutputStream fos = new FileOutputStream(filename);
workbook.write(fos);
fos.flush();
fos.close();
}
//浏览器下载excel
public void buildExcelDocument(String filename,HSSFWorkbook workbook,HttpServletResponse response) throws Exception{
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(filename, "utf-8"));
OutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
}
}
好博客就要一起分享哦!分享海报
此处可发布评论
评论(0)展开评论
暂无评论,快来写一下吧
展开评论
他的专栏
他感兴趣的技术





java
vue
springboot
Mysql
ssm
小程序
uniapp
js和jquery