JAVA Excel.xlsx 上傳於下載
阿新 • • 發佈:2020-12-04
1.檔案下載
// 下載檔名 String systemDate = DateUtil.getDate(DateUtil.FORMAT_DATETIME); String fileName = "StoProcessHistory_" + systemDate + Constants.RESOURCE_TEMPLAT_FILE_TYPE; // 獲取根路徑 String url = this.getClass().getResource(Constants.STRING_EMPTY).getPath(); int indexLen = url.indexOf(Constants.RESOURCE_TEMPLAT_FILE_NAME); String rootPath = url.substring(0, indexLen); // 獲取模板位置,讀取資料庫(也可以讀取配置檔案或寫死) String templatePath = rootPath + Constants.RESOURCE_TEMPLAT_PATH; // 實際位置 String path = templatePath + templateName; // 給伺服器上的EXCEL下拉框賦值 InputStream is = new FileInputStream(path); XSSFWorkbook workBook = new XSSFWorkbook(is); XSSFSheet sheet = workBook.getSheetAt(0); // 從資料庫中取出資料 List<WmsStoStockDto> resultListExcel = stockSearchListService.getStockSearchList(searchDto, null); // 資料存在的情況下 if (resultListExcel != null && resultListExcel.size() != 0) { // 向excel檔案中寫內容 sheet = stockSearchListService.setSelectDataToExcel(sheet, resultListExcel, outputFlag); // 設定檔案ContentType型別,這樣設定,會自動判斷下載檔案型別 response.setContentType("multipart/form-data"); // 設定檔案頭:最後一個引數是設定下載檔名 response.setHeader("Content-Disposition", "attachment;filename=" + fileName); ServletOutputStream out = response.getOutputStream(); workBook.write(out); out.close(); return null; } else { // 0件的場合 this.addActionMessage(getText(MsgCodeConstants.COMMON_MESSAGE_NORESULT)); return SUCCESS; } /** * 向excel檔案中寫內容 * * @param oldSheet XSSFSheet * @param dataList List<WmsMstStorageDto> * @return 處理結果 */ @Override public XSSFSheet setSelectDataToExcel(XSSFSheet oldSheet, List<WmsStoStockDto> stoSearchDtoList, String outputFlag) throws Exception { CellStyle style0 = oldSheet.getRow(1).getCell(0).getCellStyle(); for (int i = 0; i < stoSearchDtoList.size(); i++) { WmsStoStockDto dtoL = stoSearchDtoList.get(i); XSSFRow rowLine = oldSheet.createRow(i + 1); Cell cell = null; // 編號 cell = rowLine.createCell(0); cell.setCellStyle(style0); cell.setCellValue(i + 1); } return oldSheet; }
2.檔案上傳
/** * 讀取excel檔案 * * @param path 檔案路徑 * @return 處理結果 */ @Override public List<WmsStoCheckDetailDto> getExcelFile(String path, File uploadFile) throws Exception { // 路徑編碼的轉換 byte[] b = path.getBytes("ISO-8859-1"); String strPath = new String(b, "utf-8"); XSSFWorkbook wb = this.readExcelUtils(strPath, uploadFile); if (wb == null) { return null; } XSSFSheet sheet = wb.getSheetAt(0); int rowNum = sheet.getLastRowNum(); // 讀取的資料list List<WmsStoCheckDetailDto> dataList = new ArrayList<WmsStoCheckDetailDto>(); // 正文內容應該從第二行開始,第一行為表頭的標題 for (int i = 10; i <= rowNum; i += 2) { XSSFRow row = sheet.getRow(i); XSSFRow row2 = sheet.getRow(i + 1); WmsStoCheckDetailDto lineDto = new WmsStoCheckDetailDto(); XSSFRow firstRow = sheet.getRow(0); // 盤點編號 String item0 = firstRow.getCell(91).getRichStringCellValue().getString().replace("盤點編號:", ""); lineDto.setStockCheckId(item0); // 盤點明細編號 String item1 = row.getCell(0).getRichStringCellValue().getString(); lineDto.setCheckDetailNo(item1); // 盤點數量 String item2 = row2.getCell(54).getRichStringCellValue().getString(); lineDto.setCheckNum(item2); // 盤點擔當 String item3 = row2.getCell(81).getRichStringCellValue().getString(); lineDto.setCheckUserName(item3); // excel對應的行號 lineDto.setLineNo(i + 1); // 空白行跳過 if (ValidatorUtil.isEmptyIgnoreSpace(item0) && ValidatorUtil.isEmptyIgnoreSpace(item1) && ValidatorUtil.isEmptyIgnoreSpace(item2) && ValidatorUtil.isEmptyIgnoreSpace(item3)) { continue; } dataList.add(lineDto); } return dataList; }