SSM框架使用POI导出EXCEL
2017-09-28 15:32
363 查看
Java使用poi组件导出excel报表,poi能很好的实现输出excel各种功能,本文主要介绍SSM框架使用poi导出excel功能实现案例(成绩查询明细)。
poi-3.6-20091214.jar下载地址:http://download.csdn.net/download/astrologer_/9999906
jsp代码:
Controller层:
@Controller
@RequestMapping(value = "/score")
public class ScoreController{
@Autowired
IScoreService scoreService;
@RequestMapping("/exportExcel")
public void exportExcel(HttpServletRequest request, HttpServletResponse response) {
List<ScoreInfo> excelList = null;
//判断学院代码是否存在
String schoolCode = null;
Map<String, String> parameters = ViewUtil.convertParameterMAP(request.getParameterMap());
if (parameters != null && parameters.containsKey("schoolCode")) {
schoolCode = parameters.get("schoolCode");
}
// 查询学生成绩信息
if (null == schoolCode || "".equals(schoolCode)) {
excelList = new ArrayList<ScoreInfo>();
} else {
//查询成绩信息
excelList = scoreInfoService.getScoreInfoList(parameters);
}
// 创建HSSFWorkbook 对象,用于将excel输出到输出流
HSSFWorkbook excel = new HSSFWorkbook();
// 创建table工作薄
HSSFSheet sheet = excel.createSheet("学生成绩情况明细");
// 创建表格行
HSSFRow row = sheet.createRow(0);
// 单元格样式
HSSFCellStyle style = excel.createCellStyle();
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直
//style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCell cell;
// 设置表头内容
String[] tableHead = {"学院名称","专业名称","科目名称","参考人数","通过数量","通过率"};
for(int iHead=0; iHead<tableHead.length; iHead++) {
cell =row.createCell(iHead);
cell.setCellValue(tableHead[iHead]);
cell.setCellStyle(style);;
}
// 数字格式化
DecimalFormat df = new DecimalFormat("0.00");
// 设置表格内容
for(int iBody=0; iBody<excelList.size(); iBody++) {
row = sheet.createRow(iBody+1);
ScoreInfo scoreInfo = excelList.get(iBody);
String[] scoreInfoArray = new String[6];
scoreInfoArray[0]=scoreInfo.getSchool();
scoreInfoArray[1]=scoreInfo.getMajor();
scoreInfoArray[2]=scoreInfo.getSubject();
scoreInfoArray[3]=scoreInfo.getStuNum();
scoreInfoArray[4]=scoreInfo.getPassNum();
// 设置通过率格式
scoreInfoArray[5]=df.format(Double.valueOf(scoreInfo.getPassRate()));
for(int iArray=0; iArray<sjclqkmxArray.length; iArray++) {
row.createCell(iArray).setCellValue(scoreInfoArray[iArray]);
}
}
//设置表格的行高和列宽
for(int iHigh=0; iHigh<=excelList.size(); iHigh++) {
row = sheet.getRow(iHigh);
row.setHeightInPoints(20);
}
for(int iWidth=0; iWidth<tableHead.length; iWidth++) {
sheet.setColumnWidth(iWidth, 4000);
}
// 合并单元格(学院和专业)
int firstRow=1;
int lastRow=1;
int firstMajorRow=1;
int lastMajorRow=1;
for(int iRow=1; iRow<excelList.size(); iRow++) {
if(excelList.get(iRow-1).getSchoolCode().equals(excelList.get(iRow).getSchoolCode())) {
lastRow=iRow+1;
if(excelList.get(iRow-1).getMajorCode().equals(excelList.get(iRow).getMajorCode())) {
lastMajorRow=iRow+1;
}else {
if(firstMajorRow!=lastMajorRow) {
CellRangeAddress region = new CellRangeAddress(firstMajorRow, lastMajorRow, (short) 1, (short) 1);
sheet.addMergedRegion(region);
HSSFCell nowCell = sheet.getRow(firstMajorRow).getCell(1);
nowCell.setCellStyle(style);
}
firstMajorRow=lastMajorRow+1;
lastMajorRow=lastMajorRow+1;
}
}else {
if(firstMajorRow!=lastMajorRow) {
CellRangeAddress region1 = new CellRangeAddress(firstMajorRow, lastMajorRow, (short) 1, (short) 1);
sheet.addMergedRegion(region1);
HSSFCell nowCell1 = sheet.getRow(firstMajorRow).getCell(1);
nowCell1.setCellStyle(style);
}
CellRangeAddress region2 = new CellRangeAddress(firstRow, lastRow, (short) 0, (short) 0);
sheet.addMergedRegion(region2);
HSSFCell nowCell2 = sheet.getRow(firstRow).getCell(0);
nowCell2.setCellStyle(style);
firstRow=lastRow+1
4000
;
lastRow=firstRow;
firstMajorRow=firstRow;
lastMajorRow=firstRow;
}
}
if(firstMajorRow!=lastMajorRow) {
CellRangeAddress region3 = new CellRangeAddress(firstMajorRow, lastMajorRow, (short) 1, (short) 1);
sheet.addMergedRegion(region3);
HSSFCell nowCell3 = sheet.getRow(firstMajorRow).getCell(1);
nowCell3.setCellStyle(style);
}
if(firstRow!=lastRow) {
CellRangeAddress region4 = new CellRangeAddress(firstRow, lastRow, (short) 0, (short) 0);
sheet.addMergedRegion(region4);
HSSFCell nowCell4 = sheet.getRow(firstRow).getCell(0);
nowCell4.setCellStyle(style);
}
// 设置文件名
String fileName ="学生成绩情况明细";
try {
// 捕获内存缓冲区的数据,转换成字节数组
ByteArrayOutputStream out = new ByteArrayOutputStream();
excel.write(out);
// 获取内存缓冲中的数据
byte[] content = out.toByteArray();
// 将字节数组转化为输入流
InputStream in = new ByteArrayInputStream(content);
//通过调用reset()方法可以重新定位。
response.reset();
// 如果文件名是英文名不需要加编码格式,如果是中文名需要添加"iso-8859-1"防止乱码
response.setHeader("Content-Disposition", "attachment; filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
response.addHeader("Content-Length", "" + content.length);
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
ServletOutputStream outputStream = response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(in);
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
byte[] buff = new byte[8192];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
poi-3.6-20091214.jar下载地址:http://download.csdn.net/download/astrologer_/9999906
jsp代码:
<input id="export" type="button" value="导出EXCEL" onclick = "exportExcel()"/>js代码:
function exportExcel(){ if("" == schoolCode || undefined == schoolCode){ $.dialog({ type : 'alert', content : "查询必须选择学院!" }); return; } var url = context +"/service/score/exportExcel?schoolCode="+schoolCode+"&majorCode="+majorCode; // 这里一定不能使用Ajax请求 window.open(url); }模型层的实体bean:
public class ScoreInfo { @Column(name="school_code") private String schoolCode; @Column(name="school") private String school; @Column(name="major_code") private String majorCode; @Column(name="major") private String major; @Column(name="subject_code") private String subjectCode; @Column(name="subject") private String subject; @Column(name="stu_num") private String stuNum; @Column(name="pass_num") private String passNum; @Column(name="pass_rate") private String passRate; public String getSchoolCode() { return schoolCode; } public void setSchoolCode(String schoolCode) { this.schoolCode = schoolCode; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } public String getMajorCode() { return majorCode; } public void setMajorCode(String majorCode) { this.majorCode = majorCode; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public String getSubjectCode() { return subjectCode; } public void setSubjectCode(String subjectCode) { this.subjectCode = subjectCode; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getStuNum() { return stuNum; } public void setStuNum(String stuNum) { this.stuNum = stuNum; } public String getPassNum() { return passNum; } public void setPassNum(String passNum) { this.passNum = passNum; } public String getPassRate() { return passRate; } public void setPassRate(String passRate) { this.passRate = passRate; } }Service层接口:
public interface IScoreInfoService { public List<ScoreInfo> getScoreInfoList(Map<String, String> parameters); }Service层实现类:
@Service("scoreInfoService") public class ScoreInfoServiceImpl implements IScoreInfoService { @Autowired private ScoreInfoMapper scoreInfoMapper; @Override public List<ScoreInfo> getScoreInfoList(Map<String, String> map) { return scoreInfoMapper.getScoreInfoList(map); } }Dao层ScoreInfoMapper接口:
public interface ScoreInfoMapper extends EntityMapper<ScoreInfo> { public List<ScoreInfo> getScoreInfoList(Map<String, String> map); }Dao层ScoreInfoMapper.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.inspur.tax.sjcjgl.dsf.sjcjqkhz.dao.SjcjqkhzMapper"> <resultMap type="com.csdn.score.data.ScoreInfo" id="scoreInfoResultMap"> </resultMap> <select id="getScoreInfoList" resultMap="scoreInfoResultMap" parameterType="java.util.Map"> <!-- SQL语句根据实际情况拼接 --> SELECT * FROM SCORE_INFO </select> </mapper>
Controller层:
@Controller
@RequestMapping(value = "/score")
public class ScoreController{
@Autowired
IScoreService scoreService;
@RequestMapping("/exportExcel")
public void exportExcel(HttpServletRequest request, HttpServletResponse response) {
List<ScoreInfo> excelList = null;
//判断学院代码是否存在
String schoolCode = null;
Map<String, String> parameters = ViewUtil.convertParameterMAP(request.getParameterMap());
if (parameters != null && parameters.containsKey("schoolCode")) {
schoolCode = parameters.get("schoolCode");
}
// 查询学生成绩信息
if (null == schoolCode || "".equals(schoolCode)) {
excelList = new ArrayList<ScoreInfo>();
} else {
//查询成绩信息
excelList = scoreInfoService.getScoreInfoList(parameters);
}
// 创建HSSFWorkbook 对象,用于将excel输出到输出流
HSSFWorkbook excel = new HSSFWorkbook();
// 创建table工作薄
HSSFSheet sheet = excel.createSheet("学生成绩情况明细");
// 创建表格行
HSSFRow row = sheet.createRow(0);
// 单元格样式
HSSFCellStyle style = excel.createCellStyle();
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直
//style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCell cell;
// 设置表头内容
String[] tableHead = {"学院名称","专业名称","科目名称","参考人数","通过数量","通过率"};
for(int iHead=0; iHead<tableHead.length; iHead++) {
cell =row.createCell(iHead);
cell.setCellValue(tableHead[iHead]);
cell.setCellStyle(style);;
}
// 数字格式化
DecimalFormat df = new DecimalFormat("0.00");
// 设置表格内容
for(int iBody=0; iBody<excelList.size(); iBody++) {
row = sheet.createRow(iBody+1);
ScoreInfo scoreInfo = excelList.get(iBody);
String[] scoreInfoArray = new String[6];
scoreInfoArray[0]=scoreInfo.getSchool();
scoreInfoArray[1]=scoreInfo.getMajor();
scoreInfoArray[2]=scoreInfo.getSubject();
scoreInfoArray[3]=scoreInfo.getStuNum();
scoreInfoArray[4]=scoreInfo.getPassNum();
// 设置通过率格式
scoreInfoArray[5]=df.format(Double.valueOf(scoreInfo.getPassRate()));
for(int iArray=0; iArray<sjclqkmxArray.length; iArray++) {
row.createCell(iArray).setCellValue(scoreInfoArray[iArray]);
}
}
//设置表格的行高和列宽
for(int iHigh=0; iHigh<=excelList.size(); iHigh++) {
row = sheet.getRow(iHigh);
row.setHeightInPoints(20);
}
for(int iWidth=0; iWidth<tableHead.length; iWidth++) {
sheet.setColumnWidth(iWidth, 4000);
}
// 合并单元格(学院和专业)
int firstRow=1;
int lastRow=1;
int firstMajorRow=1;
int lastMajorRow=1;
for(int iRow=1; iRow<excelList.size(); iRow++) {
if(excelList.get(iRow-1).getSchoolCode().equals(excelList.get(iRow).getSchoolCode())) {
lastRow=iRow+1;
if(excelList.get(iRow-1).getMajorCode().equals(excelList.get(iRow).getMajorCode())) {
lastMajorRow=iRow+1;
}else {
if(firstMajorRow!=lastMajorRow) {
CellRangeAddress region = new CellRangeAddress(firstMajorRow, lastMajorRow, (short) 1, (short) 1);
sheet.addMergedRegion(region);
HSSFCell nowCell = sheet.getRow(firstMajorRow).getCell(1);
nowCell.setCellStyle(style);
}
firstMajorRow=lastMajorRow+1;
lastMajorRow=lastMajorRow+1;
}
}else {
if(firstMajorRow!=lastMajorRow) {
CellRangeAddress region1 = new CellRangeAddress(firstMajorRow, lastMajorRow, (short) 1, (short) 1);
sheet.addMergedRegion(region1);
HSSFCell nowCell1 = sheet.getRow(firstMajorRow).getCell(1);
nowCell1.setCellStyle(style);
}
CellRangeAddress region2 = new CellRangeAddress(firstRow, lastRow, (short) 0, (short) 0);
sheet.addMergedRegion(region2);
HSSFCell nowCell2 = sheet.getRow(firstRow).getCell(0);
nowCell2.setCellStyle(style);
firstRow=lastRow+1
4000
;
lastRow=firstRow;
firstMajorRow=firstRow;
lastMajorRow=firstRow;
}
}
if(firstMajorRow!=lastMajorRow) {
CellRangeAddress region3 = new CellRangeAddress(firstMajorRow, lastMajorRow, (short) 1, (short) 1);
sheet.addMergedRegion(region3);
HSSFCell nowCell3 = sheet.getRow(firstMajorRow).getCell(1);
nowCell3.setCellStyle(style);
}
if(firstRow!=lastRow) {
CellRangeAddress region4 = new CellRangeAddress(firstRow, lastRow, (short) 0, (short) 0);
sheet.addMergedRegion(region4);
HSSFCell nowCell4 = sheet.getRow(firstRow).getCell(0);
nowCell4.setCellStyle(style);
}
// 设置文件名
String fileName ="学生成绩情况明细";
try {
// 捕获内存缓冲区的数据,转换成字节数组
ByteArrayOutputStream out = new ByteArrayOutputStream();
excel.write(out);
// 获取内存缓冲中的数据
byte[] content = out.toByteArray();
// 将字节数组转化为输入流
InputStream in = new ByteArrayInputStream(content);
//通过调用reset()方法可以重新定位。
response.reset();
// 如果文件名是英文名不需要加编码格式,如果是中文名需要添加"iso-8859-1"防止乱码
response.setHeader("Content-Disposition", "attachment; filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
response.addHeader("Content-Length", "" + content.length);
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
ServletOutputStream outputStream = response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(in);
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
byte[] buff = new byte[8192];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
相关文章推荐
- ssm框架使用poi实现数据导出到excel,并弹框下载
- SSM框架使用POI技术导出Excel表
- SSM框架使用POI技术导出Excel表
- SSM框架,使用POI技术实现简单导出Excel表
- J2EE项目系列(四)--SSM框架构建积分系统和基本商品检索系统(Spring+SpringMVC+MyBatis)(3)Ajax使用详解(级联列表)以及企业级报表Excel导入导出实现
- 在SSM下使用POI实现Excel表的导入/导出
- SSM框架使用POI技术导出Excel表
- 在SSM下使用POI实现Excel表的导入/导出
- 在SSM下使用POI实现Excel表的导入/导出
- 使用工具类 使用poi导入导出excel报表
- 使用struts2和poi导出excel文档
- springSSM 使用poi导出excel(一)
- 使用POI实现Excel导入与导出
- JAVA使用POI根据模版导出EXCEL
- JAVA使用POI根据模版导出EXCEL
- 使用POI插件,提取导出excel的工具类
- Yii框架中使用PHPExcel导出Excel文件
- 使用apache的poi实现导入导出excel
- SpringMvc 使用poi导入导出Excel