Collect from 企业网站模板

poi导出图片

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

html

<a href="${ctx }/stu/getExcel" class="btn btn-success">导出excel</a>
	<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi</artifactId>
		    <version>3.13</version>
		</dependency>
		<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi-ooxml</artifactId>
		    <version>3.13</version>
		</dependency>
	<dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.9</version>
    </dependency>

导出图片: 

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



		String title =( o.getSys_name()==null?"":o.getSys_name() )+( o.getMajor_name()==null?"":o.getMajor_name() )+ "学生表";

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

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

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

		region = new CellRangeAddress(1, 1, 0, 5);
		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_ = "电子照,学号,姓名,性别,学院,专业";

		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<EtStu> all_li = etStuService.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 (EtStu 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();
			}
			int c = 1;
			row.createCell(c).setCellValue(s.getStu_no()); c++;
			row.createCell(c).setCellValue(s.getRealname()); c++;
			row.createCell(c).setCellValue(s.getSex()); c++;
			row.createCell(c).setCellValue(s.getSys_name()); c++;
			row.createCell(c).setCellValue(s.getMajor_name()); 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);

	}
	

下载工具

import org.apache.poi.hssf.usermodel.*;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

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

点击加载更多...