excel 2007代码(部分代码与另一篇2003共享)


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

你可能感兴趣的:(excel 2007代码(部分代码与另一篇2003共享))