Java 实现查询数据库下载导出Excel详解

Excel表头

测试→下载导出表头文件,也可以改成直接存到本地磁盘目录下。

package com.kero99.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellUtil;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Component

public class ExportExcel {
	//localhost:12935/20190419_V1.0_hbsb/excelExport


//	public static void main(String[] args) {
	/**
	 * Excel导出
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 * @author ygc
	 */
	@RequestMapping(value = "/excelExport")
	public ResponseEntity<byte[]> excelExport(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		try {
		    String fileName = "项目及方案基本信息.xlsx";
			// 创建Excel工作簿
			XSSFWorkbook wb = new XSSFWorkbook();
			String[] title = {"项目基本信息","","","","","","","","","","","","","","","","建设单位信息","","","","","","",
					"方案基本信息","","","","","","","","","","","", "防治目标","","","","","","水保费用","","","",
					"","","","", "方案编制单位","","","","","","", "技术审查信息","","","","", "方案批复信息","",""};
	
			String[] title1= {"方案审批部门代码","方案审批部门级别","发改委项目编码","生产建设项目名称","项目所属行业","项目类型","项目性质","项目规模","工程总投资",
					"土建投资","设计水平年","建设地点","计划开工时间","计划完工时间","东经","北纬","建设单位名称","建设单位地址",
					"建设单位邮编","法定代表人","项目联系人","项目联系人电话","项目联系人邮箱","受理日期","涉及防治区","原地貌土壤侵蚀模数",
					"项目建设区面积","直接影响区面积","扰动地表面积","损毁水保设施面积","建设期水土流失预测总量","预测水土流失量",
					"新增水土流失主要区域","措施减少水土流失量","弃渣量","扰动土地整治率","水土流失总治理度","土壤流失控制比",
					"拦渣率","植被恢复系数","林草覆盖率","水保总投资","独立费","监理费","监测费","补偿费","工程措施费",
					"植物措施费","临时措施费","水平评价单位等级","编制单位","单位地址","单位法定代表人","单位联系人","单位联系人电话",
					"单位联系人电子信箱","技术审查单位","是否现场检查","会议开始时间","会议结束时间","审查意见发文时间","批复时间","批复文号","批复单位"};

			//创建一个工作表sheet 默认是表名是sheet0
			Sheet sheet = wb.createSheet("项目及方案基本信息");
			//重构表样式
			ExportExcel.sheetStyle(sheet);
			//设置单元格字体
		    Font fontTitle = wb.createFont();
		    //单元格1 设置标题单元格类型
	        CellStyle cellStyleTitle = wb.createCellStyle();
	        //样式重构1
	        ExportExcel.cellStyleTitle1(cellStyleTitle,fontTitle);
	        //单元格2
            CellStyle cellStyleTitle1 = wb.createCellStyle();   
            //样式重构2
            ExportExcel.cellStyleTitle2(cellStyleTitle1,fontTitle);
			// 创建表的第一行
			Row row = sheet.createRow(0);
			row.setHeightInPoints(30);
			// 创建单元格
			Cell cell = null;
			// 循环为第一行三个单元格插入三个标题
			for (int i = 0; i < title.length; i++) {
				cell = row.createCell(i);
				cell.setCellStyle(cellStyleTitle);
				cell.setCellValue(title[i]);
			}
			Row row1 = sheet.createRow(1);
			row1.setHeightInPoints(30);
			
			for (int i = 0; i < title1.length; i++) {
				cell = row1.createCell(i);
				cell.setCellStyle(cellStyleTitle1);
				cell.setCellValue(title1[i]);
			}		
			// 追加数据 2是第三行
			for (int i = 2; i <= 10; i++) {
				Row nextrow = sheet.createRow(i);

				Cell cell2 = nextrow.createCell(0);
				cell2.setCellValue("a" + i);

				cell2 = nextrow.createCell(1);
				cell2.setCellValue("user" + i);

				cell2 = nextrow.createCell(2);
				cell2.setCellValue("男");
			}
	
			File file = new File("e:/基本信息.xlsx");
			   FileOutputStream stream=new FileOutputStream(file);
		        wb.write(stream);
		        stream.close();
	
			HttpHeaders headers = new HttpHeaders();
			// 下载显示的文件名,解决中文名称乱码问题
			String downloadFielName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
			// 通知浏览器以attachment(下载方式)打开
			headers.setContentDispositionFormData("attachment", downloadFielName);
			// application/octet-stream : 二进制流数据(最常见的文件下载)。
			headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
			return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
		
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
//		return null;
//	}
//		return null;
	
//	}
//	}

	
	public static void sheetStyle(Sheet sheet){
		// 下标从0开始  起始行号,终止行号, 起始列号,终止列
	    CellRangeAddress region = new CellRangeAddress(0, 0, 0, 15);
	    CellRangeAddress region1 = new CellRangeAddress(0, 0, 16, 22);
	    CellRangeAddress region2 = new CellRangeAddress(0, 0, 23, 34);
	    CellRangeAddress region3 = new CellRangeAddress(0, 0, 35, 40);
	    CellRangeAddress region4 = new CellRangeAddress(0, 0, 41, 48);
	    CellRangeAddress region5 = new CellRangeAddress(0, 0, 49, 55);
	    CellRangeAddress region6 = new CellRangeAddress(0, 0, 56, 60);
	    CellRangeAddress region7 = new CellRangeAddress(0, 0, 61, 63);
		//设置默认宽度
        sheet.setDefaultColumnWidth(25);
        //合并单元格
		sheet.addMergedRegion(region);
		sheet.addMergedRegion(region1);
		sheet.addMergedRegion(region2);
		sheet.addMergedRegion(region3);
		sheet.addMergedRegion(region4);
		sheet.addMergedRegion(region5);
		sheet.addMergedRegion(region6);
		sheet.addMergedRegion(region7);
	}

	public static void cellStyleTitle1(CellStyle cellStyleTitle,Font fontTitle ) {
		fontTitle.setFontHeightInPoints((short) 12); // 字体大小
		fontTitle.setColor(HSSFColor.BLACK.index); // 字体颜色
		fontTitle.setFontName("宋体"); // 字体
		fontTitle.setBoldweight(Font.BOLDWEIGHT_BOLD); // 粗体显示
		cellStyleTitle.setFont(fontTitle);
		cellStyleTitle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
		cellStyleTitle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		cellStyleTitle.setAlignment(CellStyle.ALIGN_CENTER); // 水平布局:居中
		cellStyleTitle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		cellStyleTitle.setWrapText(true);// 设置自动换行
		cellStyleTitle.setBorderBottom(CellStyle.ALIGN_CENTER); // 下边框
		cellStyleTitle.setBorderLeft(CellStyle.ALIGN_CENTER);// 左边框
		cellStyleTitle.setBorderTop(CellStyle.ALIGN_CENTER);// 上边框
		cellStyleTitle.setBorderRight(CellStyle.ALIGN_CENTER);// 右边框
		// 设置边框颜色
		cellStyleTitle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
		cellStyleTitle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
		cellStyleTitle.setTopBorderColor(IndexedColors.BLACK.getIndex());
		cellStyleTitle.setRightBorderColor(IndexedColors.BLACK.getIndex());
	}
	public static void cellStyleTitle2(CellStyle cellStyleTitle1,Font fontTitle ) {
		  cellStyleTitle1.setFont(fontTitle);
          cellStyleTitle1.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
          cellStyleTitle1.setFillPattern(CellStyle.SOLID_FOREGROUND);       
          cellStyleTitle1.setAlignment(CellStyle.ALIGN_LEFT); //水平布局:居中
          cellStyleTitle1.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
          cellStyleTitle1.setWrapText(true);//设置自动换行
          cellStyleTitle1.setBorderBottom(CellStyle.ALIGN_CENTER); //下边框  
          cellStyleTitle1.setBorderLeft(CellStyle.ALIGN_CENTER);//左边框   
          cellStyleTitle1.setBorderTop(CellStyle.ALIGN_CENTER);//上边框    
          cellStyleTitle1.setBorderRight(CellStyle.ALIGN_CENTER);//右边框   
          //设置边框颜色
          cellStyleTitle1.setBottomBorderColor(IndexedColors.BLACK.getIndex());
          cellStyleTitle1.setLeftBorderColor(IndexedColors.BLACK.getIndex());
          cellStyleTitle1.setTopBorderColor(IndexedColors.BLACK.getIndex());
          cellStyleTitle1.setRightBorderColor(IndexedColors.BLACK.getIndex());
	}
}

导出的表头如下图↓

查询数据库导出Excel完整版实现

public static void sheetStyle(Sheet sheet){
		// 下标从0开始  起始行号,终止行号, 起始列号,终止列
	    CellRangeAddress region = new CellRangeAddress(0, 0, 0, 15);
	    CellRangeAddress region1 = new CellRangeAddress(0, 0, 16, 22);
	    CellRangeAddress region2 = new CellRangeAddress(0, 0, 23, 34);
	    CellRangeAddress region3 = new CellRangeAddress(0, 0, 35, 40);
	    CellRangeAddress region4 = new CellRangeAddress(0, 0, 41, 48);
	    CellRangeAddress region5 = new CellRangeAddress(0, 0, 49, 55);
	    CellRangeAddress region6 = new CellRangeAddress(0, 0, 56, 60);
	    CellRangeAddress region7 = new CellRangeAddress(0, 0, 61, 63);
		//设置默认宽度
        sheet.setDefaultColumnWidth(25);
        //合并单元格
		sheet.addMergedRegion(region);
		sheet.addMergedRegion(region1);
		sheet.addMergedRegion(region2);
		sheet.addMergedRegion(region3);
		sheet.addMergedRegion(region4);
		sheet.addMergedRegion(region5);
		sheet.addMergedRegion(region6);
		sheet.addMergedRegion(region7);
	}

	public static void cellStyleTitle1(CellStyle cellStyleTitle,Font fontTitle ) {
		fontTitle.setFontHeightInPoints((short) 12); // 字体大小
		fontTitle.setColor(HSSFColor.BLACK.index); // 字体颜色
		fontTitle.setFontName("宋体"); // 字体
		fontTitle.setBoldweight(Font.BOLDWEIGHT_BOLD); // 粗体显示
		cellStyleTitle.setFont(fontTitle);
		cellStyleTitle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
		cellStyleTitle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		cellStyleTitle.setAlignment(CellStyle.ALIGN_CENTER); // 水平布局:居中
		cellStyleTitle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		cellStyleTitle.setWrapText(true);// 设置自动换行
		cellStyleTitle.setBorderBottom(CellStyle.ALIGN_CENTER); // 下边框
		cellStyleTitle.setBorderLeft(CellStyle.ALIGN_CENTER);// 左边框
		cellStyleTitle.setBorderTop(CellStyle.ALIGN_CENTER);// 上边框
		cellStyleTitle.setBorderRight(CellStyle.ALIGN_CENTER);// 右边框
		// 设置边框颜色
		cellStyleTitle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
		cellStyleTitle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
		cellStyleTitle.setTopBorderColor(IndexedColors.BLACK.getIndex());
		cellStyleTitle.setRightBorderColor(IndexedColors.BLACK.getIndex());
	}
	public static void cellStyleTitle2(CellStyle cellStyleTitle1,Font fontTitle ) {
		  cellStyleTitle1.setFont(fontTitle);
          cellStyleTitle1.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
          cellStyleTitle1.setFillPattern(CellStyle.SOLID_FOREGROUND);       
          cellStyleTitle1.setAlignment(CellStyle.ALIGN_LEFT); //水平布局:居中
          cellStyleTitle1.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
          cellStyleTitle1.setWrapText(true);//设置自动换行
          cellStyleTitle1.setBorderBottom(CellStyle.ALIGN_CENTER); //下边框  
          cellStyleTitle1.setBorderLeft(CellStyle.ALIGN_CENTER);//左边框   
          cellStyleTitle1.setBorderTop(CellStyle.ALIGN_CENTER);//上边框    
          cellStyleTitle1.setBorderRight(CellStyle.ALIGN_CENTER);//右边框   
          //设置边框颜色
          cellStyleTitle1.setBottomBorderColor(IndexedColors.BLACK.getIndex());
          cellStyleTitle1.setLeftBorderColor(IndexedColors.BLACK.getIndex());
          cellStyleTitle1.setTopBorderColor(IndexedColors.BLACK.getIndex());
          cellStyleTitle1.setRightBorderColor(IndexedColors.BLACK.getIndex());
	}	
	public ResponseEntity<byte[]> ExportProjectBaseInfo(HttpServletRequest request, String approvalDept) {
		List<LconstructionprojectExtends> temp=buildMapper.findExportProjectBaseInfoByDept(approvalDept);
		System.out.println(temp.get(0).getApprovalDept());
		try {
		    String fileName = "项目及方案基本信息.xlsx";
			// 创建Excel工作簿
			XSSFWorkbook wb = new XSSFWorkbook();
			String[] title = {"项目基本信息","","","","","","","","","","","","","","","","建设单位信息","","","","","","",
					"方案基本信息","","","","","","","","","","","", "防治目标","","","","","","水保费用","","","",
					"","","","", "方案编制单位","","","","","","", "技术审查信息","","","","", "方案批复信息","",""};
//			//重构1 表头标题0行
//			String[] title=ExportExcel.title1Data();
			String[] title1= {"方案审批部门代码","方案审批部门级别","发改委项目编码","生产建设项目名称","项目所属行业","项目类型","项目性质","项目规模","工程总投资",
					"土建投资","设计水平年","建设地点","计划开工时间","计划完工时间","东经","北纬","建设单位名称","建设单位地址",
					"建设单位邮编","法定代表人","项目联系人","项目联系人电话","项目联系人邮箱","受理日期","涉及防治区","原地貌土壤侵蚀模数",
					"项目建设区面积","直接影响区面积","扰动地表面积","损毁水保设施面积","建设期水土流失预测总量","预测水土流失量",
					"新增水土流失主要区域","措施减少水土流失量","弃渣量","扰动土地整治率","水土流失总治理度","土壤流失控制比",
					"拦渣率","植被恢复系数","林草覆盖率","水保总投资","独立费","监理费","监测费","补偿费","工程措施费",
					"植物措施费","临时措施费","水平评价单位等级","编制单位","单位地址","单位法定代表人","单位联系人","单位联系人电话",
					"单位联系人电子信箱","技术审查单位","是否现场检查","会议开始时间","会议结束时间","审查意见发文时间","批复时间","批复文号","批复单位"};
			//重构2 表头标题1行
//			String[] title1=ExportExcel.title1Data();
			//创建一个工作表sheet 默认是表名是sheet0
			Sheet sheet = wb.createSheet("项目及方案基本信息");
			//重构表样式
			ExportExcel.sheetStyle(sheet);
			//设置单元格字体
		    Font fontTitle = wb.createFont();
		    //单元格1 设置标题单元格类型
	        CellStyle cellStyleTitle = wb.createCellStyle();
	        //样式重构1
	        ExportExcel.cellStyleTitle1(cellStyleTitle,fontTitle);
	        //单元格2
            CellStyle cellStyleTitle1 = wb.createCellStyle();   
            //样式重构2
            ExportExcel.cellStyleTitle2(cellStyleTitle1,fontTitle);
			// 创建表的第一行
			Row row = sheet.createRow(0);
			row.setHeightInPoints(30);
			// 创建单元格
			Cell cell = null;
			// 循环为第一行三个单元格插入三个标题
			for (int i = 0; i < title.length; i++) {
				cell = row.createCell(i);
				cell.setCellStyle(cellStyleTitle);
				cell.setCellValue(title[i]);
			}
			Row row1 = sheet.createRow(1);
			row1.setHeightInPoints(30);
			
			for (int i = 0; i < title1.length; i++) {
				cell = row1.createCell(i);
				cell.setCellStyle(cellStyleTitle1);
				cell.setCellValue(title1[i]);
			}		
			// 追加数据 2是第三行
			for (int i = 2; i <= temp.size(); i++) {
				Row nextrow = sheet.createRow(i);	
				Cell cell2 = nextrow.createCell(0);
				cell2.setCellValue(temp.get(i-2).getApprovalDept());
				cell2 = nextrow.createCell(1);
				cell2.setCellValue(temp.get(i-2).getLevel());
				cell2 = nextrow.createCell(2);
				cell2.setCellValue(temp.get(i-2).getFgwCode()==null?"":temp.get(i-2).getFgwCode());	
				cell2 = nextrow.createCell(3);
				cell2.setCellValue(temp.get(i-2).getName());	
				cell2 = nextrow.createCell(4);
				cell2.setCellValue(temp.get(i-2).getIndustry());	
				cell2 = nextrow.createCell(5);
				cell2.setCellValue(temp.get(i-2).getProjecttype());		
				cell2 = nextrow.createCell(6);
				cell2.setCellValue(temp.get(i-2).getNature());	
				cell2 = nextrow.createCell(7);
				cell2.setCellValue(temp.get(i-2).getProjectscale()==null?"":temp.get(i-2).getProjectscale());	
				cell2 = nextrow.createCell(8);
				cell2.setCellValue(temp.get(i-2).getTotalinvestment()==null?"":temp.get(i-2).getTotalinvestment().toString());		
				cell2 = nextrow.createCell(9);
				cell2.setCellValue(temp.get(i-2).getBuildinvestment()==null?"":temp.get(i-2).getBuildinvestment().toString());		
				cell2 = nextrow.createCell(10);
				cell2.setCellValue(temp.get(i-2).getMonitoryear()==null?"":temp.get(i-2).getMonitoryear().toString());			
				cell2 = nextrow.createCell(11);
				cell2.setCellValue(temp.get(i-2).getProjelocation()==null?"":temp.get(i-2).getProjelocation());		
				cell2 = nextrow.createCell(12);
				cell2.setCellValue(temp.get(i-2).getStarttime()==null?"":CommonUtil.getSysDate(temp.get(i-2).getStarttime()).toString());			
				cell2 = nextrow.createCell(13);
				cell2.setCellValue(temp.get(i-2).getEndtime()==null?"":CommonUtil.getSysDate(temp.get(i-2).getEndtime())==null?"":CommonUtil.getSysDate(temp.get(i-2).getEndtime()).toString());
				cell2 = nextrow.createCell(14);
				cell2.setCellValue(temp.get(i-2).getLongitude()==null?"":temp.get(i-2).getLongitude().toString());
				cell2 = nextrow.createCell(15);
				cell2.setCellValue(temp.get(i-2).getLatitude()==null?"":temp.get(i-2).getLatitude().toString());			
				cell2 = nextrow.createCell(16);
				cell2.setCellValue(temp.get(i-2).getConstructioncompany()==null?"":temp.get(i-2).getConstructioncompany());
				cell2 = nextrow.createCell(17);
				cell2.setCellValue(temp.get(i-2).getConstructionaddress()==null?"":temp.get(i-2).getConstructionaddress());
				cell2 = nextrow.createCell(18);
				cell2.setCellValue(temp.get(i-2).getConstructionpostal()==null?"":temp.get(i-2).getConstructionpostal());
				cell2 = nextrow.createCell(19);
				cell2.setCellValue(temp.get(i-2).getConstructionlegal()==null?"":temp.get(i-2).getConstructionlegal());	
				cell2 = nextrow.createCell(20);
				cell2.setCellValue(temp.get(i-2).getConstructioncontact()==null?"":temp.get(i-2).getConstructioncontact());		
				cell2 = nextrow.createCell(21);
				cell2.setCellValue(temp.get(i-2).getConstructionnumber()==null?"":temp.get(i-2).getConstructionnumber());		
				cell2 = nextrow.createCell(22);
				cell2.setCellValue(temp.get(i-2).getConstructionemail()==null?"":temp.get(i-2).getConstructionemail());		
				cell2 = nextrow.createCell(23);
				cell2.setCellValue(temp.get(i-2).getAcceptancetime()==null?"":CommonUtil.getSysDate(temp.get(i-2).getAcceptancetime()).toString());
				cell2 = nextrow.createCell(24);
				cell2.setCellValue(temp.get(i-2).getInvolvingarea()==null?"":temp.get(i-2).getInvolvingarea().toString());
				cell2 = nextrow.createCell(25);
				cell2.setCellValue(temp.get(i-2).getModulus()==null?"":temp.get(i-2).getModulus().toString());
				cell2 = nextrow.createCell(26);
				cell2.setCellValue(temp.get(i-2).getArea()==null?"":temp.get(i-2).getArea().toString());
				cell2 = nextrow.createCell(27);
				cell2.setCellValue(String.valueOf(temp.get(i-2).getImpactarea())==null?"":String.valueOf(temp.get(i-2).getImpactarea()));
				cell2 = nextrow.createCell(28);
				cell2.setCellValue(temp.get(i-2).getDisturbedarea()==null?"":temp.get(i-2).getDisturbedarea().toString());
				cell2 = nextrow.createCell(29);
				cell2.setCellValue(temp.get(i-2).getFacilityarea()==null?"":temp.get(i-2).getFacilityarea().toString());
				cell2 = nextrow.createCell(30);
				cell2.setCellValue(temp.get(i-2).getTotalforecast()==null?"":temp.get(i-2).getTotalforecast().toString());
				cell2 = nextrow.createCell(31);
				cell2.setCellValue(temp.get(i-2).getTotalnew()==null?"":temp.get(i-2).getTotalnew().toString());
				cell2 = nextrow.createCell(32);
				cell2.setCellValue(temp.get(i-2).getAddmainlosearea()==null?"":temp.get(i-2).getAddmainlosearea().toString());
				cell2 = nextrow.createCell(33);
				cell2.setCellValue(temp.get(i-2).getTotalreduce()==null?"":temp.get(i-2).getTotalreduce().toString());
				cell2 = nextrow.createCell(34);
				cell2.setCellValue(temp.get(i-2).getWasteslag()==null?"":temp.get(i-2).getWasteslag().toString());
				cell2 = nextrow.createCell(35);
				cell2.setCellValue(temp.get(i-2).getDisturbedrate()==null?"":temp.get(i-2).getDisturbedrate().toString());
				cell2 = nextrow.createCell(36);
				cell2.setCellValue(temp.get(i-2).getLossrate()==null?"":temp.get(i-2).getLossrate().toString());
				cell2 = nextrow.createCell(37);
				cell2.setCellValue(temp.get(i-2).getSoilrate()==null?"":temp.get(i-2).getSoilrate().toString());	
				cell2 = nextrow.createCell(38);
				cell2.setCellValue(temp.get(i-2).getBlockingrate()==null?"":temp.get(i-2).getBlockingrate().toString());
				cell2 = nextrow.createCell(39);
				cell2.setCellValue(temp.get(i-2).getRecoveryrate()==null?"":temp.get(i-2).getRecoveryrate().toString());			
				cell2 = nextrow.createCell(40);
				cell2.setCellValue(temp.get(i-2).getCoveragerate()==null?"":temp.get(i-2).getCoveragerate().toString());
				cell2 = nextrow.createCell(41);
				cell2.setCellValue(temp.get(i-2).getXmfyConservationinvestment()==null?"":temp.get(i-2).getXmfyConservationinvestment().toString());
				cell2 = nextrow.createCell(42);
				cell2.setCellValue(temp.get(i-2).getIndependent()==null?"":temp.get(i-2).getIndependent().toString());
				cell2 = nextrow.createCell(43);
				cell2.setCellValue(temp.get(i-2).getSupervisionfee()==null?"":temp.get(i-2).getSupervisionfee().toString());		
				cell2 = nextrow.createCell(44);
				cell2.setCellValue(temp.get(i-2).getMonitoringfee()==null?"":temp.get(i-2).getMonitoringfee().toString());
				cell2 = nextrow.createCell(45);
				cell2.setCellValue(temp.get(i-2).getCompensation()==null?"":temp.get(i-2).getCompensation().toString());
				cell2 = nextrow.createCell(46);
				cell2.setCellValue(temp.get(i-2).getXmfyProjectmeasures()==null?"":temp.get(i-2).getXmfyProjectmeasures().toString());
				cell2 = nextrow.createCell(47);
				cell2.setCellValue(temp.get(i-2).getXmfyPlantmeasures()==null?"":temp.get(i-2).getXmfyPlantmeasures().toString());
				cell2 = nextrow.createCell(48);
				cell2.setCellValue(temp.get(i-2).getXmfyTemporarymeasures()==null?"":temp.get(i-2).getXmfyTemporarymeasures().toString());		
				cell2 = nextrow.createCell(49);
				cell2.setCellValue(temp.get(i-2).getCompilelevel()==null?"":temp.get(i-2).getCompilelevel().toString());
				cell2 = nextrow.createCell(50);
				cell2.setCellValue(temp.get(i-2).getCompile()==null?"":temp.get(i-2).getCompile().toString());
				cell2 = nextrow.createCell(51);
				cell2.setCellValue(temp.get(i-2).getCompileaddress()==null?"":temp.get(i-2).getCompileaddress().toString());
				cell2 = nextrow.createCell(52);
				cell2.setCellValue(temp.get(i-2).getCompilelegal()==null?"":temp.get(i-2).getCompilelegal().toString());
				cell2 = nextrow.createCell(53);
				cell2.setCellValue(temp.get(i-2).getCompilecontact()==null?"":temp.get(i-2).getCompilecontact().toString());		
				cell2 = nextrow.createCell(54);
				cell2.setCellValue(temp.get(i-2).getCompilenumber()==null?"":temp.get(i-2).getCompilenumber().toString());
				cell2 = nextrow.createCell(55);
				cell2.setCellValue(temp.get(i-2).getCompileemail()==null?"":temp.get(i-2).getCompilenumber().toString());
				cell2 = nextrow.createCell(56);
				cell2.setCellValue(temp.get(i-2).getReviewunitcompany()==null?"":temp.get(i-2).getReviewunitcompany().toString());
				cell2 = nextrow.createCell(57);
				cell2.setCellValue(temp.get(i-2).getIslocale()==null?"":temp.get(i-2).getIslocale().toString());
				cell2 = nextrow.createCell(58);
				cell2.setCellValue(temp.get(i-2).getMeetstime()==null?"":CommonUtil.getSysTime2(temp.get(i-2).getMeetstime()).toString());
				cell2 = nextrow.createCell(59);
				cell2.setCellValue(temp.get(i-2).getMeetetime()==null?"":CommonUtil.getSysTime2(temp.get(i-2).getMeetetime()).toString());		
				cell2 = nextrow.createCell(60);
				cell2.setCellValue(temp.get(i-2).getPublishtime()==null?"":CommonUtil.getSysDate(temp.get(i-2).getPublishtime()).toString());
				cell2 = nextrow.createCell(61);
				cell2.setCellValue(temp.get(i-2).getReplydate()==null?"":CommonUtil.stringToDate(temp.get(i-2).getReplydate().toString()).toString());
				cell2 = nextrow.createCell(62);
				cell2.setCellValue(temp.get(i-2).getReplynum()==null?"":temp.get(i-2).getReplynum().toString());
				cell2 = nextrow.createCell(63);
				cell2.setCellValue(temp.get(i-2).getReplyunit()==null?"":temp.get(i-2).getReplyunit().toString());
			

			
			}
			String path=Commons.PROJECT_BASE_INFO_EXCEL_PATH+File.separator+fileName;
			File storeDirectory = new File(Commons.PROJECT_BASE_INFO_EXCEL_PATH);
			if (!storeDirectory.exists() &&!storeDirectory.isDirectory()) {
				storeDirectory.mkdirs();
			}
			File file = new File(path);
			   FileOutputStream stream=new FileOutputStream(file);
		        wb.write(stream);
		        stream.close();
			HttpHeaders headers = new HttpHeaders();
			// 下载显示的文件名,解决中文名称乱码问题
			String downloadFielName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
			// 通知浏览器以attachment(下载方式)打开
			headers.setContentDispositionFormData("attachment", downloadFielName);
			// application/octet-stream : 二进制流数据(最常见的文件下载)。
			headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
			return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
		
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;

最终Excel导出样式

发布了117 篇原创文章 · 获赞 32 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/qq_17025903/article/details/97777440