ExecutorService 생성 스레드
package com.test.bo;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@Service
public class TestBo {
//
private static ExecutorService executor = Executors.newSingleThreadExecutor();
/**
*
*
* @param uploadFile
* @param uploadFileName
* @throws FileNotFoundException
* @throws IOException
*/
public void uploadFile(final MultipartFile uploadFile, final String uploadFileName) throws Exception {
final String[][] result = PoiUtil.getData(uploadFile, uploadFileName, 0, 1);// 0: sheet // 1: ,
executor.execute(new Runnable() {
@Override
public void run() {
try {
uploadFile(uploadFileName, result);
} catch (Exception e) {
e.printStackTrace();
logger.error(" ");
}
}
});
}
/**
* excel
*
* @param uploadFileName
* @param result
*/
public void uploadFile(final String uploadFileName, final String[][] result) {
//..... ,
}
}
PoiUtil에서 가져온 Excel 파일을 확인합니다.
package com.test.common.util;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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.web.multipart.MultipartFile;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
public class PoiUtil {
/**
* Excel , ,
*
* @param file
* Excel
* @param ignoreRows
* , 1
* @return Excel
* @throws FileNotFoundException
* @throws IOException
*/
public static String[][] getData(MultipartFile file, String uploadFileName, int sheetNum, int rowNum)
throws FileNotFoundException, IOException {
List result = new ArrayList();
int rowSize = 0;
InputStream in = file.getInputStream();
Workbook wb = null;
if (isExcel2003(uploadFileName)){
wb = new HSSFWorkbook(in);
}else{
wb = new XSSFWorkbook(in);
}
Cell cell = null;
Sheet st = wb.getSheetAt(sheetNum);
// ,
for (int rowIndex = rowNum; rowIndex <= st.getLastRowNum(); rowIndex++) {
Row row = st.getRow(rowIndex);
if (row == null) {
continue;
}
int tempRowSize = row.getLastCellNum() + 1;
if (tempRowSize > rowSize) {
rowSize = tempRowSize;
}
String[] values = new String[rowSize];
Arrays.fill(values, "");
boolean hasValue = false;
for (short columnIndex = 0; columnIndex <= row.getLastCellNum(); columnIndex++) {
String value = "";
cell = row.getCell(columnIndex);
if (cell != null) {
//cell.setEncoding(HSSFCell.ENCODING_UTF_16);
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
if (date != null) {
value = new SimpleDateFormat("yyyy-MM-dd").format(date);
} else {
value = "";
}
} else {
value = formatCnt(BigDecimal.valueOf(cell.getNumericCellValue()).toString());
}
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
value = String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
value = "";
break;
case HSSFCell.CELL_TYPE_ERROR:
value = "";
break;
default:
value = "";
}
}
// if (columnIndex == 0 && value.trim().equals("")) {
// break;
// }
values[columnIndex] = value.trim();
hasValue = true;
}
if (hasValue) {
result.add(values);
}
}
in.close();
String[][] returnArray = new String[result.size()][rowSize];
for (int i = 0; i < returnArray.length; i++) {
returnArray[i] = (String[]) result.get(i);
}
return returnArray;
}
public static boolean isExcel2003(String filePath){
return filePath.matches("^.+\\.(?i)(xls)$");
}
public static boolean isExcel2007(String filePath){
return filePath.matches("^.+\\.(?i)(xlsx)$");
}
public static String formatCnt(String cnt){
String[] arrayStr = cnt.split("\\.");
if(arrayStr.length>1 && Double.parseDouble("0."+arrayStr[1]) == 0){
return arrayStr[0];
}else{
return cnt;
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
ExecutorService 생성 스레드자세히 보기 PoiUtil에서 가져온 Excel 파일을 확인합니다....
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.