1、在pom.xml中添加Excel导入导出依赖
org.apache.poi
poi-ooxml
3.14-beta1
org.apache.poi
poi-ooxml-schemas
3.14-beta1
org.apache.poi
poi
3.14-beta1
org.apache.httpcomponents
httpclient
4.5.2
2、jsp使用
<%--导入--%>
function openImportWin() {
document.getElementById("ImportDiv").style.display = "block";
}
<%--导出--%>
function exportAllGrade() {
var CourseID = $("#ds_course").val();
window.location.href = "/excel/allExports?CourseID="+CourseID;
}
2、controller
package com.graduation.controller;
import com.graduation.bean.Exercise;
import com.graduation.bean.ExerciseCheck;
import com.graduation.service.ExcelService;
import com.graduation.service.ExerciseService;
import com.graduation.service.UserService;
import com.graduation.util.CosineSimilarAlgorithm;
import com.graduation.util.ExcelUtil;
import com.graduation.util.WordRead;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response;
import java.beans.IntrospectionException;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Excel控制器
* Created by ASUS on 2018/5/20
*
* @Authod K
*/
@Controller
@RequestMapping("/excel")
public class ExcelController {
@Autowired
private ExcelService excelService;
@Autowired
private UserService userService;
@Autowired
private ExerciseService exerciseService;
@ResponseBody
@RequestMapping("/exports")
public void export(@RequestParam(value = "CourseID",required = true)String CourseID,@RequestParam(value = "ChapterID",required = true) String ChapterID,@RequestParam(value = "MinChapterID",required = true) String MinChapterID,HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, ParseException, InvocationTargetException {
System.out.println(CourseID);
System.out.println("excel导出开始");
String salaryDate="1";
System.out.println("list:"+salaryDate);
if(salaryDate!=""){
response.reset(); //清除buffer缓存
Map map=new HashMap();
System.out.println("文件名:");
// 指定下载的文件名
response.setHeader("Content-Disposition", "attachment;filename=GradeList.xlsx");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
XSSFWorkbook workbook=null;
//导出Excel对象
workbook = excelService.exportExcelInfo(salaryDate,CourseID,ChapterID,MinChapterID);
System.out.println(workbook);
OutputStream output;
try {
System.out.println("创建成功!");
output = response.getOutputStream();
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
bufferedOutPut.flush();
workbook.write(output);
bufferedOutPut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@ResponseBody
@RequestMapping("/allExports")
public void allExports(@RequestParam(value = "CourseID",required = true)String CourseID,HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, ParseException, InvocationTargetException {
System.out.println(CourseID);
System.out.println("excel导出开始");
String salaryDate="1";
System.out.println("list:"+salaryDate);
if(salaryDate!=""){
response.reset(); //清除buffer缓存
Map map=new HashMap();
System.out.println("文件名:");
// 指定下载的文件名
response.setHeader("Content-Disposition", "attachment;filename=GradeList.xlsx");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
XSSFWorkbook workbook=null;
//导出Excel对象
workbook = excelService.exportAllExcelInfo(salaryDate,CourseID);
System.out.println(workbook);
OutputStream output;
try {
System.out.println("创建成功!");
output = response.getOutputStream();
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
bufferedOutPut.flush();
workbook.write(output);
bufferedOutPut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@ResponseBody
@RequestMapping("/ExcelImport")
public void ajaxUploadExcel(HttpServletRequest request,HttpServletResponse response) throws Exception {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
InputStream in =null;
List> listob = null;
MultipartFile file = multipartRequest.getFile("upfile");
if(file.isEmpty()){
throw new Exception("文件不存在!");
}
in = file.getInputStream();
listob = new ExcelUtil().getBankListByExcel(in,file.getOriginalFilename());
//该处可调用service相应方法进行数据保存到数据库中,现只对数据输出
for (int i = 0; i < listob.size(); i++) {
List
5、Service
package com.graduation.service;
import com.graduation.bean.ExerciseCheck;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.List;
/**
* Created by ASUS on 2018/5/20
*
* @Authod K
*/
public interface ExcelService {
/**
* 导出Excel表格
* @param salaryDate
* @return
* @throws InvocationTargetException
* @throws ClassNotFoundException
* @throws IntrospectionException
* @throws ParseException
* @throws IllegalAccessException
*/
XSSFWorkbook exportChapterExcelInfo(String salaryDate,String CourseID,String ChapterID) throws InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException;
XSSFWorkbook exportAllExcelInfo(String salaryDate,String CourseID) throws InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException;
XSSFWorkbook exportExcelInfo(String salaryDate,String CourseID,String ChapterID,String MinChapterID) throws InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException;
XSSFWorkbook CheckexportExcelInfo(String salaryDate, List list) throws InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException;
}
6、ServiceImpl
package com.graduation.service.impl;
import com.graduation.bean.ExcelBean;
import com.graduation.bean.ExcelClass;
import com.graduation.bean.ExcelExercise;
import com.graduation.bean.ExerciseCheck;
import com.graduation.service.ExcelService;
import com.graduation.service.ExerciseService;
import com.graduation.util.ExcelUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.swing.text.AbstractDocument;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Excel实现类
* Created by ASUS on 2018/5/20
*
* @Authod Grey Wolf
*/
@Service("ExcelService")
public class ExcelServiceImpl implements ExcelService {
@Autowired
private ExerciseService exerciseService;
@Override
public XSSFWorkbook exportChapterExcelInfo(String salaryDate, String CourseID, String ChapterID) throws InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {
List list = exerciseService.QueryChapterExcelExercise(CourseID,ChapterID);
List excel=new ArrayList<>();
Map> map=new ExcelClass().contentExcel();
XSSFWorkbook xssfWorkbook=null;
String sheetName = "成绩";
//调用ExcelUtil的方法
System.out.println("创建表格:"+sheetName);
xssfWorkbook = ExcelUtil.createExcelFile(ExcelExercise.class, list, map, sheetName);
return xssfWorkbook;
}
@Override
public XSSFWorkbook exportAllExcelInfo(String salaryDate, String CourseID) throws InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {
List list = exerciseService.QueryAllExcelExercise(CourseID);
List excel=new ArrayList<>();
Map> map=new ExcelClass().contentExcel();
XSSFWorkbook xssfWorkbook=null;
String sheetName = "成绩";
//调用ExcelUtil的方法
System.out.println("创建表格:"+sheetName);
xssfWorkbook = ExcelUtil.createExcelFile(ExcelExercise.class, list, map, sheetName);
return xssfWorkbook;
}
@Override
public XSSFWorkbook exportExcelInfo(String salaryDate,String CourseID,String ChapterID,String MinChapterID) throws InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {
//根据条件查询数据,把数据装载到一个list中
List list = exerciseService.QueryExcelExercise(CourseID,ChapterID,MinChapterID);
List excel=new ArrayList<>();
Map> map=new ExcelClass().contentExcel();
XSSFWorkbook xssfWorkbook=null;
String sheetName = "成绩";
//调用ExcelUtil的方法
System.out.println("创建表格:"+sheetName);
xssfWorkbook = ExcelUtil.createExcelFile(ExcelExercise.class, list, map, sheetName);
return xssfWorkbook;
}
@Override
public XSSFWorkbook CheckexportExcelInfo(String salaryDate, List list) throws InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {
List excel=new ArrayList<>();
Map> map=new ExcelClass().CheckcontentExcel();
XSSFWorkbook xssfWorkbook=null;
String sheetName = "疑似抄袭名单";
//调用ExcelUtil的方法
System.out.println("创建表格:"+sheetName);
xssfWorkbook = ExcelUtil.createExcelFile(ExerciseCheck.class, list, map, sheetName);
return xssfWorkbook;
}
}
7、ExcelUtil
package com.graduation.util;
import com.graduation.bean.ExcelBean;
import org.apache.http.client.utils.DateUtils;
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.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* Excel导入导出工具类
* Created by ASUS on 2018/5/20
*
* @Authod K
*/
public class ExcelUtil {
private final static String excel2003L =".xls"; //2003- 版本的excel
private final static String excel2007U =".xlsx"; //2007+ 版本的excel
/**
* Excel导入
*/
public static List> getBankListByExcel(InputStream in, String fileName) throws Exception{
List> list = null;
//创建Excel工作薄
Workbook work = getWorkbook(in,fileName);
if(null == work){
throw new Exception("创建Excel工作薄为空!");
}
Sheet sheet = null;
Row row = null;
Cell cell = null;
list = new ArrayList>();
//遍历Excel中所有的sheet
for (int i = 0; i < work.getNumberOfSheets(); i++) {
sheet = work.getSheetAt(i);
if(sheet==null){continue;}
//遍历当前sheet中的所有行
//包涵头部,所以要小于等于最后一列数,这里也可以在初始值加上头部行数,以便跳过头部
for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
//读取一行
row = sheet.getRow(j);
//去掉空行和表头
if(row==null||row.getFirstCellNum()==j){continue;}
//遍历所有的列
List