ExecutorService 생성 스레드

6087 단어 Executorsexcel
자세히 보기

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;
		}
	} 
}


좋은 웹페이지 즐겨찾기