欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

文件下载

程序员文章站 2022-06-22 09:42:38
...

@SuppressWarnings("unused")

public void downYear(String name,NdfxEntity data,HttpServletRequest request,HttpServletResponse response) throws IOException{

String filename = name + ".xls";

OutputStream os = response.getOutputStream();

response.reset();

response.setHeader("Content-Disposition",

"attachment;filename=" + new String(filename.getBytes("GB2312"), "ISO8859-1"));

response.setContentType("application/msexcel");

HSSFWorkbook wb = new HSSFWorkbook();

// 创建Sheet

HSSFSheet sheet = wb.createSheet("分析报表");

// 设置单元格默认宽度

sheet.setDefaultColumnWidth(30);

// 设置字体样式

HSSFCellStyle headCellStyle1 = wb.createCellStyle();

headCellStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);

headCellStyle1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中

HSSFFont headFont1 = wb.createFont();

headFont1.setFontHeightInPoints((short) 20);// 设置字体大小

headFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 粗体显示

headCellStyle1.setFont(headFont1);

// 设置字体样式

HSSFCellStyle headCellStyle2 = wb.createCellStyle();

headCellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);

headCellStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中

HSSFFont headFont2 = wb.createFont();

headFont2.setFontHeightInPoints((short) 16);// 设置字体大小

headCellStyle2.setFont(headFont2);

 

String[] titles = new String[]{"月份",data.getYear1() + "年度",data.getYear2() + "年度","环比增长量","同比增长率"};

String[] months = new String[]{"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};

//创建大标题

HSSFRow rowtitle = sheet.createRow(0);

CellRangeAddress region = new CellRangeAddress(0,0,0,titles.length-1);//起始行,起始列,结束行,结束列        

sheet.addMergedRegion(region);

HSSFCell cell0 = rowtitle.createCell(0);

cell0.setCellValue("接入数据分析("+data.getYear1()+"年度-"+data.getYear2()+"年度)");

cell0.setCellStyle(headCellStyle1);

// 创建小标题

HSSFRow row = sheet.createRow(1);

row.setRowStyle(headCellStyle2);

for (int i = 0; i < titles.length; i++) {

HSSFCell cell = row.createCell(i);

cell.setCellValue(titles[i]);// 添加第i格并设置值

cell.setCellStyle(headCellStyle2);

}

   String[] year1data = data.getYear1data().substring(7,data.getYear1data().length()-2).split(",");

   String[] year2data = data.getYear2data().substring(7,data.getYear2data().length()-2).split(",");

   String[] hb = data.getHbdata().substring(7,data.getHbdata().length()-2).split(",");

   String[] tb = data.getTbdata().substring(7,data.getTbdata().length()-2).split(",");

   for(int i=0;i<year1data.length;i++){

      HSSFRow rowContent = sheet.createRow(i + 2);

      rowContent.createCell(0).setCellValue(months[i]);

  rowContent.createCell(1).setCellValue(year1data[i]);

  rowContent.createCell(2).setCellValue(year2data[i]);

  rowContent.createCell(3).setCellValue(hb[i] + "%");

  rowContent.createCell(4).setCellValue(tb[i] + "%");

   }

wb.write(os);

wb.close();

}

相关标签: 文件下载