Collect from 企业网站模板

poi导出图片

第一次登录送5个金币,绑定QQ送5个金币。金币可以用来观看视频和下载资源哦!

导出图片: 

@RequestMapping("/getExcel")
	public void getExcel(DyUsers o, HttpServletResponse response,
			HttpSession session, HttpServletRequest request) throws Exception {

		String t = "";
		if (o.getState().equals("1"))
			t = "党员";
		if (o.getState().equals("2"))
			t = "预备党员";
		if (o.getState().equals("3"))
			t = "入党积极分子";
		String title = t + "表";

		ExcelUtils excelutils = new ExcelUtils();
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet(title);
		HSSFCellStyle cellStyle = workbook.createCellStyle();

		// 大标题
		CellRangeAddress region = new CellRangeAddress(0, 0, 0, 21);
		sheet.addMergedRegion(region);

		String[] big_titles = { title };
		excelutils.createTitle(workbook, sheet, big_titles);

		region = new CellRangeAddress(1, 1, 0, 21);
		sheet.addMergedRegion(region);
		HSSFRow row1 = sheet.createRow(1);
		row1.createCell(0).setCellValue(
				"日期:" + DateUtils.DateToString(new Date()));
		cellStyle = workbook.createCellStyle();
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT); // 居中
		row1.getCell(0).setCellStyle(cellStyle);

		cellStyle = workbook.createCellStyle();
		HSSFFont font = workbook.createFont();
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		cellStyle.setFont(font);
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中

		// 设置列
		cellStyle = workbook.createCellStyle();
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		cellStyle.setFont(font);
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
		String titles_ = "";
		if (o.getState().equals("1")) {
			titles_ = "电子照,党员编号,学号,姓名,性别,出生日期,民族,身份证,籍贯,机构名称,职业,文化程度,入党时间,学校,学院,系,专业,党内职务,行政职务,流入时间,原机构名称,原机构电话,入党介绍人1,入党介绍人2,家庭住址,备注,状态";
		} else {
			titles_ = "电子照,学号,姓名,性别,出生日期,民族,身份证,籍贯,职业,文化程度,录入时间,机构名称,学校,学院,系,专业,培养联系人1,培养联系人2,家庭住址,备注,状态,成为积极分子时间,成为预备党员时间";
		}
		HSSFRow row2 = sheet.createRow(2);

		String[] titles = titles_.split(",");

		for (int i = 0; i < titles.length; i++) {
			row2.createCell(i).setCellValue(titles[i]);
			row2.getCell(i).setCellStyle(cellStyle);
		}

		int rowNum = 3;

		List<DyUsers> all_li = usersService.list(o);

		cellStyle = workbook.createCellStyle();
		cellStyle.setWrapText(true);

		// 画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
		HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
		// anchor主要用于设置图片的属性
		sheet.setColumnWidth(0, 15 * 256);

		cellStyle = workbook.createCellStyle();
		cellStyle.setWrapText(true);

		for (DyUsers s : all_li) {

			HSSFRow row = sheet.createRow(rowNum);
			sheet.getRow(rowNum).setHeightInPoints(100);

			if (s.getPhoto() != null && !s.getPhoto().isEmpty()) {
				String abPath = request.getSession().getServletContext()
						.getRealPath("")
						+ s.getPhoto();
				BufferedImage bufferImg = ImageIO.read(new File(abPath));
				ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
				ImageIO.write(bufferImg, "jpg", byteArrayOut);
				HSSFClientAnchor anchor = new HSSFClientAnchor(30, 15, 0, 0,
						(short) (0), rowNum, (short) (0), rowNum);
				anchor.setAnchorType(3);
				// 插入图片
				HSSFPicture p = patriarch.createPicture(anchor, workbook
						.addPicture(byteArrayOut.toByteArray(),
								HSSFWorkbook.PICTURE_TYPE_JPEG));
				p.resize(0.99, 0.90);

				bufferImg.flush();
				byteArrayOut.close();
			}

			if (o.getState().equals("1")) {
				int c = 1;
				row.createCell(c).setCellValue(s.getDy_num());
				c++;
				row.createCell(c).setCellValue(s.getStu_num());
				c++;
				row.createCell(c).setCellValue(s.getRealname());
				c++;
				row.createCell(c).setCellValue(s.getSex());
				c++;
				row.createCell(c).setCellValue(s.getBirth());
				c++;
				row.createCell(c).setCellValue(s.getEthnic());
				c++;
				row.createCell(c).setCellValue(s.getIdcard());
				c++;
				row.createCell(c).setCellValue(s.getHometown());
				c++;
				row.createCell(c).setCellValue(s.getBname());
				c++;
				row.createCell(c).setCellValue(s.getProfessional());
				c++;
				row.createCell(c).setCellValue(s.getEducation_level());
				c++;
				row.createCell(c).setCellValue(s.getJoin_party_ts());
				c++;
				row.createCell(c).setCellValue(s.getUniversity());
				c++;
				row.createCell(c).setCellValue(s.getCollege());
				c++;
				row.createCell(c).setCellValue(s.getMajor());
				c++;
				row.createCell(c).setCellValue(s.getClass_name());
				c++;
				row.createCell(c).setCellValue(s.getParty_position());
				c++;
				row.createCell(c).setCellValue(s.getAdministrative_position());
				c++;
				row.createCell(c).setCellValue(s.getIn_ts());
				c++;
				row.createCell(c).setCellValue(s.getPre_organization_name());
				c++;
				row.createCell(c).setCellValue(s.getPre_organization_tel());
				c++;
				if (s.getForm_().equals("1")) {
					row.createCell(c).setCellValue(
							s.getDy_references_realname1());
					c++;
					row.createCell(c).setCellValue(
							s.getDy_references_realname2());
					c++;
				} else {
					row.createCell(c).setCellValue(s.getReferences_realname1());
					c++;
					row.createCell(c).setCellValue(s.getReferences_realname2());
					c++;
				}
				row.createCell(c).setCellValue(s.getAddress());
				c++;
				row.createCell(c).setCellValue(s.getNote());
				c++;
				row.createCell(c).setCellValue("党员");
			} else {
				int c = 1;
				row.createCell(c).setCellValue(s.getStu_num());
				c++;
				row.createCell(c).setCellValue(s.getRealname());
				c++;
				row.createCell(c).setCellValue(s.getSex());
				c++;
				row.createCell(c).setCellValue(s.getBirth());
				c++;
				row.createCell(c).setCellValue(s.getEthnic());
				c++;
				row.createCell(c).setCellValue(s.getIdcard());
				c++;
				row.createCell(c).setCellValue(s.getHometown());
				c++;
				row.createCell(c).setCellValue(s.getProfessional());
				c++;
				row.createCell(c).setCellValue(s.getEducation_level());
				c++;
				row.createCell(c).setCellValue(s.getCts());
				c++;
				row.createCell(c).setCellValue(s.getBname());
				c++;
				row.createCell(c).setCellValue(s.getUniversity());
				c++;
				row.createCell(c).setCellValue(s.getCollege());
				c++;
				row.createCell(c).setCellValue(s.getMajor());
				c++;
				row.createCell(c).setCellValue(s.getClass_name());
				c++;
				row.createCell(c).setCellValue(s.getReferences_realname1());
				c++;
				row.createCell(c).setCellValue(s.getReferences_realname2());
				c++;
				row.createCell(c).setCellValue(s.getAddress());
				c++;
				row.createCell(c).setCellValue(s.getNote());
				c++;
				row.createCell(c).setCellValue(
						s.getState().equals("3") ? "积极分子" : "预备党员");
				c++;
				row.createCell(c).setCellValue(s.getCts());
				c++;
				row.createCell(c).setCellValue(s.getCts2());
				c++;
			}

			rowNum++;

		}

		// 设置日期格式
		HSSFCellStyle style = workbook.createCellStyle();
		style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));

		String fileName = title + ".xls";

		// 生成excel文件
		excelutils.buildExcelFile(fileName, workbook);

		// 浏览器下载excel
		excelutils.buildExcelDocument(fileName, workbook, response);

	}

