import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.CellValue;
/**
* excel單元格工具類,可以擴展某行中單元格的公式到指定行中
* <p>
* 1. generate a new row base on the specific row
* <p>
* 2. copy a cell to another one, the formula of the cell will be extended
* <p>
* 3. automatically extends the formula
*
* @date 2011-03-15
* @version 1.0
*
*/
public class CellFormulaUtil {
/**
* formula variable pattern
*/
private static final Pattern FORMULA_AUTO_VARIABLE_PATTERN = Pattern
.compile("$?[A-Za-z]+[0-9]+");
/**
* formula variable row number pattern
*/
private static final Pattern FORMULA_ROW_NUM_PATTERN = Pattern
.compile("[A-Za-z]+");
/**
* generate a new row base on the specific row
*
* @param sourceRow
* the based on row
* @param targetSheet
* the target sheet
* @param pStartRow
* the start row number of target sheet
* @param pEndRow
* void the end row number of target sheet
*/
public static void excelRowExtend(HSSFRow sourceRow, HSSFSheet targetSheet,
int pStartRow, int pEndRow) {
// extend the row from the pStartRow to pEndRow
for (int rowIndex = pStartRow; rowIndex < pEndRow; rowIndex++) {
HSSFRow targetRow = targetSheet.createRow(rowIndex);
// iterate the columns
for (int colIndex = sourceRow.getFirstCellNum(); colIndex < sourceRow
.getLastCellNum(); colIndex++) {
HSSFCell sourceCell = sourceRow.getCell(colIndex);
HSSFCell targetCell = targetRow.createCell(colIndex);
// copy cell
cellCopy(sourceCell, targetCell, rowIndex - pStartRow + 1);
}
}
}
/**
* copy a cell to another one, the formula of the cell will be extended
*
* @param sourceCell
* the based on cell
* @param targetCell
* the target cell
* @param extendIndex
* the index of the extends formula
*/
public static void cellCopy(HSSFCell sourceCell, HSSFCell targetCell,
int extendIndex) {
if (sourceCell != null) {
// set cell comment
targetCell.setCellComment(sourceCell.getCellComment());
// set cell type
targetCell.setCellType(sourceCell.getCellType());
// set cell style
targetCell.setCellStyle(sourceCell.getCellStyle());
switch (sourceCell.getCellType()) {
case HSSFCell.CELL_TYPE_BOOLEAN:
targetCell.setCellValue(sourceCell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR:
targetCell.setCellErrorValue(sourceCell.getErrorCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA:
// auto increase the formula of the cell
targetCell.setCellFormula(formulaExtend(sourceCell
.getCellFormula(), extendIndex));
break;
case HSSFCell.CELL_TYPE_NUMERIC:
targetCell.setCellValue(sourceCell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
targetCell.setCellValue(sourceCell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
default:
break;
}
}
}
/**
* automatically extends the formula with the specific extend index
*
* @param sourceFormula
* the formula of the cell
* @param extendIndex
* the index of the extends formula
* @return String the target formula
*/
public static String formulaExtend(String sourceFormula, int extendIndex) {
List<String> variableList = new ArrayList<String>();
List<String> notFormulaList = new ArrayList<String>();
// all need auto increase variables
// temporary variable
String tempFormula = sourceFormula;
// split the formula with the regular expression
String[] arr = FORMULA_AUTO_VARIABLE_PATTERN.split(tempFormula);
if (null == arr || arr.length == 0) {
notFormulaList.add("");
variableList.add(tempFormula.replace("$", ""));
}
// iterate from 0 to array length -2
for (int i = 0; i < arr.length - 1; i++) {
int firstIndex = tempFormula.indexOf(arr[i]);
int leftIndex = arr[i].length();
String rightPart = tempFormula.substring(firstIndex + leftIndex);
int endIndex = rightPart.indexOf(arr[i + 1]) + leftIndex;
String variable = "";
if (firstIndex == 0) {
variable = tempFormula.substring(firstIndex + arr[i].length(),
endIndex);
tempFormula = tempFormula.substring(endIndex);
} else {
variable = tempFormula.substring(0, firstIndex);
tempFormula = tempFormula.substring(firstIndex);
}
notFormulaList.add(arr[i]);
variableList.add(variable);
// last one
if (i == arr.length - 2) {
notFormulaList.add(arr[i + 1]);
if (tempFormula.length() > arr[i + 1].length()) {
variable = tempFormula.substring(firstIndex
+ arr[i + 1].length());
variableList.add(variable);
notFormulaList.add("");
}
}
}
StringBuffer bf = new StringBuffer();
for (int j = 0; j < variableList.size(); j++) {
// replace the variable with the new one
String numbers = FORMULA_ROW_NUM_PATTERN.matcher(
variableList.get(j)).replaceAll("");
String characters = variableList.get(j).replace(numbers, "");
bf.append(notFormulaList.get(j));
int next = Integer.parseInt(numbers) + extendIndex;
bf.append(characters + String.valueOf(next));
}
bf.append(notFormulaList.get(notFormulaList.size() - 1));
return bf.toString();
}
/**
*
* getCellFormatValue:Format all cell data to string type
*
* @param
* @param cell
* @return String
*/
public static Object getCellFormatValue(HSSFCell cell) {
Object cellvalue = "";
if (cell != null) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// Cell Type
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
case HSSFCell.CELL_TYPE_ERROR:
return String.valueOf(cell.getErrorCellValue());
case HSSFCell.CELL_TYPE_BLANK:
return String.valueOf(cell.getRichStringCellValue());
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
cellvalue = sdf.format(cell.getDateCellValue());
} else {
cellvalue = Double.valueOf(cell.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_FORMULA: {
HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(cell
.getSheet().getWorkbook());
// if the formula is error
if (cell.getCachedFormulaResultType() == HSSFCell.CELL_TYPE_ERROR) {
return new CellValue("formula error");
}
CellValue cellResultValue = eval.evaluate(cell);
if (cellResultValue.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
if (HSSFDateUtil.isCellDateFormatted(cell)) {
cellvalue = sdf.format(cell.getDateCellValue());
} else {
cellvalue = Double.valueOf(cell.getNumericCellValue());
}
break;
}
if (cellResultValue.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
cellvalue = Boolean.valueOf(cellResultValue
.getBooleanValue());
break;
} else {
String stringValue = cellResultValue.getStringValue();
if (null != stringValue) {
cellvalue = stringValue.replaceAll("'", "''");
} else {
cellvalue = stringValue;
}
break;
}
}
case HSSFCell.CELL_TYPE_STRING:
cellvalue = cell.getStringCellValue().replaceAll("'", "''");
break;
default:
cellvalue = "";
break;
}
} else {
cellvalue = "";
}
return cellvalue;
}
}
簡單的excel工具類
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.