net.sf.jxls重写包 解决poi升级版本后jxls版本过低不兼容的问题
立即下载
资源介绍:
jxls重写包,解决poi升级版本后jxls版本过低不兼容的问题;由于jxls后续没有维护,版本一直处于较老版本,而且jxls内部使用了poi的东西,poi一直有在升级版本,如果项目上把poi版本升了上来,jxls会不兼容,需要重写才能使用
package net.sf.jxls.util;
import net.sf.jxls.transformer.RowCollection;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.regex.Pattern;
/**
* This class contains many utility methods used by jXLS framework
*
* @author Leonid Vysochyn
* @author Vincent Dutat
*/
public final class Util {
protected static final Log log = LogFactory.getLog(Util.class);
private static final String[][] ENTITY_ARRAY = { { "quot", "34" }, // " -
// double
// -
// quote
{ "amp", "38" }, // & - ampersand
{ "lt", "60" }, // < - less-than
{ "gt", "62" }, // > - greater-than
{ "apos", "39" } // XML apostrophe
};
private static Map xmlEntities = new HashMap();
static {
for (int i = 0; i < ENTITY_ARRAY.length; i++) {
xmlEntities.put(ENTITY_ARRAY[i][1], ENTITY_ARRAY[i][0]);
}
}
public static void removeRowCollectionPropertiesFromRow(
RowCollection rowCollection) {
int startRow = rowCollection.getParentRow().getPoiRow().getRowNum();
Sheet sheet = rowCollection.getParentRow().getSheet()
.getPoiSheet();
for (int i = 0, c = rowCollection.getDependentRowNumber(); i <= c; i++) {
Row hssfRow = sheet.getRow(startRow + i);
if (hssfRow.getFirstCellNum() >= 0 && hssfRow.getLastCellNum() >= 0) {
for (int j = hssfRow.getFirstCellNum(), c2 = hssfRow.getLastCellNum(); j <= c2; j++) {
Cell cell = hssfRow.getCell(j);
removeRowCollectionPropertyFromCell(cell, rowCollection
.getCollectionProperty().getFullCollectionName());
}
}
}
}
private static void removeRowCollectionPropertyFromCell(Cell cell,
String collectionName) {
String regex = "[-+*/().A-Za-z_0-9\\s]*";
if (cell != null && cell.getCellType() == CellType.STRING) {
String cellValue = cell.getRichStringCellValue().getString();
String strToReplace = "\\$\\{" + regex
+ collectionName.replaceAll("\\.", "\\\\.") + "\\." + regex
+ "\\}";
cell.setCellValue(cell.getSheet().getWorkbook().getCreationHelper().createRichTextString(cellValue.replaceAll(
strToReplace, "")));
}
}
/**
* Removes merged region from sheet
*
* @param sheet
* @param region
*/
public static void removeMergedRegion(Sheet sheet,
CellRangeAddress region) {
int index = getMergedRegionIndex(sheet, region);
if (index >= 0) {
sheet.removeMergedRegion(index);
}
}
/**
* returns merged region index
*
* @param sheet
* @param mergedRegion
* @return index of mergedRegion or -1 if the region not found
*/
private static int getMergedRegionIndex(Sheet sheet,
CellRangeAddress mergedRegion) {
for (int i = 0, c = sheet.getNumMergedRegions(); i < c; i++) {
CellRangeAddress region = getMergedRegion(sheet, i);
if (areRegionsEqual(region, mergedRegion)) {
return i;
}
}
return -1;
}
public static boolean areRegionsEqual(CellRangeAddress region1,
CellRangeAddress region2) {
if ((region1 == null && region2 != null)
|| (region1 != null && region2 == null)) {
return false;
}
if (region1 == null) {
return true;
}
return (region1.getFirstColumn() == region2.getFirstColumn()
&& region1.getLastColumn() == region2.getLastColumn()
&& region1.getFirstRow() == region2.getFirstRow() && region2
.getLastRow() == region2.getLastRow());
}
private static CellRangeAddress getMergedRegion(Sheet sheet, int i) {
CellRangeAddress region = sheet.getMergedRegion(i);
return region;
}
protected static boolean isNewMergedRegion(CellRangeAddress region,
Collection mergedRegions) {
for (Iterator iterator = mergedRegions.iterator(); iterator.hasNext();) {
CellRangeAddress cellRangeAddress = (CellRangeAddress) iterator
.next();
if (areRegionsEqual(cellRangeAddress, region)) {
return false;
}
}
return true;
}
public static CellRangeAddress getMergedRegion(Sheet sheet, int rowNum, int cellNum) {
for (int i = 0, c = sheet.getNumMergedRegions(); i < c; i++) {
CellRangeAddress merged = getMergedRegion(sheet, i);
if (isRangeContainsCell(merged, rowNum, cellNum)) {
return merged;
}
}
return null;
}
public static boolean isRangeContainsCell(CellRangeAddress range, int row, int col) {
if ((range.getFirstRow() <= row) && (range.getLastRow() >= row)
&& (range.getFirstColumn() <= col)
&& (range.getLastColumn() >= col)) {
return true;
}
return false;
}
public static boolean removeMergedRegion(Sheet sheet, int rowNum,
int cellNum) {
Set mergedRegionNumbersToRemove = new TreeSet();
for (int i = 0, c = sheet.getNumMergedRegions(); i < c; i++) {
CellRangeAddress merged = getMergedRegion(sheet, i);
if (isRangeContainsCell(merged, rowNum, cellNum)) {
mergedRegionNumbersToRemove.add(i);
}
}
for (Iterator iterator = mergedRegionNumbersToRemove.iterator(); iterator
.hasNext();) {
Integer regionNumber = (Integer) iterator.next();
sheet.removeMergedRegion(regionNumber.intValue());
}
return !mergedRegionNumbersToRemove.isEmpty();
}
public static void prepareCollectionPropertyInRowForDuplication(
RowCollection rowCollection, String collectionItemName) {
int startRow = rowCollection.getParentRow().getPoiRow().getRowNum();
Sheet sheet = rowCollection.getParentRow().getSheet()
.getPoiSheet();
for (int i = 0, c = rowCollection.getDependentRowNumber(); i <= c; i++) {
Row hssfRow = sheet.getRow(startRow + i);
if (hssfRow.getFirstCellNum() >= 0 && hssfRow.getLastCellNum() >= 0) {
for (int j = hssfRow.getFirstCellNum(), c2 = hssfRow.getLastCellNum(); j <= c2; j++) {
Cell cell = hssfRow.getCell(j);
prepareCollectionPropertyInCellForDuplication(cell,
rowCollection.getCollectionProperty()
.getFullCollectionName(), collectionItemName);
}
}
}
}
private static void prepareCollectionPropertyInCellForDuplication(
Cell cell, String collectionName, String collectionItemName) {
if (cell != null && cell.getCellType() == CellType.STRING) {
String cellValue = cell.getRic