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

Java 导出数据Excel

程序员文章站 2022-06-02 18:24:39
...
     @RequestMapping(value = "/expXsKs")
	@ResponseBody
	public void exportTdxx(HttpSession session,HttpServletResponse response,XsksPage xskspage) throws IOException{
		XSSFWorkbook workbook = new XSSFWorkbook();
		XSSFDataFormat format = workbook.createDataFormat();
		response.setCharacterEncoding("UTF-8");
		ServletOutputStream outStream = response.getOutputStream();
        XSSFSheet sheet = workbook.createSheet("文件");
		
		sheet.setDefaultColumnWidth((short) 12);
		sheet.setColumnWidth(0, 5 * 800);
		sheet.setColumnWidth(1, 5 * 800);
		sheet.setColumnWidth(2, 5 * 800);
		sheet.setColumnWidth(3, 5 * 800);
		sheet.setColumnWidth(4, 5 * 800);
		sheet.setColumnWidth(5, 5 * 800);
		sheet.setColumnWidth(6, 6 * 800);
		
		//常规字体(居中 )
		XSSFCellStyle style4_1 = workbook.createCellStyle();
		//设置水平对齐的样式为居中对齐;  
		style4_1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		//设置垂直对齐的样式为居中对齐;
		style4_1.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER);
		XSSFFont font3_1 = workbook.createFont();
		//在样式用应用设置的字体;
		font3_1.setFontName("宋体");
		//字体高度(以点为单位) 设置字体大小  
		font3_1.setFontHeightInPoints((short) 10);
		//加粗
		font3_1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		style4_1.setFont(font3_1);
		style4_1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style4_1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style4_1.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style4_1.setBorderTop(HSSFCellStyle.BORDER_THIN);
		//设置底边框颜色;// 黑色
		style4_1.setBottomBorderColor(IndexedColors.BLACK.getIndex());// 黑色
		//设置左边框颜色;// 黑色
		style4_1.setLeftBorderColor(IndexedColors.BLACK.getIndex());// 黑色
		//设置上边框颜色;// 黑色
		style4_1.setTopBorderColor(IndexedColors.BLACK.getIndex());// 黑色
		//设置右边框颜色;// 黑色
		style4_1.setRightBorderColor(IndexedColors.BLACK.getIndex());// 黑色
		//设置自动换行; 
		style4_1.setWrapText(true);
		style4_1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		style4_1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style4_1.setDataFormat(format.getFormat("@"));
			
		//第一行
		XSSFRow row = sheet.createRow((short) 0);
		row.setHeight((short)800);//高度
		XSSFCellStyle style = workbook.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		XSSFFont font = workbook.createFont(); 
		font.setFontName("黑体");
		font.setFontHeightInPoints((short) 18);// 设置字体大小
		style.setFont(font);
		XSSFCell cell1;
//		for(int i = 0;i<9;i++){
			cell1 = row.createCell(0);
//			if(i==0){
				cell1.setCellValue("标题");
//			}
			cell1.setCellStyle(style);
//		}
		//参数1:行号 参数2:起始列号 参数3:行号 参数4:终止列号
		sheet.addMergedRegion(new CellRangeAddress(0, (short) (0), 0, (short) (4)));
		
		
		//第二行
		XSSFRow row1 = sheet.createRow((short) 1);
		row1.setHeight((short)400);
		
		XSSFCell cell71   = row1.createCell(0); 
		cell71.setCellValue("第一列");
		cell71.setCellStyle(style4_1);
		XSSFCell cell711   = row1.createCell(1); 
		cell711.setCellValue("第二列");
		cell711.setCellStyle(style4_1);
		XSSFCell cell712   = row1.createCell(2); 
		cell712.setCellValue("第三列");
		cell712.setCellStyle(style4_1);
		XSSFCell cell73 = row1.createCell((short) 3);
		cell73.setCellValue("第四列");
		cell73.setCellStyle(style4_1);
		XSSFCell cell74 = row1.createCell((short) 4);
		cell74.setCellValue("第五列");
		cell74.setCellStyle(style4_1);
		
		//循环开始
		int i = 2;
		xskspage.setSftj("1");
		xskspage.setPageSize(1000000);
		List<XsksEntity> jspyksList=moocKswhService.query(xskspage);
		for(XsksEntity obj : jspyksList){
			XSSFRow row_n = sheet.createRow((short) i);
			
			XSSFCell cell_n1   = row_n.createCell(0); 
			cell_n1.setCellValue(obj.getXsxm());
			cell_n1.setCellStyle(style4_1);
			cell_n1.setCellType(HSSFCell.CELL_TYPE_STRING);
			
			XSSFCell cell_n11   = row_n.createCell(1); 
			cell_n11.setCellValue(obj.getXsdm());
			cell_n11.setCellStyle(style4_1);
			cell_n11.setCellType(HSSFCell.CELL_TYPE_STRING);
			
			XSSFCell cell_n2 = row_n.createCell((short) 2);
			cell_n2.setCellValue(obj.getZsxh());
			cell_n2.setCellStyle(style4_1);
			cell_n2.setCellType(HSSFCell.CELL_TYPE_STRING);
			
			XSSFCell cell_n3 = row_n.createCell((short) 3);
			cell_n3.setCellValue(obj.getHpcj());
			cell_n3.setCellStyle(style4_1);
			cell_n3.setCellType(HSSFCell.CELL_TYPE_STRING);
			
			XSSFCell cell_n4 = row_n.createCell((short) 4);
			cell_n4.setCellValue(obj.getSfpy()!=null&&obj.getSfpy().equals("1")?obj.getKscj():"");
			cell_n4.setCellStyle(style4_1);
			cell_n4.setCellType(HSSFCell.CELL_TYPE_STRING);
			i++;
		}
		
		sheet.getPrintSetup().setLandscape(true);//设置为横向打印
		String fileName ="导出文件名称.xlsx";
		fileName=new String(fileName.getBytes("GBK"),"ISO-8859-1");
		response.setHeader("Content-disposition","attachment;filename=\""+fileName+"\"");
		response.setContentType("application/octet-stream;charset=UTF-8");
		workbook.write(outStream);
		outStream.close();
	}
相关标签: Java excel 导出