mirror of
https://github.com/suhf/No1WMS.git
synced 2026-02-05 12:13:33 +09:00
add excel upload
This commit is contained in:
17
src/main/java/com/no1/wms/excel/EgovWebUtil.java
Normal file
17
src/main/java/com/no1/wms/excel/EgovWebUtil.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package com.no1.wms.excel;
|
||||
|
||||
//../을 통해 부모디렉토리로 이동하는 방법을 막기 위한 보안
|
||||
public class EgovWebUtil {
|
||||
public static String filePathBlackList(String value) {
|
||||
String returnValue = value;
|
||||
if (returnValue == null || returnValue.trim().equals("")) {
|
||||
return "";
|
||||
}
|
||||
|
||||
returnValue = returnValue.replaceAll("\\.\\./", "");
|
||||
returnValue = returnValue.replaceAll("\\.\\.\\\\", "");
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,16 +1,21 @@
|
||||
package com.no1.wms.excel;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
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.xssf.usermodel.XSSFWorkbook;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.no1.wms.category.CategoryDto;
|
||||
@@ -19,8 +24,12 @@ import com.no1.wms.category.CategoryDto;
|
||||
|
||||
|
||||
@Service
|
||||
public class ExcelUtils {
|
||||
public class ExcelDownlodeUtils {
|
||||
|
||||
|
||||
|
||||
|
||||
//마지막 List<CategoryDto> dto 이부분을 수정해서 만들어야함.
|
||||
public void downloadCategoryExcelFile(String excelFileName, HttpServletResponse response,
|
||||
String sheetName, String[] columnName, List<CategoryDto> dto) {
|
||||
String fileName = "";
|
||||
@@ -32,7 +41,7 @@ public class ExcelUtils {
|
||||
response.setContentType("ms-vnd/excel");
|
||||
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");
|
||||
|
||||
Workbook workbook = new HSSFWorkbook();
|
||||
Workbook workbook = new XSSFWorkbook();
|
||||
Sheet sheet = workbook.createSheet(sheetName);
|
||||
|
||||
Row row = null;
|
||||
@@ -45,6 +54,7 @@ public class ExcelUtils {
|
||||
cell.setCellValue(columnName[i]);
|
||||
}
|
||||
rowNum += 1;
|
||||
|
||||
//수정부분
|
||||
makeCategoryBody(dto,row,sheet,cell,rowNum);
|
||||
|
||||
@@ -77,5 +87,43 @@ public class ExcelUtils {
|
||||
}
|
||||
}
|
||||
|
||||
//엑셀 폼 파일 다운로드
|
||||
//엑셀 폼은 직접 만들어서 src/main/webapp/excelfiles/excelform 이 경로의 폴더안에 넣으면 됩니다.
|
||||
//확장자는 .xlsx로 해주세요.
|
||||
//매개변수는 HttpServletResponse response와 파일명 String을 넣으면 되고 파일명은 한글도 가능합니다.
|
||||
public void downlodeExcelForm(HttpServletResponse response, String formName) throws IOException {
|
||||
String excelfilesDirectory = "src/main/webapp/excelfiles/excelform/";
|
||||
|
||||
|
||||
File file = new File(excelfilesDirectory+formName);
|
||||
|
||||
try( FileInputStream fis = new FileInputStream(file);
|
||||
BufferedInputStream bis = new BufferedInputStream(fis);
|
||||
OutputStream out = response.getOutputStream()){
|
||||
String encodedFilename = URLEncoder.encode(formName, "UTF-8").replaceAll("\\+", "%20");
|
||||
|
||||
response.addHeader("Content-Disposition", "attachment;filename=\""+encodedFilename+"\"");
|
||||
response.setContentType("application/vnd.ms-excel");
|
||||
// 응답 크기 명시
|
||||
response.setContentLength((int)file.length());
|
||||
int read = 0;
|
||||
while((read = bis.read()) != -1) {
|
||||
out.write(read);
|
||||
}
|
||||
|
||||
|
||||
}catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
110
src/main/java/com/no1/wms/excel/ExcelManagerXlsx.java
Normal file
110
src/main/java/com/no1/wms/excel/ExcelManagerXlsx.java
Normal file
@@ -0,0 +1,110 @@
|
||||
package com.no1.wms.excel;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.CellType;
|
||||
import org.apache.poi.ss.usermodel.DateUtil;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
|
||||
public class ExcelManagerXlsx {
|
||||
|
||||
private static ExcelManagerXlsx excelXlsxMng;
|
||||
|
||||
public ExcelManagerXlsx() {
|
||||
}
|
||||
|
||||
public static ExcelManagerXlsx getInstance() {
|
||||
if (excelXlsxMng == null) {
|
||||
excelXlsxMng = new ExcelManagerXlsx();
|
||||
}
|
||||
return excelXlsxMng;
|
||||
}
|
||||
|
||||
public List<HashMap<String, String>> getListXlsxRead(String excel) throws Exception {
|
||||
|
||||
List<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
|
||||
File file = new File( excel );
|
||||
if( !file.exists() || !file.isFile() || !file.canRead() ) {
|
||||
throw new IOException( excel );
|
||||
}
|
||||
XSSFWorkbook wb = new XSSFWorkbook( new FileInputStream(file) );
|
||||
|
||||
//xls시 이용
|
||||
//HSSFWorkbook wb = new HSSFWorkbook ( new FileInputStream(file) );
|
||||
|
||||
int sheetIndex = 0;
|
||||
|
||||
try {
|
||||
// 모든 시트 순회
|
||||
for( int i=0; i<1; i++ ) {
|
||||
XSSFSheet sheet = wb.getSheetAt(sheetIndex);
|
||||
for( Row row : sheet ) {
|
||||
// 첫 번째 시트만 사용
|
||||
if(row.getRowNum() == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
HashMap<String, String> hMap = new HashMap<String, String>();
|
||||
String valueStr = "";
|
||||
|
||||
int cellLength = (int) row.getLastCellNum();///
|
||||
|
||||
for (int j = 0; j < row.getLastCellNum(); j++) {
|
||||
Cell cell = row.getCell(j);
|
||||
|
||||
if (cell == null || cell.getCellType() == CellType.BLANK) {
|
||||
valueStr = "";
|
||||
}else{
|
||||
switch(cell.getCellType()){
|
||||
case STRING :
|
||||
valueStr = cell.getStringCellValue();
|
||||
break;
|
||||
case NUMERIC : // 날짜 형식이든 숫자 형식이든 다 CELL_TYPE_NUMERIC으로 인식함.
|
||||
if(DateUtil.isCellDateFormatted(cell)){ // 날짜 유형의 데이터일 경우,
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.KOREA);
|
||||
String formattedStr = dateFormat.format(cell.getDateCellValue());
|
||||
valueStr = formattedStr;
|
||||
break;
|
||||
}else{ //숫자 데이터일 경우,
|
||||
Double numericCellValue = cell.getNumericCellValue();
|
||||
if(Math.floor(numericCellValue) == numericCellValue){ // 소수점 이하를 버린 값이 원래의 값과 같다면,,
|
||||
valueStr = numericCellValue.intValue() + ""; // int형으로 소수점 이하 버리고 String으로 데이터 담는다.
|
||||
}else{
|
||||
valueStr = numericCellValue + "";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BOOLEAN :
|
||||
valueStr = cell.getBooleanCellValue() + "";
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
// 엑셀의 열 인덱스와 함께 값 매핑
|
||||
hMap.put("cell_"+j ,valueStr);
|
||||
|
||||
}
|
||||
// 결과 리스트에 추가
|
||||
list.add(hMap);
|
||||
}
|
||||
sheetIndex++;
|
||||
}
|
||||
|
||||
}catch(Exception ex){
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
86
src/main/java/com/no1/wms/excel/ExcelRequestManager.java
Normal file
86
src/main/java/com/no1/wms/excel/ExcelRequestManager.java
Normal file
@@ -0,0 +1,86 @@
|
||||
package com.no1.wms.excel;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
public class ExcelRequestManager {
|
||||
|
||||
public List<HashMap<String, String>> parseExcelSpringMultiPart
|
||||
(Map<String, MultipartFile> files , String KeyStr, int fileKeyParam,String atchFileId ,String storePath,HttpServletRequest request) throws Exception{
|
||||
List<HashMap<String, String>> list = null;
|
||||
int fileKey = fileKeyParam;
|
||||
|
||||
String storePathString = "";
|
||||
String atchFileIdString = "";
|
||||
|
||||
String folder = request.getServletContext().getRealPath("/");
|
||||
|
||||
if ("".equals(storePath) || storePath == null) {
|
||||
storePathString = folder+"/excelfiles/upload/";
|
||||
} else {
|
||||
storePathString = folder+"/excelfiles/upload/"+storePath;
|
||||
}
|
||||
|
||||
if (!"".equals(atchFileId) || atchFileId != null) {
|
||||
atchFileIdString = atchFileId;
|
||||
}
|
||||
|
||||
File saveFolder = new File(EgovWebUtil.filePathBlackList(storePathString));
|
||||
|
||||
//폴더 없으면 생성
|
||||
if (!saveFolder.exists() || saveFolder.isFile()) {
|
||||
saveFolder.mkdirs();
|
||||
}
|
||||
|
||||
Iterator<Entry<String, MultipartFile>> itr = files.entrySet().iterator();
|
||||
MultipartFile file;
|
||||
String filePath = "";
|
||||
|
||||
while (itr.hasNext()) {
|
||||
//파일 이름을 가져오는데 파일이 여러개일 경우
|
||||
Entry<String, MultipartFile> entry = itr.next();
|
||||
|
||||
file = entry.getValue();
|
||||
String orginFileName = file.getOriginalFilename();
|
||||
|
||||
if ("".equals(orginFileName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//파일의 확장자
|
||||
int index = orginFileName.lastIndexOf(".");
|
||||
String fileExt = orginFileName.substring(index + 1);
|
||||
//파일의 이름 정하기 지정한 KeyStr값과 시간 값과 fileKey를 넣어서 만듬
|
||||
String newName = KeyStr + getTimeStamp() + fileKey;
|
||||
|
||||
if (!"".equals(orginFileName)) {
|
||||
filePath = storePathString + File.separator + newName+"."+fileExt;
|
||||
file.transferTo(new File(EgovWebUtil.filePathBlackList(filePath)));
|
||||
}
|
||||
list = ExcelManagerXlsx.getInstance().getListXlsxRead(filePath);
|
||||
|
||||
fileKey++;
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private static String getTimeStamp() {
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
Date currentDate = new Date(currentTimeMillis);
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
|
||||
String formattedTimeStamp = dateFormat.format(currentDate);
|
||||
return formattedTimeStamp;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user