zoukankan      html  css  js  c++  java
  • java 根据excel模板导出文件

    <!--读取excel文件,配置POI框架的依赖-->
    		<dependency>
    			<groupId>org.apache.poi</groupId>
    			<artifactId>poi</artifactId>
    			<version>3.17</version>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.poi</groupId>
    			<artifactId>poi-ooxml</artifactId>
    			<version>3.17</version>
    		</dependency>
    

      

    package com.example.demo;
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.UnsupportedEncodingException;
    import java.net.URLEncoder;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping(value = "/index")
    public class IndexController {
    	
    	@RequestMapping(value = "/excel2007Export")
    	public ResponseEntity<Resource> excel2007Export(HttpServletResponse response, HttpServletRequest request) 
    	{
    		try
    		{
    			ClassPathResource cpr = new ClassPathResource("/student.xlsx");
    			InputStream is = cpr.getInputStream();
    			Workbook workbook = new XSSFWorkbook(is);
    			org.apache.poi.ss.usermodel.Sheet sheet0 = workbook.getSheetAt(0);
    			// 这里作为演示,造几个演示数据,模拟数据库里查数据
    			List<String> list = new ArrayList<String>();
    			list.add("张三");
    			list.add("张四");
    			list.add("王五");
    			for (int i = 0; i < list.size(); i++) 
    			{
    				Row row = sheet0.createRow(i + 5);
    				row.createCell(0).setCellValue(i);
    				row.createCell(1).setCellValue(list.get(i));
    			}
    			String fileName = "eeeee.xlsx";
    			downLoadExcel(fileName, response, workbook);
    
    		} 
    		catch (FileNotFoundException e) 
    		{
    			e.printStackTrace();
    		} 
    		catch (IOException e)
    		{
    			e.printStackTrace();
    		}
    		return new ResponseEntity<Resource>(HttpStatus.OK);
    	}
    
    	public static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) 
    	{
    		try 
    		{
    			response.setCharacterEncoding("UTF-8");
    			response.setHeader("content-Type", "application/vnd.ms-excel");
    			response.setHeader("Content-Disposition",
    					"attachment;filename="" + URLEncoder.encode(fileName, "UTF-8") + """);
    			workbook.write(response.getOutputStream());
    		} 
    		catch (IOException e)
    		{
    			e.printStackTrace();
    		}
    	}
    
    	@RequestMapping(value = "/exportExcel", method = { RequestMethod.GET, RequestMethod.POST })
    	public void exportExcel(HttpServletResponse response) throws UnsupportedEncodingException {
    		String fileName = "导出excel.xls";
    		response.setContentType("application/excel");
    		response.setHeader("Content-disposition",
    				"attachment;filename=" + fileName + ";filename*=utf-8''" + URLEncoder.encode(fileName, "UTF-8"));
    		HSSFWorkbook workbook = new HSSFWorkbook();
    		HSSFSheet sheet = workbook.createSheet();
    		int rowNum = 0;
    		// 添加标题
    		String[] headers = { "标题", "版块", "作者", "创建时间", "内容" };
    		HSSFRow row = sheet.createRow(rowNum);
    		List<String> list = new ArrayList<String>();
    		list.add("111");
    		list.add("222");
    		list.add("333");
    		for (String text : list) {
    			HSSFRow row1 = sheet.createRow(rowNum);
    			row1.createCell(0).setCellValue(rowNum);
    			row1.createCell(1).setCellValue(text);
    			rowNum++;
    		}
    
    		try {
    			workbook.write(response.getOutputStream());
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    

      

  • 相关阅读:
    最小二乘法拟合圆
    OpenCV solve() 解线性方程
    Halcon、OpenCV、C++ 实现最小二乘法拟合直线
    Halcon 字符串与HTuple互转,double与HTuple互转,Mat与HObject互转
    Halcon的HWindowControl控件在WinForm程序中的使用介绍(重点解决图片缩放的问题)
    将Halcon HObject类型转为Qt QImage类型
    Halcon HImage 与 Qt QImage 的相互转换
    Qt 加载DLL的几种方式小结
    Halcon17无法加载"hdevenginecpp":找不到指定的模块
    Halcon 安装提示 could not write updated path to HKLM
  • 原文地址:https://www.cnblogs.com/scode2/p/11857180.html
Copyright © 2011-2022 走看看