POI native introducing reading method EXCEL

  POI native introducing reading method EXCEL

  import org.apache.poi.hssf.usermodel.*;

  import org.apache.poi.poifs.filesystem.POIFSFileSystem;

  import java.io.FileInputStream;

  import java.io.FileNotFoundException;

  import java.io.IOException;

  import java.io.InputStream;

  import java.text.SimpleDateFormat;

  import java.util.Date;

  import java.util.HashMap;

  import java.util.Map;

  public class ReadExcelUtil {

  private POIFSFileSystem fs;

  private HSSFWorkbook wb;

  private HSSFSheet sheet;

  private HSSFRow row;

  /**

  * Read the contents of the header of the Excel spreadsheet

  * @param is

  * @Return String array header content

  */

  public String[] readExcelTitle(InputStream is) {

  try {

  fs = new POIFSFileSystem(is);

  wb = new HSSFWorkbook(fs);

  } catch (IOException e) {

  e.printStackTrace ();

  }

  sheet = wb.getSheetAt(0);

  // get the first line row

  row = sheet.getRow(0);

  The total number of columns // title

  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;

  }

  /**

  * Read Excel data content

  * @param is

  * @Return Map Map object that contains the cell data content

  */

  public Map readExcelContent(InputStream is) {

  Map content = new HashMap();

  String str = "";

  String strCell = "";

  try {

  fs = new POIFSFileSystem(is);

  wb = new HSSFWorkbook(fs);

  } catch (IOException e) {

  e.printStackTrace ();

  }

  sheet = wb.getSheetAt(0);

  // get the number of rows

  int rowNum = sheet.getLastRowNum();

  // Since the 0th row and the first row has been incorporated herein index from start 2

  row = sheet.getRow(2);

  int colNum = row.getPhysicalNumberOfCells();

  // body content should start from the second row, first row header title

  for (int i = 2; i <= rowNum; i++) {

  row = sheet.getRow(i);

  int j = 0;

  while (j < colNum) {

  strCell = getStringCellValue(row.getCell(j));

  str += strCell + "-";

  // str +=row.getCell((short) j) + "^";

  j++;

  }

  content.put(i, str);

  str = "";

  }

  return content;

  }

  /**

  * Acquiring cell data content type string data

  *

  * @Param cell Excel cells

  * @Return String cell data content

  */

  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((int)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;

  }

  /**

  * Acquiring cell data content of the data type Date

  *

  * @param cell

  * Excel cells

  * @Return String cell data content

  */

  private String getDateCellValue(HSSFCell cell) {

  String result = "";

  try {

  int cell type = 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 ( "Date format is not correct!");

  e.printStackTrace ();

  }

  return result;

  }

  /**

  * The data set type HSSFCell

  * @param cell

  * @return

  */

  private String getCellFormatValue(HSSFCell cell) {

  String cellvalue = "";

  if (cell != null) {

  Analyzing current // Type of Cell

  switch (cell.getCellType()) {

  // If the current Cell Type to NUMERIC

  case HSSFCell.CELL_TYPE_NUMERIC:

  case HSSFCell.CELL_TYPE_FORMULA: {

  // determine whether the current cell is Date

  if (HSSFDateUtil.isCellDateFormatted(cell)) {

  Date date = cell.getDateCellValue();

  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

  CellValue = sdf.format (date);

  } Wuxi Women's Hospital ××× http://wapyyk.39.net/wx/zonghe/fc96e.html

  // If it is a pure digital

  else {

  // Get the current value of the Cell

  cellvalue = String.valueOf(cell.getNumericCellValue());

  }

  break;

  }

  // If the current Cell Type is STRIN

  case HSSFCell.CELL_TYPE_STRING:

  // Get the current string Cell

  cellvalue = cell.getRichStringCellValue().getString();

  break;

  // the default value Cell

  default:

  cellvalue = " ";

  }

  } else {

  cellvalue = "";

  }

  return cellvalue;

  }

  public static void main(String[] args) {

  try {

  // to read Excel spreadsheet title test

  InputStream is = new FileInputStream("d:\\test2.xls");

  ReadExcelUtil excelReader = new ReadExcelUtil();

  String[] title = excelReader.readExcelTitle(is);

  System.out.println ( "headline Excel table:");

  for (String s : title) {

  System.out.print(s + " ");

  }

  System.out.println();

  // to read Excel spreadsheet content test

  InputStream is2 = new FileInputStream("d:\\test2.xls");

  Map map = excelReader.readExcelContent(is2);

  System.out.println ( "get the contents of the Excel spreadsheet:");

  // here because the merged cell xls require special handling for index

  for (int i = 2; i <= map.size()+1; i++) {

  System.out.println(map.get(i));

  }

  } catch (FileNotFoundException e) {

  System.out.println ( "specified path was not found in the file!");

  e.printStackTrace ();

  }

  }

  }


Guess you like

Origin blog.51cto.com/14335413/2403490