com.xidu.framework.common.view.documenthandler.AbstractExcel2007DocumentHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.xidu.framework.common.view.documenthandler.AbstractExcel2007DocumentHandler.java

Source

/******************************************************************************
 * @File name   :      AbstractExcel2007DocumentHandler.java
 *
 * @Author      :      Jianxi Wu
 *
 * @Date        :      2011-3-30
 *
 * @Copyright Notice: 
 * Copyright (c) 2011 SGM, Inc. All  Rights Reserved.
 * This software is published under the terms of the SGM Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2011-3-30 ?09:49:38        Jianxi Wu     1.0            Initial Version
 *****************************************************************************/
package com.xidu.framework.common.view.documenthandler;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;

import com.xidu.framework.common.view.documenthandler.Excel2007DocumentHandler;

/**
 *
 */
public abstract class AbstractExcel2007DocumentHandler implements Excel2007DocumentHandler {
    /**
     * Convenient method to obtain the cell in the given sheet, row and column.
     * <p>Creates the row and the cell if they still doesn't already exist.
     * Thus, the column can be passed as an int, the method making the needed downcasts.
     * @param sheet a sheet object. The first sheet is usually obtained by workbook.getSheetAt(0)
     * @param row thr row number
     * @param col the column number
     * @return the HSSFCell
     */
    protected XSSFCell getCell(XSSFSheet sheet, int row, int col) {
        XSSFRow sheetRow = sheet.getRow(row);
        if (sheetRow == null) {
            sheetRow = sheet.createRow(row);
        }
        XSSFCell cell = sheetRow.getCell(col);
        if (cell == null) {
            cell = sheetRow.createCell(col);
        }
        return cell;
    }

    /**
     * Convenient method to set a String as text content in a cell.
     * @param cell the cell in which the text must be put
     * @param text the text to put in the cell
     */
    protected void setText(XSSFCell cell, String text) {
        cell.setCellType(cell.getCellType());
        cell.setCellValue(text);
    }
}