下载工具

public class ExcelUtils {

	 
    //创建表头
    public void  createTitle(HSSFWorkbook workbook,HSSFSheet sheet,String title[]){
        HSSFRow row = sheet.createRow(0);
        //设置列宽,setColumnWidth的第二个参数要乘以256,这个参数的单位是1/256个字符宽度
        sheet.setColumnWidth(1,12*256);
        sheet.setColumnWidth(3,17*256);

        //设置为居中加粗
        HSSFCellStyle style = workbook.createCellStyle();
        HSSFFont font = workbook.createFont();
        font.setBold(true);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        style.setFont(font);

        HSSFCell cell;
        for(int i=0;i<title.length;i++){
        	 cell = row.createCell(i);
             cell.setCellValue(title[i]);
             cell.setCellStyle(style);
        }
         
    }
  //生成excel文件
    public void buildExcelFile(String filename,HSSFWorkbook workbook) throws Exception{
        //String abp="D:/apache-tomcat-8.5.57/wtpwebapps/";
       // String abp="/home/xys20813ixoyns42n0h8o1f3/wwwroot/";
    	FileOutputStream fos = new FileOutputStream(filename);
        workbook.write(fos);
        fos.flush();
        fos.close();
    }

    //浏览器下载excel
    public void buildExcelDocument(String filename,HSSFWorkbook workbook,HttpServletResponse response) throws Exception{
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(filename, "utf-8"));
        OutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }
    
    

     
}

image.png

在此处输入评论

允许提问者通过qq联系我

    现在绑定QQ

点击加载更多...