package com.beacon.sms.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.struts2.ServletActionContext;
import com.beacon.sms.bean.Address;
import com.beacon.sms.bean.Class;
import com.beacon.sms.bean.Course;
import com.beacon.sms.bean.Score;
import com.beacon.sms.bean.Semester;
import com.beacon.sms.bean.Student;
import com.beacon.sms.bean.Teacher;
import com.beacon.sms.bean.Teaching;
/**
*
* 作者:beacon 创建日期:2017年11月8日下午4:21:49 描述:处理Excel的工具类
*/
public class ExcelUtil {
private String[] headers = { "学年学期", "学号", "姓名", "所在班级", "课程名称", "成绩" };
public List<Student> readStudentExcel(InputStream inputStream) throws IOException {
List<Student> list = new ArrayList<Student>();
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(inputStream);
Sheet sheet = hssfWorkbook.getSheetAt(0);
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
Student student = new Student();
student.setStudentName(row.getCell(0).getStringCellValue());
student.setSex(row.getCell(1).getStringCellValue());
Address address = new Address();
address.setProvince(row.getCell(2).getStringCellValue());
address.setCity(row.getCell(3).getStringCellValue());
address.setCounty(row.getCell(4).getStringCellValue());
address.setDetailedAddress(row.getCell(5).getStringCellValue());
student.setAddress(address);
student.setBirthday(row.getCell(6).getStringCellValue());
student.setOrigin(row.getCell(7).getStringCellValue());
Cell cell = row.getCell(8);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
String content = cell.getStringCellValue();
student.setCellphone(content);
cell = row.getCell(9);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
content = cell.getStringCellValue();
student.setIdCard(content);
student.setPolitics(row.getCell(10).getStringCellValue());
list.add(student);
}
return list;
}
public List<Teacher> readTeacherExcel(InputStream inputStream) throws IOException {
List<Teacher> list = new ArrayList<Teacher>();
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(inputStream);
Sheet sheet = hssfWorkbook.getSheetAt(0);
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
Teacher teacher = new Teacher();
teacher.setTeacherName(row.getCell(0).getStringCellValue());
Cell cell = row.getCell(1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
String content = cell.getStringCellValue();
teacher.setCellphone(content);
teacher.setSex(row.getCell(2).getStringCellValue());
teacher.setBirthday(row.getCell(3).getStringCellValue());
teacher.setEducation(row.getCell(4).getStringCellValue());
cell = row.getCell(5);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
content = cell.getStringCellValue();
teacher.setIdCard(content);
Address address = new Address();
address.setProvince(row.getCell(6).getStringCellValue());
address.setCity(row.getCell(7).getStringCellValue());
address.setCounty(row.getCell(8).getStringCellValue());
address.setDetailedAddress(row.getCell(9).getStringCellValue());
teacher.setAddress(address);
list.add(teacher);
}
return list;
}
public void exportExcel(List<Score> list) throws IOException {
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
Sheet sheet = hssfWorkbook.createSheet("学生成绩表");
Row header = sheet.createRow(0);
for (int i = 0; i < headers.length; i++) {
header.createCell(i).setCellValue(headers[i]);
}
for (int i = 0; i < list.size(); i++) {
Row row = sheet.createRow(i + 1);
Score score = list.get(i);
Teaching teaching = score.getTeaching();
Student student = score.getStudent();
int fenshu = score.getScore();
Semester semester = teaching.getSemester();
Class class1 = student.getClass1();
Course course = teaching.getCourse();
row.createCell(0).setCellValue(semester.getSemesterName());
row.createCell(1).setCellValue(student.getStudentNo());
row.createCell(2).setCellValue(student.getStudentName());
row.createCell(3).setCellValue(class1.getClassName());
row.createCell(4).setCellValue(course.getCourseName());
row.createCell(5).setCellValue(fenshu);
}
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=score.xls");
OutputStream ouputStream = response.getOutputStream();
hssfWorkbook.write(ouputStream);
ouputStream.flush();
ouputStream.close();
}
public InputStream getExcelInputStream(List<Score> list) throws IOException {
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
Sheet sheet = hssfWorkbook.createSheet("学生成绩表");
Row header = sheet.createRow(0);
for (int i = 0; i < headers.length; i++) {
header.createCell(i).setCellValue(headers[i]);
}
for (int i = 0; i < list.size(); i++) {
Row row = sheet.createRow(i + 1);
Score score = list.get(i);
Teaching teaching = score.getTeaching();
Student student = score.getStudent();
int fenshu = score.getScore();
Semester semester = teaching.getSemester();
Class class1 = student.getClass1();
Course course = teaching.getCourse();
row.createCell(0).setCellValue(semester.getSemesterName());
row.createCell(1).setCellValue(student.getStudentNo());
row.createCell(2).setCellValue(student.getStudentName());
row.createCell(3).setCellValue(class1.getClassName());
row.createCell(4).setCellValue(course.getCourseName());
row.createCell(5).setCellValue(fenshu);
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
hssfWorkbook.write(byteArrayOutputStream);
byte[] buffer = byteArrayOutputStream.toByteArray();
ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(buffer);
return byteArrayInputStream;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ssm物流管理系统.rar (294个子文件)
ExcelUtil.class 7KB
UpLoadAction.class 7KB
LoginAction.class 6KB
TeacherAction.class 5KB
TeachingAction.class 5KB
StudentServiceImpl.class 5KB
StudentAction.class 5KB
TeacherServiceImpl.class 5KB
CourseAction.class 4KB
ScoreAction.class 4KB
TeachingServiceImpl.class 4KB
Student.class 4KB
ValidateCodeUtil.class 4KB
Teacher.class 3KB
MajorServiceImpl.class 3KB
ScoreSearchBean.class 3KB
TeachingSearchBean.class 2KB
MajorAction.class 2KB
Class.class 2KB
CourseServiceImpl.class 2KB
Teaching.class 2KB
LoginServiceImpl.class 2KB
Score.class 2KB
Address.class 2KB
Course.class 2KB
ValidateCodeServlet.class 2KB
TeacherSearchBean.class 2KB
DatagridBean.class 1KB
SemesterAction.class 1KB
EncodingFilter.class 1KB
Major.class 1KB
ScoreServiceImpl.class 1KB
StudentSearchBean.class 1KB
Admin.class 1KB
PageBean.class 1KB
Semester.class 1KB
Department.class 1KB
JsonUtil.class 994B
CourseSearchBean.class 917B
SemesterServiceImpl.class 911B
TeacherService.class 873B
TeacherDao.class 869B
StudentDao.class 839B
DateUtil.class 697B
TeachingService.class 688B
StringUtil.class 687B
StudentService.class 673B
CourseService.class 655B
CourseDao.class 594B
TeachingDao.class 591B
ScoreDao.class 511B
MajorDao.class 483B
ScoreService.class 441B
LoginDao.class 431B
LoginService.class 427B
MajorService.class 285B
SemesterService.class 254B
SemesterDao.class 242B
.classpath 817B
org.eclipse.wst.common.component 458B
org.eclipse.wst.jsdt.ui.superType.container 49B
bootstrap.css 143KB
bootstrap.min.css 118KB
easyui.css 65KB
bootstrap-theme.css 26KB
bootstrap-theme.min.css 23KB
icon.css 3KB
main.css 459B
glyphicons-halflings-regular.eot 20KB
loading.gif 2KB
blank.gif 43B
blank.gif 43B
poi-3.9.jar 1.78MB
aspectjweaver-1.6.8.jar 1.55MB
mybatis-3.4.4.jar 1.53MB
mysql-connector-java-5.1.38.jar 961KB
freemarker-2.3.19.jar 909KB
struts2-core-2.3.16.3.jar 782KB
spring-context-3.0.5.RELEASE.jar 653KB
xwork-core-2.3.16.3.jar 639KB
javassist-3.11.0.GA.jar 600KB
spring-beans-3.0.5.RELEASE.jar 542KB
fastjson-1.2.31.jar 452KB
spring-jdbc-4.3.9.RELEASE.jar 418KB
spring-web-3.0.5.RELEASE.jar 386KB
spring-core-3.0.5.RELEASE.jar 373KB
spring-aop-3.1.0.RELEASE.jar 324KB
commons-lang3-3.1.jar 308KB
spring-tx-3.1.0.RELEASE.jar 240KB
ognl-3.0.6.jar 223KB
commons-io-2.2.jar 170KB
spring-expression-3.0.5.RELEASE.jar 166KB
javax.servlet-api-3.1.0.jar 94KB
commons-codec-1.5.jar 71KB
commons-fileupload-1.3.1.jar 67KB
commons-logging-1.1.1.jar 59KB
mybatis-spring-1.3.0.jar 52KB
spring-asm-3.0.5.RELEASE.jar 52KB
asm-3.3.jar 43KB
asm-commons-3.3.jar 37KB
共 294 条
- 1
- 2
- 3
资源评论
一枚渣渣程序猿
- 粉丝: 50
- 资源: 328
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- STC15单片机串口2使用程序例子
- 读取日志的excel生成周报 用python3开发weekplan-master.zip
- python 读取excel数据导入dbimport-data-master.zip
- K折交叉验证BP神经网络,多输入多输出BP神经网络(代码完整,数据齐全)
- B07训练原图.zip
- python-对Excel数据处理做可视化分析.zip
- 人工智能大作业-无人机图像目标检测的python源代码+文档说明.zip
- 基于GoogLeNet实现Cifar-10图像分类项目python源码(高分项目).zip
- 数据库 sql 面试题目及答案解析.docx
- 汽车常见 10 种传感器故障后的表现与解决措施.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功