4.0.0
cn.zj
DownloadExcel1
war
0.0.1-SNAPSHOT
DownloadExcel1 Maven Webapp
http://maven.apache.org
junit
junit
3.8.1
test
org.springframework
org.springframework.jdbc
3.1.2.RELEASE
c3p0
c3p0
0.9.1.2
org.springframework
spring-context
3.2.4.RELEASE
jar
org.springframework
spring-core
3.1.2.RELEASE
jar
org.springframework
spring-beans
3.2.4.RELEASE
jar
org.springframework
spring-webmvc
3.2.4.RELEASE
jar
org.springframework
spring-orm
3.2.4.RELEASE
jar
org.hibernate
hibernate-validator
5.1.3.Final
org.hibernate
hibernate-core
4.2.16.Final
jar
org.hibernate
hibernate-ehcache
4.3.8.Final
mysql
mysql-connector-java
5.1.34
commons-dbcp
commons-dbcp
1.4
jstl
jstl
1.2
log4j
log4j
1.2.17
commons-fileupload
commons-fileupload
1.2.1
commons-io
commons-io
1.4
junit
junit
3.8.1
test
javax.validation
validation-api
1.1.0.Final
org.apache.ant
ant
1.7.0
org.apache.poi
poi
3.14
DownloadExcel1
db.properties
连接数据库的配置
jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test?useUnicode\=true&characterEncoding\=utf-8
jdbc.removeAbandoned=true
jdbc.maxIdleTime=2S
Spring-JDBC.xml
10
100
10
Spring.xml
新建包与方法(包名,类名可以根据自己习惯修改):
JDBC.java代码(定义操作据库的接口,这里就只有一个查询的接口)
package cn.zj.downloadExcel.dao;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import cn.zj.downloadExcel.GradeExcelData;
@Repository("JDBC")
public interface JDBC {
public List query();
}
JDBCImpl.java代码(操作数据库接口的实现)
package cn.zj.downloadExcel.daoimpl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import cn.zj.downloadExcel.GradeExcelData;
import cn.zj.downloadExcel.dao.JDBC;
/**
* 操作数据库的实现
* @author Administrator
*
*/
@Repository("JDBCImpl")
public class JDBCImpl implements JDBC {
@Autowired
private JdbcTemplate jdbcTemplateObject;
@Override
public List query() {
// TODO Auto-generated method stub
String sql = "select * from user";
RowMapper rowMapper = new BeanPropertyRowMapper<>(GradeExcelData.class);
List employee = jdbcTemplateObject.query(sql,rowMapper);
System.out.println("获得数据:"+employee);
return employee;
}
}
DownloadExcel.java(页面点击下载后访问的类,楼主没写前台代码,跑起来之后可以直接访问DownloadExcel.action直接下载)
package cn.zj.downloadExcel;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 界面访问的类
* @author Administrator
*
*/
@Controller
public class DownloadExcel {
@RequestMapping("/DownloadExcel")
@ResponseBody
public void DownloadExcel(HttpServletResponse response,HttpServletRequest request) throws IOException{
service service = new service();
String eId = "0";
service.export(eId, response);
}
}
service.java方法
package cn.zj.downloadExcel;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* Service方法
* @author Administrator
*
*/
public class service {
public boolean export(String eId,HttpServletResponse response){
try {
List list = cardDao.getExcelData(eId);
String[] fields = {"学号","姓名","得分","是否通过"};
ExcelExport export = new ExcelExport();
HSSFWorkbook wb = export.generateExcel();
wb = export.generateSheet(wb, "学生成绩单", fields, list);
export.export(wb, response);
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
cardDao.java方法
package cn.zj.downloadExcel;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.zj.downloadExcel.dao.JDBC;
import cn.zj.downloadExcel.daoimpl.JDBCImpl;
/**
* 连接数据库
* 调用操作数据库的类
* @author Administrator
*
*/
public class cardDao {
@Autowired
static public ApplicationContext context =
new ClassPathXmlApplicationContext("Spring-JDBC.xml");
@Autowired
static public JDBCImpl A = (JDBCImpl) context.getBean("JDBCImpl");
public static List getExcelData(String eId) {
List list = new ArrayList<>();
list = A.query();
// GradeExcelData gr = new GradeExcelData();
// gr.setName("曾杰");
// gr.setPassOrNot("小姑凉");
// gr.setScore(1);
// gr.setStuId("1");
// TODO Auto-generated method stub
// list.add(gr);
return list;
}
}
ExcelExport.java(根据获取的数据创建excel表格并返回给前台)
package cn.zj.downloadExcel;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* 创建excel表格并返回给前台
* @author Administrator
*
*/
public class ExcelExport {
// 第一步,创建一个webbook,对应一个Excel文件
public HSSFWorkbook generateExcel() {
return new HSSFWorkbook();
}
public HSSFWorkbook generateSheet(HSSFWorkbook wb, String sheetName, String[] fields, List list) {
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
//设置表头字段名
HSSFCell cell;
int m=0;
for(String fieldName:fields){
cell = row.createCell(m);
cell.setCellValue(fieldName);
cell.setCellStyle(style);
m++;
}
for (int i = 0; i < list.size(); i++)
{
row = sheet.createRow(i + 1);
GradeExcelData data = list.get(i);
// 第五步,创建单元格,并设置值
row.createCell(0).setCellValue(data.getStuId());
row.createCell(1).setCellValue(data.getName());
row.createCell(2).setCellValue(data.getScore());
row.createCell(3).setCellValue(data.getPassOrNot());
}
return wb;
}
public void export(HSSFWorkbook wb, HttpServletResponse response){
// 第六步,实现文件下载保存
try
{
response.setHeader("content-disposition", "attachment;filename="
+ URLEncoder.encode("学生成绩单", "utf-8") + ".xls");
OutputStream out = response.getOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
wb.write( baos);
byte[] xlsBytes = baos .toByteArray();
out.write( xlsBytes);
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
实体类:GradeExcelData.java(参数同数据库中参数一致)
package cn.zj.downloadExcel;
public class GradeExcelData {
private String stuId;
private String name;
private String passOrNot;
private int score;
public String getStuId() {
return stuId;
}
public void setStuId(String stuId) {
this.stuId = stuId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassOrNot() {
return passOrNot;
}
public void setPassOrNot(String passOrNot) {
this.passOrNot = passOrNot;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}