课程批量导入报错
package com.vincent.course.data;
import org.apache.commons.fileupload.FileItem;
public class ImportExcelParam {
private FileItem excel;
public FileItem getExcel() {
return excel;
}
public void setExcel(FileItem excel) {
this.excel = excel;
}
}
package com.vincent.course.data;
import java.util.List;
public class ImportExcelResult {
private List<Course> courseList;
public List<Course> getCourseList() {
return courseList;
}
public void setCourseList(List<Course> courseList) {
this.courseList = courseList;
}
}
package com.vincent.course.data;
import org.apache.commons.fileupload.FileItem;
import java.util.HashMap;
import java.util.Map;
public class ParamDto {
// 表单里的普通数据
private Map<String, String> paramMap;
// 表单里的文件数据
private Map<String, FileItem> fileMap;
public ParamDto() {
paramMap=new HashMap<>();
fileMap=new HashMap<>();
}
public Map<String, String> getParamMap() {
return paramMap;
}
public void setParamMap(Map<String, String> paramMap) {
this.paramMap = paramMap;
}
public Map<String, FileItem> getFileMap() {
return fileMap;
}
public void setFileMap(Map<String, FileItem> fileMap) {
this.fileMap = fileMap;
}
}
package com.vincent.course.service;
import com.vincent.course.data.Course;
import com.vincent.course.data.ImportExcelParam;
import com.vincent.course.data.ImportExcelResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ExcelService {
public ImportExcelResult imp(ImportExcelParam dto) {
ImportExcelResult result = new ImportExcelResult();
List<Course> courseList = new ArrayList<>();
result.setCourseList(courseList);
String fileName=null;
Workbook workbook=null;
try {
workbook = WorkbookFactory.create(dto.getExcel().getInputStream());
Sheet sheet = workbook.getSheetAt(0);
int rowNum = sheet.getLastRowNum();
for (int i = 1; i <= rowNum; i++) {
Row row = sheet.getRow(i);
Course course = new Course();
courseList.add(course);
course.setId(row.getCell(0).getStringCellValue());
course.setName(row.getCell(1).getStringCellValue());
course.setCourseDirection(row.getCell(2).getStringCellValue());
course.setDesc(row.getCell(3).getStringCellValue());
course.setDuration(row.getCell(4).getStringCellValue());
course.setOperator(row.getCell(5).getStringCellValue());
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("解析失败");
} finally {
if (workbook != null) {
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
public Workbook export(boolean isxlsx){
Workbook workbook;
if(isxlsx){
workbook=new XSSFWorkbook();
}else{
workbook=new HSSFWorkbook();
}
Sheet sheet = workbook.createSheet("My sheet");
List<List<String>> content=this.getContent();
for(int i=0;i<content.size();i++) {
Row row=sheet.createRow(i);
for(int j=0;j<content.get(i).size();j++) {
row.createCell(j).setCellValue(content.get(i).get(j));
}
}
return workbook;
}
private List<List<String>> getContent(){
List<List<String>> result=new ArrayList<>();
List<String> row = new ArrayList<>();
result.add(row);
row.add("课程ID");
row.add("课程名");
row.add("方向");
row.add("描述");
row.add("时长(小时)");
row.add("操作人");
row=new ArrayList<>();
result.add(row);
row.add("1");
row.add("Java基础课程");
row.add("Java");
row.add("初级");
row.add("200");
row.add("imooc");
row=new ArrayList<>();
result.add(row);
row.add("2");
row.add("Web进阶课程");
row.add("前端");
row.add("中级");
row.add("300");
row.add("imooc");
row=new ArrayList<>();
result.add(row);
row.add("3");
row.add("Linux入门");
row.add("Linux");
row.add("初级");
row.add("100");
row.add("imooc");
return result;
}
}
package com.vincent.course.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ImportExcelInitServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("/jsp/courseImport.jsp").forward(req,resp);
}
}
package com.vincent.course.servlet;
import com.vincent.course.data.ImportExcelParam;
import com.vincent.course.data.ImportExcelResult;
import com.vincent.course.data.ParamDto;
import com.vincent.course.service.ExcelService;
import com.vincent.course.util.RequestUtil;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class CourseImportServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(ServletFileUpload.isMultipartContent(request)) {
ParamDto dto= RequestUtil.parseParam(request);
ImportExcelParam param = new ImportExcelParam();
param.setExcel(dto.getFileMap().get("excel"));
ExcelService service = new ExcelService();
ImportExcelResult resultDto =service.imp(param);
request.setAttribute("result", resultDto);
}else {
}
request.getRequestDispatcher("/jsp/showCourse.jsp").forward(request,response);
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%
String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>课程批量导入</title>
</head>
<body>
<center>
<h1>课程批量导入</h1>
<hr>
<form action="<%=basePath%>CourseImportServlet" method="post" enctype="multipart/form-data" id="mainForm">
<table cellspacing="0px" cellpadding="0px" border="1px" width="400px">
<tr>
<td>Excel文件</td>
<td><input type="file" name="file1"></td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="button" value="导入" onclick="document.getElementById('mainForm').submit();">
<input type="reset" value="取消">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
<%@ page import="com.vincent.course.data.CourseDaoImpl" %>
<%@ page import="com.vincent.course.data.Course" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>课程查询</title>
<!-- 分页查看 -->
<%--<link rel="stylesheet" type="text/css" href="resources/js/dataTable/jquery.dataTables.min.css">--%>
<%--<script type="text/javascript" src="resources/js/dataTable/jquery.js"></script>--%>
<%--<script type="text/javascript" src="resources/js/dataTable/jquery.dataTables.min.js"></script>--%>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<%-- <script>--%>
<%-- $(function () {--%>
<%-- $("#search").click(function () {--%>
<%-- $.ajax({--%>
<%-- "url" : "<%=basePath%>GetCourseServlet",--%>
<%-- "type" : "get",--%>
<%-- "dataType" : "json",--%>
<%-- "data" : {--%>
<%-- "courseId" :this.courseId,--%>
<%-- "courseName" : this.courseName,--%>
<%-- "courseType" : this.courseType,--%>
<%-- "description":this.description,--%>
<%-- "courseTime":this.courseTime,--%>
<%-- "operator":this.operator--%>
<%-- },--%>
<%-- "success":function (json) {--%>
<%-- console.log(json);--%>
<%-- $("#cont tr").remove();--%>
<%-- var s=null;--%>
<%-- for(var i =0;i<json.length;i++){--%>
<%-- var course=json[i];--%>
<%-- s=s+"<tr>"+--%>
<%-- "<td>"+course.courseId+"</td>"+--%>
<%-- "<td>"+course.courseName+"</td>"+--%>
<%-- "<td>"+course.courseType+"</td>"+--%>
<%-- "<td>"+course.description+"</td>"+--%>
<%-- "<td>"+course.courseTime+"</td>"+--%>
<%-- "<td>"+course.operator+"</td>"+--%>
<%-- "</tr>"--%>
<%-- }--%>
<%-- $("#cont").html(s);--%>
<%-- }--%>
<%-- });--%>
<%-- });--%>
<%-- });--%>
<%-- </script>--%>
</head>
<body>
<div style="text-align: center;">
<h1>课程查询</h1>
<hr>
<p style="text-align: right">
<input type="text">
<input type="submit" value="查询" id="search">
</p>
<table cellspacing="0px" cellpadding="0px" border="1px" width="100%" class="tablelist" id="example">
<thead>
<tr>
<th>课程ID</th>
<th>课程名</th>
<th>方向</th>
<th>描述</th>
<th>时长(小时)</th>
<th>操作人</th>
</tr>
</thead>
<tbody id="cont">
<%
CourseDaoImpl cdi = new CourseDaoImpl();
List<Course> courseList = cdi.getAllCourse();
for(Course course:courseList){
%>
<tr>
<td><%=course.getId()%></td>
<td><%=course.getName()%></td>
<td><%=course.getCourseDirection()%></td>
<td><%=course.getDesc()%></td>
<td><%=course.getDuration()%></td>
<td><%=course.getOperator()%></td>
</tr>
<%
}
%>
<c:forEach var="course" items="${courseList}">
<tr>
<td>${course.courseId}</td>
<td>${course.courseName}</td>
<td>${course.courseType}</td>
<td>${course.description}</td>
<td>${course.courseTime}</td>
<td>${course.operator}</td>
</tr>
</c:forEach>
<c:forEach var="course" items="${result.courseList}">
<tr>
<td>${course.courseId}</td>
<td>${course.courseName}</td>
<td>${course.courseType}</td>
<td>${course.description}</td>
<td>${course.courseTime}</td>
<td>${course.operator}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</body>
</html>
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星