excel 2007 코드 (일부 코드는 다른 2003과 공유)

3634 단어


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.nufront.euht.excel.imports.interfaces.ExcelInterface;
import com.nufront.euht.model.CapEbu;
import com.nufront.euht.model.CapEdu;
import com.nufront.euht.model.TrainLine;
import com.nufront.euht.pageModel.JsonTipMessage;
import com.nufront.euht.util.StringUtil;

public class Excel2007 implements ExcelInterface{
	private StringBuffer errorMessage =  new StringBuffer();
	private DecimalFormat decimalFormat = new DecimalFormat("0");//   number String

	@Override
	public boolean importExcel(String excelPath, List<TrainLine> lines) {
		if (lines == null)
			lines = new ArrayList<TrainLine>();
		File file = new File(excelPath);
		boolean isSuccess = true;
		XSSFWorkbook hwb;
		XSSFRow row = null;
		XSSFSheet sheet =null;
		try {
			hwb = new XSSFWorkbook(new FileInputStream(file));
			sheet = hwb.getSheetAt(0);
		} catch (Exception e) {
			errorMessage.append(" , excel ");
			isSuccess = false;
			e.printStackTrace();
		} 
		String preLineName = "";
		String preEduName = "";
		CapEdu edu = null;
		for (int i = sheet.getFirstRowNum()+1; i < sheet.getPhysicalNumberOfRows(); i++) {
			row = sheet.getRow(i);
			if (row == null) continue;
			// 
			String currentLineName = getValue(row, COL_LINE_NAME);
			if ( !StringUtil.isNullOrBlank(currentLineName) && !preLineName.equals(currentLineName)) {
				TrainLine line = new TrainLine();
				line.setLineName(currentLineName);
				lines.add(line);
				preLineName = currentLineName;
			}
			String currentEduName = getValue(row, COL_EDU_NAME);
			if ( !StringUtil.isNullOrBlank(currentEduName) && !preEduName.equals(currentEduName)) {
				edu = new CapEdu();
				edu.setEduName(currentEduName);
				TrainLine line = lines.get(lines.size()-1);
				line.getCapEduList().add(edu);
				preEduName = currentEduName;
			}
			CapEbu ebu = new CapEbu();
			if (edu != null) {
				edu.setEduIp(getValue(row, COL_EDU_IP));
				edu.setEduModel(getValue(row, COL_EDU_MODEL));
				edu.setEduIp(getValue(row, COL_EDU_IP));
				edu.getCapEbuList().add(ebu);
			}
			ebu.setEbuCode(getValue(row, COL_EBU_CODE));
			ebu.setLocation(getValue(row, COL_EBU_LOCATION));
			ebu.setHardwareVersion(getValue(row, COL_EBU_HARDWARE_VERSION));
			ebu.setEbuIp(getValue(row, COL_EBU_IP));
			ebu.setEbuMac(getValue(row, COL_EBU_MAC));
			// ebu.setRegion(getValue(row, COL_EBU_REGION);
		}
		return isSuccess;

	}
	
	public String getValue(XSSFRow row, int column) {
		XSSFCell cell = row.getCell(column);
		String value = null;
		if (cell == null) return null;
		if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING){
			value = cell.getStringCellValue();
		}
		else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC){
			value = decimalFormat.format(cell.getNumericCellValue());
		}
		return value;
	}

	@Override
	public void exportExcel(HttpServletResponse response, List<TrainLine> lines, String strSheetName, String strFileName)
			throws IOException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public JsonTipMessage importExcel(InputStream is, List<TrainLine> lines) {
		// TODO Auto-generated method stub
		return null;
	}
}

좋은 웹페이지 즐겨찾기