Controller层
package com.southgis.imap.maintain.controller;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.southgis.imap.maintain.service.sendMessage;
@RestController
public class ExcelController{
@Autowired
private sendMessage sendmessage;
@RequestMapping("/Excel")
public Object excelFind(HttpServletResponse response) {
XSSFWorkbook wb =this.sendmessage.show();
String fileName = "Goods报表.xlsx";
OutputStream outputStream =null;
try {
fileName = URLEncoder.encode(fileName,"UTF-8");
//设置ContentType请求信息格式
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
outputStream = response.getOutputStream();
wb.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
}
Service层
(1)接口(sendMessage.java)
package com.southgis.imap.maintain.service;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public interface sendMessage {
XSSFWorkbook show();
}
(2)实现类(SendMessageImpl.java)
package com.southgis.imap.maintain.service.impl;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.southgis.imap.maintain.entity.SendMessage;
import com.southgis.imap.maintain.mapper.SendMessageMapper;
import com.southgis.imap.maintain.service.sendMessage;
@Service
@Transactional
public class SendMessageImpl implements sendMessage {
@Autowired
private SendMessageMapper sendMessageMapper;
@Override
public XSSFWorkbook show() {
// 查出数据库数据(这里用Mybatis实现)
List list = sendMessageMapper.findAll();// 查出数据库数据
XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Goods");// 创建一张表
Row titleRow = sheet.createRow(0);// 创建第一行,起始为0
titleRow.createCell(0).setCellValue("smid");// 第一列
titleRow.createCell(1).setCellValue("company");
titleRow.createCell(2).setCellValue("org");
titleRow.createCell(3).setCellValue("dest");
titleRow.createCell(4).setCellValue("senddata");
int cell = 1;
for (SendMessage goods : list) {
Row row = sheet.createRow(cell);// 从第二行开始保存数据
row.createCell(0).setCellValue(goods.getSmid());
row.createCell(1).setCellValue(goods.getCompany());// 将数据库的数据遍历出来
row.createCell(2).setCellValue(goods.getOrg());
row.createCell(3).setCellValue(goods.getDest());
row.createCell(4).setCellValue(goods.getSenddata());
cell++;
}
return wb;
}
}
数据库数据读取(Mybatis实现)
(1)接口(SendMessageMapper.java)
package com.southgis.imap.maintain.mapper;
import java.util.Date;
import java.util.List;
import com.southgis.imap.maintain.entity.SendMessage;
public interface SendMessageMapper {
//查询数据库所有字段
List findAll();
}
(2)Mybatis(SendMessageMapper.xml)
对应实体类(SendMessage.java)
package com.southgis.imap.maintain.entity;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
@Data
@Entity(name="sendmessage")
public class SendMessage {
//主键
@Id
@GeneratedValue(generator="id")
@GenericGenerator(name="id",strategy="native")
private Integer id;
private String smid;
//所属公司
private String company;
private String org;
//是否发送成功
private String dest;
private String senddata;
//创建时间
@DateTimeFormat(pattern="yyyy-MM-dd")
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
private Date insertDate;
//是否发送成功?success or fail
private Integer status;
private String msgGroup;
private String reportStatus;
private String errorCode;
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date receiveDate;
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date sendDate;
}
主类(Application.java)
package com.southgis.imap.maintain;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.southgis.imap.maintain.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
System.out.println("(♥◠‿◠)ノ゙ SouthProject启动成功 ლ(´ڡ`ლ)゙ \\n");
}
}
相应pom依赖(pom.xml)
4.0.0
southtechnogy
imap-southtechnogy2
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
org.springframework.boot
spring-boot-maven-plugin
true
true
org.apache.maven.plugins
maven-surefire-plugin
true
false
org.apache.maven.plugins
maven-compiler-plugin
1.8
maven-resources-plugin
copy-yml
validate
copy-resources
${project.build.directory}
src/main/resources
application.yml
org.apache.poi
poi
3.15
org.apache.poi
poi-ooxml-schemas
3.15
org.apache.poi
poi-ooxml
3.15
com.alibaba
fastjson
1.2.7
net.sourceforge.jexcelapi
jxl
2.6.10
junit
junit
4.12
org.springframework.boot
spring-boot-starter-test
test
2.0.2.RELEASE
org.springframework
spring-beans
5.0.5.RELEASE
compile
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-cache
net.sf.ehcache
ehcache
org.springframework.boot
spring-boot-starter-data-jpa
com.alibaba
druid
1.0.9
mysql
mysql-connector-java
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
org.springframework.boot
spring-boot-starter-thymeleaf
javax.servlet
jstl
org.apache.tomcat.embed
tomcat-embed-jasper
provided
org.apache.commons
commons-lang3
org.projectlombok
lombok
provided
com.alibaba
fastjson
1.2.32
com.google.guava
guava
org.springframework.boot
spring-boot-starter-aop
-->
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
org.apache.tomcat.embed
tomcat-embed-jasper
provided
com.h2database
h2
test
org.springframework.cloud
spring-cloud-dependencies
Finchley.M9
pom
import
spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone
false
releases
infra realease
http://172.16.50.180:8085/nexus/content/repositories/releases/
snapshots
infra snapshotRepository
http://112.94.224.249:8085/nexus/content/repositories/snapshots/
相关配置文件(application.yml)
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/build?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: 112233
initialize: true
init-db: true
mybatis:
type-aliases-package: com.southgis.imap.maintain.entity
configuration:
map-underscore-to-camel-case: true