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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值