Java 导出数据Excel

     @RequestMapping(value = "/expExcel")
	@ResponseBody
	public void expExcel(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("文件");
		
		//一个字符的1/256的宽度作为一个单位 假如你要的宽的是15个字符 在不设置默认默认宽度的时候就是256*15的宽度
		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
		List<XsksEntity> jspyksList=service.query(page);
		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();
	}

设置excel单元格日期及文本等格式,请点击这里
链接中是对固定的列进行设置,如果想对指定的列进行设置的话可以这么写

//对单独某一列进行样式赋值,第一个参数为列数,第二个参数为样式
Sheet.setDefaultColumnStyle(0, style);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值