Java代码嵌套不超过几层_JAVA代码实现嵌套层级列表,POI导出嵌套层级列表

/***

*

Description: 科目树导出

*@paramrequest

*@paramresponse

*@returnExcel文件

*@throwsException*/

publicModelAndView downloadExcel(HttpServletRequest request,

HttpServletResponse response)throwsException {//文件配置

String fileName = new String(("科目导出文件.xls").getBytes("UTF-8"), "iso-8859-1");

response.reset();

response.setHeader("Content-disposition", "attachment;filename=" +fileName);

response.setContentType("application/vnd.ms-excel;charset=utf-8");//创建POI-workbook文件

XSSFWorkbook workbook = newXSSFWorkbook();

XSSFSheet sheet= workbook.createSheet("科目树");//插入抬头

PoiUtil.addRowSXSSF(sheet, 0, new String[] { "说明:", "level1", "level2", "level3", "level4", "level5", "level6","level7", "level8"});//调整sheet样式//红

CellStyle styleRed =workbook.createCellStyle();

styleRed.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);

styleRed.setFillForegroundColor(IndexedColors.RED.index);//绿

CellStyle styleGreen =workbook.createCellStyle();

styleGreen.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);

styleGreen.setFillForegroundColor(IndexedColors.LIME.index);//灰

CellStyle styleGrey =workbook.createCellStyle();

styleGrey.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);

styleGrey.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);//白

CellStyle styleWhite =workbook.createCellStyle();

styleWhite.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);

styleWhite.setFillForegroundColor(IndexedColors.WHITE.index);//金黄

CellStyle styleGold =workbook.createCellStyle();

styleGold.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);

styleGold.setFillForegroundColor(IndexedColors.GOLD.index);//黄

CellStyle styleYellow =workbook.createCellStyle();

styleYellow.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);

styleYellow.setFillForegroundColor(IndexedColors.YELLOW.index);//亮黄

CellStyle styleLight =workbook.createCellStyle();

styleLight.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);

styleLight.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.index);//柠檬

CellStyle styleLemon =workbook.createCellStyle();

styleLemon.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);

styleLemon.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.index);//设置抬头颜色

XSSFRow row = sheet.getRow(0);

Cell cell= row.getCell(1);

cell.setCellStyle(styleRed);

cell= row.getCell(2);

cell.setCellStyle(styleGreen);

cell= row.getCell(3);

cell.setCellStyle(styleGrey);

cell= row.getCell(4);

cell.setCellStyle(styleWhite);

cell= row.getCell(5);

cell.setCellStyle(styleGold);

cell= row.getCell(6);

cell.setCellStyle(styleYellow);

cell= row.getCell(7);

cell.setCellStyle(styleLight);

cell= row.getCell(8);

cell.setCellStyle(styleLemon);//查询Excel用科目树

List subjectTree = new ArrayList();

Subject root= newSubject();

root.setItemCode("1001");

subjectTree=getSubjectListByRoot(root);//插入数据

int index = 0;for(Subject subject : subjectTree){

index= index + 1;

Row dataRow=sheet.createRow(index);

String lv=subject.getLevel();if(lv.equals("1")){

Cell dataCell= dataRow.createCell(0);

dataCell.setCellValue(subject.getItemName());

dataCell.setCellStyle(styleRed);

}else if(lv.equals("2")){

Cell dataCell= dataRow.createCell(0);

dataCell.setCellValue(subject.getItemName());

dataCell.setCellStyle(styleGreen);

}else if(lv.equals("3")){

Cell dataCell= dataRow.createCell(0);

dataCell.setCellValue(subject.getItemName());

dataCell.setCellStyle(styleGrey);

}else if(lv.equals("4")){

Cell dataCell= dataRow.createCell(0);

dataCell.setCellValue(subject.getItemName());

dataCell.setCellStyle(styleWhite);

}else if(lv.equals("5")){

Cell dataCell= dataRow.createCell(0);

dataCell.setCellValue(subject.getItemName());

dataCell.setCellStyle(styleGold);

}else if(lv.equals("6")){

Cell dataCell= dataRow.createCell(0);

dataCell.setCellValue(subject.getItemName());

dataCell.setCellStyle(styleYellow);

}else if(lv.equals("7")){

Cell dataCell= dataRow.createCell(0);

dataCell.setCellValue(subject.getItemName());

dataCell.setCellStyle(styleLight);

}else if(lv.equals("8")){

Cell dataCell= dataRow.createCell(0);

dataCell.setCellValue(subject.getItemName());

dataCell.setCellStyle(styleLemon);

}

}

sheet.setColumnWidth(0, 9000);//创建输出流,生成文件

OutputStream out = newBufferedOutputStream(response.getOutputStream());

workbook.write(out);

out.flush();

out.close();return null;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值