Package org.apache.poi.hssf.usermodel

Examples of org.apache.poi.hssf.usermodel.HSSFRow$MissingCellPolicy


                 int rowCount2 = sheet2.getLastRowNum();
                 // check if rows are the same for both books
                 if(rowCount1 == rowCount2) {
                   // iterate through the rows
                   for(int j=sheet1.getFirstRowNum();j<=rowCount1;j++) {
                     HSSFRow row1 = sheet1.getRow(j);
                     HSSFRow row2 = sheet2.getRow(j);
                     // check if the rows are not null
                     if(row1 != null && row2 != null) {
                         Iterator it1 = row1.cellIterator();
                         Iterator it2 = row2.cellIterator();
                         while(it1.hasNext() && it2.hasNext()) {
                             HSSFCell cell1 = (HSSFCell) it1.next();
                             HSSFCell cell2 = (HSSFCell) it2.next();
                             if(cell1 != null && cell2 != null) {
                               HSSFCellStyle cell1Style = cell1.getCellStyle();
View Full Code Here


    // iterate through the rows
    int rowCount1 = sheet1.getLastRowNum();
    for (int j = sheet1.getFirstRowNum(); j <= rowCount1; j++)
    {
      HSSFRow row1 = sheet1.getRow(j);
      HSSFRow row2 = sheet2.getRow(j);
      // check if the rows are not null
      if (row1 == null && row2 == null)
      {
        continue;
      }
     
      if (row1 == null || row2 == null)
      {
        String dataToWrite = "Sheet #" + sheetNumber + "Row # " + j + "has a different data.File 1 Cell has " + row1 + "Whereas File2 has " + row2 + "\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
        outputStream.write(dataToWrite.getBytes());
        return false;
      }

      Iterator it1 = row1.cellIterator();
      Iterator it2 = row2.cellIterator();
      while (it1.hasNext() && it2.hasNext())
      {
        HSSFCell cell1 = (HSSFCell) it1.next();
        HSSFCell cell2 = (HSSFCell) it2.next();
        if (cell1 == null || cell2 == null)
View Full Code Here

 
  // ----------------------------------------------------------

  public void parseSheet(HSSFSheet oSheet, String sFileDescriptor) {
    HSSFCell oCel;
    HSSFRow oRow = oSheet.getRow(0);
    int iRow;
    char cDelim;
    String[] aFileDescriptor;
    int iFileDescLen;
   
    if (isVoid(sFileDescriptor)) {
      iRow = 1;
      sFileDescriptor = "";
      short iCel = (short) 0;
    oCel = oRow.getCell(iCel);
    while (oCel!=null) {
      if (isVoid(oCel.getStringCellValue())) break;
      sFileDescriptor += (sFileDescriptor.length()==0 ? "" : "\t") + oCel.getStringCellValue();
      oCel = oRow.getCell(++iCel);
    } // wend
    } else {
      iRow = 1;
      aFileDescriptor = Gadgets.split(sFileDescriptor, extractDelimiter(sFileDescriptor));
      iFileDescLen = aFileDescriptor.length;
      for (int c=0; c<iFileDescLen; c++) {
        oCel = oRow.getCell(c);
        if (null==oCel) {
          iRow = 0;
          break;
        } else if (!aFileDescriptor[c].equalsIgnoreCase(oCel.getStringCellValue())) {
          iRow = 0;
          break;         
        }
      } //next
    } // fi
    cDelim = extractDelimiter(sFileDescriptor);
    aFileDescriptor = Gadgets.split(sFileDescriptor, cDelim);
    iFileDescLen = aFileDescriptor.length;
    StringBuffer oData = new StringBuffer();
  while (!isEmptyRow(oSheet.getRow(iRow),iFileDescLen)){
    oRow = oSheet.getRow(iRow);
    if (oRow.getCell(0)!=null)
      oData.append(oRow.getCell(0).getStringCellValue());
    for (int c=1; c<iFileDescLen; c++) {
      oData.append(cDelim);
      if (oRow.getCell(c)!=null)
        oData.append(oRow.getCell(c).getStringCellValue());
    } // next
    oData.append((char) 10);
    iRow++;
  } // wend
  parseData(oData.toString().toCharArray(), sFileDescriptor);
View Full Code Here

  {
   StringBuffer sb = new StringBuffer();

   //*-- scan the rows from top to bottom
   for (int i = cSheet.getFirstRowNum(); i <= cSheet.getLastRowNum(); i++)
   { HSSFRow cRow = cSheet.getRow(i);
     if (cRow == null) continue;      //*-- skip empty rows

     //*-- for every row, scan each cell from left to right
     for (short j = cRow.getFirstCellNum(); j <= cRow.getLastCellNum(); j++)
     { HSSFCell cCell = cRow.getCell(j);

     if (cCell == null) continue//*-- skip empty cells
     if (cCell.getCellType() == HSSFCell.CELL_TYPE_STRING)
      { String cellValue = cCell.getStringCellValue();
        if (cellValue != null) { sb.append(" "); sb.append(cellValue); }
View Full Code Here

      sheet = workbook.createSheet();
    }
    title = data;
    index = 0;
    writeItem(data);
    HSSFRow titleRow = sheet.getRow(index);
    HSSFCellStyle titleStyle = getTitleStyle();
    for (int i = 0; i < titleRow.getLastCellNum(); i++) {
      titleRow.getCell(i).setCellStyle(titleStyle);
    }
    index++;
  }
View Full Code Here

  public String getFormat() {
    return TransferFormats.XLS;
  }

  protected void writeItem(Object datas) {
    HSSFRow row = sheet.createRow(index); // 建立新行
    if (datas != null) {
      if (datas.getClass().isArray()) {
        Object[] values = (Object[]) datas;
        for (int i = 0; i < values.length; i++) {
          HSSFCell cell = row.createCell(i);
          if (values[i] instanceof Number) {
            cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
            cell.setCellValue(((Number) values[i]).doubleValue());
          } else if (values[i] instanceof java.sql.Date) {
            cell.setCellValue((Date) values[i]);
            cell.setCellStyle(getDateStyle());
          } else if (values[i] instanceof java.util.Date) {
            cell.setCellValue((Date) values[i]);
            cell.setCellStyle(getTimeStyle());
          } else if (values[i] instanceof Calendar) {
            cell.setCellValue((Calendar) values[i]);
            cell.setCellStyle(getTimeStyle());
          } else {
            cell.setCellValue(new HSSFRichTextString((values[i] == null) ? "" : values[i]
                .toString()));
          }
        }
      } else {
        HSSFCell cell = row.createCell(0);
        // cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        if (datas instanceof Number) {
          cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        }
        cell.setCellValue(new HSSFRichTextString(datas.toString()));
View Full Code Here

   * @param sheet
   * @param rowIndex
   * @return
   */
  protected String[] readLine(HSSFSheet sheet, int rowIndex) {
    HSSFRow row = sheet.getRow(rowIndex);
    logger.debug("values count:{}", row.getLastCellNum());
    List<String> attrList = CollectUtils.newArrayList();
    for (int i = 0; i < row.getLastCellNum(); i++) {
      HSSFCell cell = row.getCell(i);
      if (null != cell) {
        String attr = cell.getRichStringCellValue().getString();
        if (StringUtils.isEmpty(attr)) {
          break;
        } else {
View Full Code Here

  }

  public Object read() {
    HSSFSheet sheet = workbook.getSheetAt(sheetNum);
    if (indexInSheet > sheet.getLastRowNum()) { return null; }
    HSSFRow row = sheet.getRow(indexInSheet);
    indexInSheet++;
    // 如果是个空行,返回空记录
    if (row == null) {
      return new Object[attrCount];
    } else {
      Object[] values = new Object[((attrCount != 0) ? attrCount : row.getLastCellNum())];
      for (int k = 0; k < values.length; k++) {
        values[k] = getCellValue(row.getCell(k));
      }
      return values;
    }
  }
View Full Code Here

  public static void main(String[] args) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");

    // Create a row and put some cells in it. Rows are 0 based.
    HSSFRow row = sheet.createRow(0);

    // Create a cell and put a date value in it. The first cell is not
    // styled as a date.
    HSSFCell cell = row.createCell(0);
    cell.setCellValue(new Date());

    // we style the second cell as a date (and time). It is important to
    // create a new cell style from the workbook
    // otherwise you can end up modifying the built in style and effecting
    // not only this cell but other cells.
    HSSFCellStyle cellStyle = wb.createCellStyle();
    DataFormat df = wb.createDataFormat();
    // cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
    cellStyle.setDataFormat(df.getFormat("YYYY-MM-DD HH:MM:SS"));
    cell = row.createCell(1);
    cell.setCellValue(new Date());
    cell.setCellStyle(cellStyle);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("src/test/resources/workbook.xls");
    wb.write(fileOut);
View Full Code Here

   * @throws Exception
   */
  public HSSFWorkbook toExcel(HSSFWorkbook wb, String sheetName, Collection<Object[]> datas,
      String propertyShowKeys) throws Exception {
    HSSFSheet sheet = wb.createSheet(sheetName);
    HSSFRow row = null;
    HSSFCell cell = null;

    /** **************** 取得传入的list列名称和显示名称 ********** */
    String[] pShowKeys = Tokenizer2StringArray(propertyShowKeys, ",");
    // 创建�?行(标题)
    row = sheet.createRow(0); // 建立新行
    // 显示标题列名
    for (int i = 0; i < pShowKeys.length; i++) {
      cell = row.createCell(i); // 建立新cell
      // cell.setEncoding(HSSFCell.ENCODING_UTF_16);
      cell.setCellValue(new HSSFRichTextString(pShowKeys[i]));
    }
    // 逐行取数
    int rowId = 1;// 数据行号(从2行开始填充数�?)
    for (Iterator<Object[]> iter = datas.iterator(); iter.hasNext(); rowId++) {
      row = sheet.createRow(rowId); // 建立新行
      Object[] objs = iter.next();
      // 生成每一�?
      for (int j = 0; j < objs.length; j++) {
        cell = row.createCell(j); // 建立新cell
        // cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue(new HSSFRichTextString((objs[j] == null) ? "" : objs[j].toString()));
      }
    }
    return wb;
View Full Code Here

TOP

Related Classes of org.apache.poi.hssf.usermodel.HSSFRow$MissingCellPolicy

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.