POI,servlet,jsp 實現excel檔案的匯入,並存到oracle資料庫中
阿新 • • 發佈:2018-12-12
導包
實體類
package com.crh.bean; import java.io.Serializable; /** * @author Chrui * @date 2018/09/24__22:48 */ public class ImportTitle implements Serializable { private String titles; private String terri; private String select1; private String score1; private String select2; private String score2; private String select3; private String score3; private String select4; private String score4; //以下是省略getter/setter }
private List<ImportTitle> excelList = new ArrayList();
private ImportTitle t = null;
後端程式碼一:讀取xls檔案
public void readXls(InputStream path) throws IOException { HSSFWorkbook hssfWorkbook = new HSSFWorkbook(path); // 迴圈工作表Sheet for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) { HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet); System.out.println("總行數:" + ",總列數:" + hssfSheet.getLastRowNum()); if (hssfSheet == null) { continue; } // 迴圈行Row for (int rowNum = 1; rowNum < hssfSheet.getLastRowNum(); rowNum++) { HSSFRow hssfRow = hssfSheet.getRow(rowNum); if (hssfRow != null) { t = new ImportTitle(); HSSFCell title = hssfRow.getCell(0); HSSFCell select1 = hssfRow.getCell(1); HSSFCell score1 = hssfRow.getCell(2); HSSFCell select2 = hssfRow.getCell(3); HSSFCell score2 = hssfRow.getCell(4); HSSFCell select3 = hssfRow.getCell(5); HSSFCell score3 = hssfRow.getCell(6); HSSFCell select4 = hssfRow.getCell(7); HSSFCell score4 = hssfRow.getCell(8); t.setTitles(getValue(title)); t.setSelect1(getValue(select1)); t.setSelect2(getValue(select2)); t.setSelect3(getValue(select3)); t.setSelect4(getValue(select4)); t.setScore1(getValue(score1)); t.setScore2(getValue(score2)); t.setScore3(getValue(score3)); t.setScore4(getValue(score4)); excelList.add(t); } } } }
後端程式碼二:servlet下使用讀取檔案
/** * 匯入檔案 */ private void inportTitles(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String territoryIds = null; Integer territoryId = 0; if (territoryIds != null && !territoryIds.equals("")) { territoryId = Integer.valueOf(territoryIds); } response.setContentType("text/html;charset=UTF-8"); try { FileItemFactory factory = new DiskFileItemFactory(); //檔案上傳核心工具類 ServletFileUpload upload = new ServletFileUpload(factory); //單個檔案大小限制 upload.setFileSizeMax(10 * 1024 * 1024); //總檔案大小限制 upload.setSizeMax(50 * 1024 * 1024); //對中文編碼處理 upload.setHeaderEncoding("UTF-8"); if (ServletFileUpload.isMultipartContent(request)) { List<FileItem> list = upload.parseRequest(request); //遍歷 for (FileItem item : list) { String name = item.getFieldName(); // 獲取name屬性的值(必須要寫的,當前端的表單有text型別的資料時候),就這樣獲取表單的元素 String value = item.getString("utf-8"); // 獲取value屬性的值 if (name.equals("territory")) { territoryIds = value; } if (!item.isFormField()) { //讀取檔案 readXls(item.getInputStream()); } } } } catch (Exception e) { throw new RuntimeException(e); } // 新增 //處理新增進列表的資料 } }
前端程式碼
<form action="${pageContext.request.contextPath}/admin/reportServlet?flag=inportTitles"
data-validator-option="{theme:'bootstrap', timely:2, stopOnError:true}" id="titleForm"
class="form-horizontal" role="form" enctype="multipart/form-data" method="post">
<div class="form-group">
<label class="control-label col-sm-3">領域:</label>
<div class="col-sm-5">
<select name="territory" id="territoryIdss" class="form-control">
<option selected value="0">請選擇</option>
<c:forEach items="${territoryList}" var="terr">
<option value="${terr.territoryId}">${terr.territoryName}</option>
</c:forEach>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3">上傳檔案:</label>
<div class="col-sm-6">
<input id="articleImageFile" name="f1" type="file" class="form-control"
style="width:100%; display: inline;"/>
</div>
</div>
<div class="form-inline">
<div class="col-sm-offset-9">
<input type="submit" class="btn btn-primary btn-sm" value="匯入"/>
</div>
</div>
</form>