在SSM下实现导出Excel

首先准备jar包

poi、poi-ooxml、poi-ooxml-schemas

 前端页面

<div>
            <button onclick="d()">导出</button>

</div> 

<script type="text/javascript">
          function d(){
              window.location.href="${pageContext.request.contextPath}/sys/customerMassWebController/excel";
          }
      </script>

后台

Controller层

@RequestMapping("/excel")
	public void excel(HttpServletResponse response )throws IOException {
		 response.setCharacterEncoding("UTF-8");  
		 List<CustomerMass> list = customerMassService.queryExcelInfo();
	        System.out.println(list);
	      //创建excel文件
	        HSSFWorkbook wb = new HSSFWorkbook();
	        //创建sheet页
	        HSSFSheet sheet = wb.createSheet("客户信息表");
	        //创建标题行
	        HSSFRow titleRow = sheet.createRow(0);
	        titleRow.createCell(0).setCellValue("日期");
	        titleRow.createCell(1).setCellValue("客户名称");
	        titleRow.createCell(2).setCellValue("类型");
	        titleRow.createCell(3).setCellValue("状态");
	        titleRow.createCell(4).setCellValue("客户阶段");
	        titleRow.createCell(5).setCellValue("消费总额");
	        titleRow.createCell(6).setCellValue("欠款金额");
	        titleRow.createCell(7).setCellValue("维护费用");
	        titleRow.createCell(8).setCellValue("业务");
//	        titleRow.createCell(9).setCellValue("创建时间 ");
//	        titleRow.createCell(10).setCellValue("更新时间");
	        //遍历将数据放到excel列中
            for (CustomerMass user : list) {
                HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
                dataRow.createCell(0).setCellValue(user.getCustomdate());
                dataRow.createCell(1).setCellValue(user.getCustomname());
                dataRow.createCell(2).setCellValue(user.getCustomtype());
                dataRow.createCell(3).setCellValue(user.getCustomstatus());
                dataRow.createCell(4).setCellValue(user.getCustomstage());
                dataRow.createCell(5).setCellValue(user.getTotalconsumption());
                dataRow.createCell(6).setCellValue(user.getAmountsowed());
                dataRow.createCell(7).setCellValue(user.getMaintenancecosts());
                dataRow.createCell(8).setCellValue(user.getBusiness());
//                dataRow.createCell(9).setCellValue(user.getCreateDateStart());
//                dataRow.createCell(10).setCellValue(user.getCreateDateEnd());
             
            }
            /*   // 设置下载时客户端Excel的名称  
            String filename =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ".xls";  
               response.setContentType("application/vnd.ms-excel");  
               response.setHeader("Content-disposition", "attachment;filename=" + filename);  */

           // 设置下载时客户端Excel的名称   (上面注释的改进版本,上面的中文不支持)
                   response.setContentType("application/octet-stream;charset=utf-8");
                   response.setHeader("Content-Disposition", "attachment;filename="
                           + new String("客户名单".getBytes(),"iso-8859-1") + ".xls");


               OutputStream ouputStream = response.getOutputStream();  
               wb.write(ouputStream);  
               ouputStream.flush();  
               ouputStream.close();
	   
	        
	}

 实体类

package com.java.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;

import com.java.sys.basic.entity.BaseEntity;
import java.util.Date;

public class CustomerMass extends BaseEntity {
	
	private static final long serialVersionUID = 1L;
	
	private Date customdate;	//日期
	private String customname;	//客户名称
	private String customtype;	//类型
	private String customstatus;	//状态
	private String customstage;	//客户阶段
	private String totalconsumption;	//消费总额
	private String amountsowed;	//欠款金额
	private String maintenancecosts;	//
	private String business;	//业务
	
	public CustomerMass() {
		super();
	}
	
    
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date getCustomdate() {
    	return customdate;
    }
    public void setCustomdate(Date customdate) {
    	this.customdate = customdate;
    }
    
    public String getCustomname() {
    	return customname;
    }
    public void setCustomname(String customname) {
    	this.customname = customname;
    }
    
    public String getCustomtype() {
    	return customtype;
    }
    public void setCustomtype(String customtype) {
    	this.customtype = customtype;
    }
    
    public String getCustomstatus() {
    	return customstatus;
    }
    public void setCustomstatus(String customstatus) {
    	this.customstatus = customstatus;
    }
    
    public String getCustomstage() {
    	return customstage;
    }
    public void setCustomstage(String customstage) {
    	this.customstage = customstage;
    }
    
    public String getTotalconsumption() {
    	return totalconsumption;
    }
    public void setTotalconsumption(String totalconsumption) {
    	this.totalconsumption = totalconsumption;
    }
    
    public String getAmountsowed() {
    	return amountsowed;
    }
    public void setAmountsowed(String amountsowed) {
    	this.amountsowed = amountsowed;
    }
    
    public String getMaintenancecosts() {
    	return maintenancecosts;
    }
    public void setMaintenancecosts(String maintenancecosts) {
    	this.maintenancecosts = maintenancecosts;
    }
    
    public String getBusiness() {
    	return business;
    }
    public void setBusiness(String business) {
    	this.business = business;
    }
	
}

Dao层

@Repository
public interface CustomerMassDao {
	
	List<CustomerMass> queryExcelInfo();
}

mapper.xml

<select id="queryExcelInfo" resultType="CustomerMass">
		select * from customer_mass 
	</select>

service

public List<CustomerMass> queryExcelInfo(){
		return customerMassDao.queryExcelInfo();
	}

展示

  • 9
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
SSM(Spring + SpringMVC + MyBatis)框架可以很方便地实现Excel文件的导入和导出。下面是一个简单的实现流程: 1. 导出Excel文件 - 添加POI依赖,例如: ``` <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> ``` - 创建一个Excel文件,例如: ``` Workbook workbook = new HSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("Hello, world!"); ``` - 将Excel文件写入输出流,例如: ``` response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=test.xls"); OutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.flush(); outputStream.close(); ``` 2. 导入Excel文件 - 添加POI依赖,例如: ``` <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> ``` - 读取Excel文件,例如: ``` InputStream inputStream = new FileInputStream("test.xls"); Workbook workbook = new HSSFWorkbook(inputStream); Sheet sheet = workbook.getSheetAt(0); Row row = sheet.getRow(0); Cell cell = row.getCell(0); System.out.println(cell.getStringCellValue()); ``` - 将Excel数据保存到数据库,例如: ``` @Autowired private UserService userService; for (int i = 1; i <= sheet.getLastRowNum(); i++) { row = sheet.getRow(i); User user = new User(); user.setName(row.getCell(0).getStringCellValue()); user.setAge((int) row.getCell(1).getNumericCellValue()); userService.saveUser(user); } ``` 以上是一个简单的实现流程,具体实现方式可以根据实际需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

l去留无心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值