ExcelReader(解析Excel的工具類)


package cn.com.css.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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.poifs.filesystem.POIFSFileSystem;

public class ExcelReader {
 
 private POIFSFileSystem fs;
 private HSSFWorkbook wb;
 private HSSFSheet sheet;
 private HSSFRow row;

 /**
  * 讀取Excel表格表頭的內容
  *
  * @param InputStream
  * @return String 表頭內容的數組
  */
 public String[] readExcelTitle(InputStream is) {
  try {
   fs = new POIFSFileSystem(is);
   wb = new HSSFWorkbook(fs);
  } catch (IOException e) {
   e.printStackTrace();
  }
  sheet = wb.getSheetAt(0);
  row = sheet.getRow(0);
  // 標題總列數
  int colNum = row.getPhysicalNumberOfCells();
  String[] title = new String[colNum];
  for (int i = 0; i < colNum; i++) {
   title[i] = getCellFormatValue(row.getCell((short) i));
  }
  return title;
 }

 /**
  * 讀取Excel數據內容
  *
  * @param InputStream
  * @return Map 包括單元格數據內容的Map對象
  */
 public Map<Integer, String> readExcelContent(InputStream is) {
  Map<Integer, String> content = new HashMap<Integer, String>();
  String str = "";
  try {
   fs = new POIFSFileSystem(is);
   wb = new HSSFWorkbook(fs);
  } catch (IOException e) {
   e.printStackTrace();
  }
  sheet = wb.getSheetAt(0);
  // 得到總行數
  int rowNum = sheet.getLastRowNum();
  row = sheet.getRow(0);
  int colNum1 = row.getPhysicalNumberOfCells(); // 獲取有數據的列的個數
  int colNum = 15;
  // 正文內容應該從第二行開始,第一行為表頭的標題
  for (int i = 1; i <= rowNum; i++) {
   row = sheet.getRow(i);
   int j = 0;

   while (j < colNum) {
    String cell = getCellFormatValue(row.getCell((short) j)).trim();
    if (cell == "") {
     j++;
     continue;
    }
    str += cell + " ";
    j++;
   }
   str.trim();
   // System.out.println(str);
   content.put(i, str);
   str = "";
  }
  return content;
 }

 /**
  * 獲取單元格數據內容為字符串類型的數據
  *
  * @param cell
  *            Excel單元格
  * @return String 單元格數據內容
  */
 /*
  * private String getStringCellValue(HSSFCell cell) { String strCell = "";
  * switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: strCell =
  * cell.getStringCellValue(); break; case HSSFCell.CELL_TYPE_NUMERIC:
  * strCell = String.valueOf(cell.getNumericCellValue()); break; case
  * HSSFCell.CELL_TYPE_BOOLEAN: strCell =
  * String.valueOf(cell.getBooleanCellValue()); break; case
  * HSSFCell.CELL_TYPE_BLANK: strCell = ""; break; default: strCell = "";
  * break; } if (strCell.equals("") || strCell == null) { return ""; } if
  * (cell == null) { return ""; } return strCell; }
  */

 /**
  * 獲取單元格數據內容為日期類型的數據
  *
  * @param cell
  *            Excel單元格
  * @return String 單元格數據內容
  */
 // private String getDateCellValue(HSSFCell cell) {
 // String result = "";
 // try {
 // int cellType = cell.getCellType();
 // if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
 // Date date = cell.getDateCellValue();
 // result = (date.getYear() + 1900) + "-" + (date.getMonth() + 1)
 // + "-" + date.getDate();
 // } else if (cellType == HSSFCell.CELL_TYPE_STRING) {
 // String date = getStringCellValue(cell);
 // result = date.replaceAll("[年月]", "-").replace("日", "").trim();
 // } else if (cellType == HSSFCell.CELL_TYPE_BLANK) {
 // result = "";
 // }
 // } catch (Exception e) {
 // //System.out.println("日期格式不對!");
 // e.printStackTrace();
 // }
 // return result;
 // }

 /**
  * 依據HSSFCell類型設置數據
  *
  * @param cell
  * @return
  */
 private String getCellFormatValue(HSSFCell cell) {
  String cellvalue = "";
  if (cell != null) {
   // 推斷當前Cell的Type
   switch (cell.getCellType()) {
   // 假設當前Cell的Type為NUMERIC
   case HSSFCell.CELL_TYPE_NUMERIC: {
    cellvalue = String.valueOf(cell.getNumericCellValue());
    break;
   }
   case HSSFCell.CELL_TYPE_FORMULA: {
    // 推斷當前的cell是否為Date
    if (HSSFDateUtil.isCellDateFormatted(cell)) {
     // 假設是Date類型則,轉化為Data格式

     // 方法1:這樣子的data格式是帶時分秒的:2011-10-12 0:00:00
     // cellvalue = cell.getDateCellValue().toLocaleString();

     // 方法2:這樣子的data格式是不帶帶時分秒的:2011-10-12
     Date date = cell.getDateCellValue();
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
     cellvalue = sdf.format(date);

    }
    // 假設是純數字
    else {
     // 取得當前Cell的數值
     cellvalue = String.valueOf(cell.getNumericCellValue());
    }
    break;
   }
    // 假設當前Cell的Type為STRIN
   case HSSFCell.CELL_TYPE_STRING:
    // 取得當前的Cell字符串
    cellvalue = cell.getRichStringCellValue().getString();
    break;
   // 默認的Cell值
   default:
    cellvalue = " ";
   }
  } else {
   cellvalue = "";
  }
  return cellvalue;

 }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM