|
|
?????????????????????????????????????????????????????????????????????????????Excel???????????????????????????????????
??????????office?????????????????????
???????
??office?????????????POI???????80%??????????????commons-fileupload?????pom????????
- <!-- office?? -->
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi</artifactId>
- <version>4.1.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi-ooxml</artifactId>
- <version>4.1.0</version>
- </dependency>
- <!-- ???? -->
- <dependency>
- <groupId>commons-fileupload</groupId>
- <artifactId>commons-fileupload</artifactId>
- <version>1.4</version>
- </dependency>
复制代码
??????????mvc???????????????????????multipart??spring-mvc.xml????????
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <property name="defaultEncoding" value="UTF-8"></property>
- <property name="maxUploadSize" value="10485770"></property>
- <property name="maxInMemorySize" value="10485760"></property>
- </bean>
复制代码
???????????10MB???excel???????
????????????
?MapController??????3???
MapController.java
- @ResponseBody
- @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
- public Object importExcel(HttpServletRequest request) {
- try {
- ServletContext servletContext = request.getServletContext();
- String uploadPath = servletContext.getRealPath("/upload");
- File dir = new File(uploadPath);
- if (!dir.exists()) {
- dir.mkdir();
- }
- CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(servletContext);
- if (multipartResolver.isMultipart(request)) {
- MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
- Iterator<String> iter = multiRequest.getFileNames();
- while (iter.hasNext()) {
- MultipartFile file = multiRequest.getFile(iter.next());
- if (file.getSize() > 0) {
- String fileName = file.getOriginalFilename();
- String extension = fileName.substring(fileName.lastIndexOf("."));
- if (!extension.toLowerCase().equals(".xls") && !extension.toLowerCase().equals(".xlsx")) {
- throw new Exception("????????????.xls?.xlsx??????");
- }
- String destFileName = fileName + "_" + System.currentTimeMillis() + extension;
- File destFile = new File(uploadPath, destFileName);
- file.transferTo(destFile);
- List<WowMap> dataList = this.loadExcelData(destFile.getPath());
- this.saveExcelData(dataList);
- if (!destFile.delete()) {
- logger.warn("?????????" + destFile.getAbsolutePath());
- }
- }
- }
- }
- return CommonResult.success();
- } catch (Exception ex) {
- logger.error(ex.getMessage(), ex);
- return CommonResult.fail();
- }
- }
- protected List<WowMap> loadExcelData(String excelPath) throws Exception {
- FileInputStream fileInputStream = new FileInputStream(excelPath);
- XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
- Sheet sheet = workbook.getSheet("??");
- List<WowMap> wowMapList = new ArrayList<>();
- // ?????????????
- String createUser = this.currentUserName();
- for (int rowNum = 2; rowNum <= sheet.getLastRowNum(); rowNum++) {
- XSSFRow row = (XSSFRow) sheet.getRow(rowNum);
- String name = PoiUtil.getCellValue(row.getCell(2));
- DataDict.Occupy occupy = DataDict.Occupy.getByDesc(PoiUtil.getCellValue(row.getCell(4)));
- WowMap wowMap = new WowMap();
- wowMap.setName(name);
- wowMap.setOccupy(occupy.getCode());
- wowMap.setDescription("");
- wowMap.setCreateUser(createUser);
- wowMapList.add(wowMap);
- }
- fileInputStream.close();
- return wowMapList;
- }
- protected void saveExcelData(List<WowMap> dataList) {
- wowMapManager.batchInsert(dataList);
- }
复制代码
???importExcel??????????????????????????????????????????????????????????????????????????????; loadExcelData?????POI??????Excel???Excel????????????????????????????????????Excel?????????????; saveExcelData???????????????????????batchInsert??????????????????????????
??????POI????Excel????????????????????????PoiUtil???????????????????????????????????????util???????????????????????util?????com.idlewow.util.poi????PoiUtil??
PoiUtil.java
- package com.idlewow.util.poi;
- import org.apache.commons.lang3.StringUtils;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.CellType;
- import org.apache.poi.ss.usermodel.DateUtil;
- import java.text.DecimalFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class PoiUtil {
- public static String getCellValue(Cell cell) {
- CellType cellType = cell.getCellType();
- if (cellType.equals(CellType.STRING)) {
- return cell.getStringCellValue();
- } else if (cellType.equals(CellType.NUMERIC)) {
- if (DateUtil.isCellDateFormatted(cell)) {
- Date date = cell.getDateCellValue();
- return date == null ? "" : new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
- } else {
- return new DecimalFormat("0.##").format(cell.getNumericCellValue());
- }
- } else if (cellType.equals(CellType.FORMULA)) {
- if (StringUtils.isNotBlank(cell.getStringCellValue())) {
- return cell.getStringCellValue();
- } else {
- return cell.getNumericCellValue() + "";
- }
- } else if (cellType.equals(CellType.BOOLEAN)) {
- return cell.getBooleanCellValue() ? "TRUE" : "FALSE";
- } else {
- return "";
- }
- }
- }
复制代码
??????util???????util??????Poi??????rms?????util??????util????????scope?provided????????????????????????????????POI??????????
- <dependencies>
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi</artifactId>
- <version>4.1.0</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi-ooxml</artifactId>
- <version>4.1.0</version>
- <scope>provided</scope>
- </dependency>
- </dependencies>
复制代码
????????
???????list.jsp??????excel????
- <form>
-
-
- <div class="layui-inline layui-show-xs-block">
- <button type="button" class="layui-btn" onclick="xadmin.open('????','add',500,500)">
- <i class="layui-icon">?</i>????
- </button>
- </div>
- <div class="layui-upload layui-inline layui-show-xs-block">
- <button type="button" class="layui-btn layui-btn-normal" id="btnSelectFile">??Excel</button>
- <button type="button" class="layui-btn" id="btnImport">????</button>
- </div>
- </form>
复制代码
??????list.js????????????
- layui.use(['upload', 'table', 'form'], function () {
-
-
- layui.upload.render({
- elem: '#btnSelectFile',
- url: '/manage/map/importExcel',
- accept: 'file',
- exts: 'xls|xlsx',
- auto: false,
- bindAction: '#btnImport',
- done: function (result) {
- if (result.code === 1) {
- layer.alert(result.message, {icon: 6},
- function () {
- layui.layer.closeAll();
- layui.table.reload('datatable');
- });
- } else {
- layer.alert(result.message, {icon: 5});
- }
- }
- });
- });
复制代码
??????
???excel??????????????????????
??
??????Excel??????????????
???????https://idlestudio.ctfile.com/fs/14960372-383760599
???????https://www.cnblogs.com/lyosaki88/p/idlewow_6.html
?????
??????????????????
????????????????????
??????????????????????
????????????????????????
???????????????????????????
??????
?????https://www.cnblogs.com/lyosaki88/p/idlewow_6.html
|
|