`

POI导入Excel

 
阅读更多

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.text.DecimalFormat;
import java.text.Format;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.cnpc.oms.om.pojo.CommOmWorkStatus;

/**
 *
 * 标题:Excel导入工具类
 *
 * 作者:ShengLiguo
 * 
 */

public class OmImportExcelUtil {

 public static List analysis03ExcelDate(InputStream inStream, String[] head)
   throws Exception {

  List list = new ArrayList();

  HSSFWorkbook book = new HSSFWorkbook(inStream);

  HSSFSheet sheet = book.getSheetAt(0);

  HSSFRow row = null;

  for (int i = 1; i <= sheet.getLastRowNum(); i++) {

   Map map = new HashMap();
   String questionSenderOrg = "";
   String questionType = "";
   int k = 0;

   row = sheet.getRow(i);

   for (int j = 0; j < head.length; j++) {

    if (head[j].equals("questionSenderOrg")) {

     if (!"".equals(questionSenderOrg)
       && !"".equals(getCellValue(row.getCell(j)))) {
      questionSenderOrg = questionSenderOrg + "-"
        + getCellValue(row.getCell(j));
     } else {
      questionSenderOrg = questionSenderOrg
        + getCellValue(row.getCell(j));
     }

     if (k++ == 2) {
      map.put(head[j], questionSenderOrg);
      k = 0;
     }

     continue;

    }

    if (head[j].equals("questionType")) {

     if (!"".equals(questionType)
       && !"".equals(getCellValue(row.getCell(j)))) {
      questionType = questionType + "-"
        + getCellValue(row.getCell(j));
     } else {
      questionType = questionType
        + getCellValue(row.getCell(j));
     }

     if (k++ == 4) {
      map.put(head[j], questionType);
      k = 0;
     }

     continue;

    }

    map.put(head[j], getCellValue(row.getCell(j)));

   }

   list.add(map);

  }

  return list;

 }

 public static List analysis07ExcelDate(InputStream inStream, String[] head)
   throws Exception {

  List list = new ArrayList();

  XSSFWorkbook book = new XSSFWorkbook(inStream);

  XSSFSheet sheet = book.getSheetAt(0);

  XSSFRow row = null;

  for (int i = 1; i <= sheet.getLastRowNum(); i++) {

   Map map = new HashMap();
   String questionSenderOrg = "";
   String questionType = "";
   int k = 0;

   row = sheet.getRow(i);

   for (int j = 0; j < head.length; j++) {

    if (head[j].equals("questionSenderOrg")) {

     if (!"".equals(questionSenderOrg)
       && !"".equals(getCellValue(row.getCell(j)))) {
      questionSenderOrg = questionSenderOrg + "-"
        + getCellValue(row.getCell(j));
     } else {
      questionSenderOrg = questionSenderOrg
        + getCellValue(row.getCell(j));
     }

     if (k++ == 2) {
      map.put(head[j], questionSenderOrg);
      k = 0;
     }

     continue;

    }

    if (head[j].equals("questionType")) {

     if (!"".equals(questionType)
       && !"".equals(getCellValue(row.getCell(j)))) {
      questionType = questionType + "-"
        + getCellValue(row.getCell(j));
     } else {
      questionType = questionType
        + getCellValue(row.getCell(j));
     }

     if (k++ == 4) {
      map.put(head[j], questionType);
      k = 0;
     }

     continue;

    }

    map.put(head[j], getCellValue(row.getCell(j)));

   }

   list.add(map);

  }

  return list;

 }

 private static Object getCellValue(Cell cell) {
  if (cell.getCellType() == cell.CELL_TYPE_STRING) {
   return cell.getStringCellValue();
  }
  if (cell.getCellType() == cell.CELL_TYPE_NUMERIC) {
   if (DateUtil.isCellDateFormatted(cell)) {
    return cell.getDateCellValue();
   } else {
    Format format = new DecimalFormat("#");
    return format.format(cell.getNumericCellValue());
   }
  }
  return "";
 }

 private static PropertyDescriptor[] getPropertyDescriptors(Object obj) {
  BeanInfo ObjInfo;
  try {
   ObjInfo = Introspector.getBeanInfo(obj.getClass());
  } catch (IntrospectionException e) {
   return new PropertyDescriptor[0];
  }
  PropertyDescriptor[] propertyDesc = ObjInfo.getPropertyDescriptors();
  return propertyDesc;
 }

 public static Object setPropertyValue(Map map, Object obj) {
  PropertyDescriptor[] propertyDesc = getPropertyDescriptors(obj);
  for (int i = 0; i < propertyDesc.length; i++) {
   if (propertyDesc[i].getName().compareToIgnoreCase("class") == 0)
    continue;
   Object objValue = map.get(propertyDesc[i].getName()) == null ? ""
     : map.get(propertyDesc[i].getName());
   Object value = parseObject(objValue, propertyDesc[i]
     .getPropertyType());
   try {
    propertyDesc[i].getWriteMethod().invoke(obj, value);
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  return obj;
 }

 public static Object parseObject(Object fromObj, Class toClass) {
  Object objReturn = null;
  if (fromObj.equals("")) {
   return objReturn;
  }
  String fromClassName = fromObj.getClass().getName();
  String toClassName = toClass.getName();
  if (fromClassName.equals(toClassName)) {
   objReturn = fromObj;
  } else if ("java.lang.Short".equals(toClassName)
    || "java.lang.Integer".equals(toClassName)
    || "java.lang.Long".equals(toClassName)
    || "java.lang.Float".equals(toClassName)
    || "java.lang.Double".equals(toClassName)
    || "java.math.BigDecimal".equals(toClassName)) {
   try {
    Constructor constructor = toClass
      .getConstructor(new Class[] { String.class });
    objReturn = constructor.newInstance(new Object[] { fromObj });
   } catch (Exception e) {
    e.printStackTrace();
   }
  } else {
   throw new IllegalArgumentException("From " + fromClassName + " To "
     + toClassName + ", Unsupported");
  }
  return objReturn;
 }

 public static void main(String[] args) throws Exception {

  String[] head = { "title", "questionSenderOrg", "questionSenderOrg",
    "questionSenderOrg", "questionType", "questionType",
    "questionType", "questionType", "questionType",
    "questionReceiver", "solveStatus", "questionAskDate",
    "questionSolveDate", "questionFrom", "questionSolver",
    "questionSolveMethod", "questionSender", "questionSenderTel" };

  InputStream in = new FileInputStream(
    "C:/Documents and Settings/ShengLiGuo/桌面/工单.xlsx");
  List<Map> mapList = analysis07ExcelDate(in, head);
  CommOmWorkStatus workStatus = new CommOmWorkStatus();
  for (Map map : mapList) {
   setPropertyValue(map, workStatus);
   System.out.println("======" + workStatus.getTitle());
   System.out.println("======" + workStatus.getQuestionFrom());
  }

 }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics