栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用Apache POI将HSSFWorkbook转换为XSSFWorkbook?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用Apache POI将HSSFWorkbook转换为XSSFWorkbook?

此代码已改编自我在preranch论坛上找到的内容

public final class ExceldocumentConverter {public static XSSFWorkbook convertWorkbookHSSFToXSSF(HSSFWorkbook source) {    XSSFWorkbook retVal = new XSSFWorkbook();    for (int i = 0; i < source.getNumberOfSheets(); i++) {        XSSFSheet xssfSheet = retVal.createSheet();        HSSFSheet hssfsheet = source.getSheetAt(i);        copySheets(hssfsheet, xssfSheet);    }    return retVal;}public static void copySheets(HSSFSheet source, XSSFSheet destination) {    copySheets(source, destination, true);}public static void copySheets(HSSFSheet source, XSSFSheet destination, boolean copyStyle) {    int maxColumnNum = 0;    Map<Integer, HSSFCellStyle> styleMap = (copyStyle) ? new HashMap<Integer, HSSFCellStyle>() : null;    for (int i = source.getFirstRowNum(); i <= source.getLastRowNum(); i++) {        HSSFRow srcRow = source.getRow(i);        XSSFRow destRow = destination.createRow(i);        if (srcRow != null) { copyRow(source, destination, srcRow, destRow, styleMap); if (srcRow.getLastCellNum() > maxColumnNum) {     maxColumnNum = srcRow.getLastCellNum(); }        }    }    for (int i = 0; i <= maxColumnNum; i++) {        destination.setColumnWidth(i, source.getColumnWidth(i));    }}public static void copyRow(HSSFSheet srcSheet, XSSFSheet destSheet, HSSFRow srcRow, XSSFRow destRow,        Map<Integer, HSSFCellStyle> styleMap) {    // manage a list of merged zone in order to not insert two times a    // merged zone    Set<CellRangeAddressWrapper> mergedRegions = new TreeSet<CellRangeAddressWrapper>();    destRow.setHeight(srcRow.getHeight());    // pour chaque row    for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) {        HSSFCell oldCell = srcRow.getCell(j); // ancienne cell        XSSFCell newCell = destRow.getCell(j); // new cell        if (oldCell != null) { if (newCell == null) {     newCell = destRow.createCell(j); } // copy chaque cell copyCell(oldCell, newCell, styleMap); // copy les informations de fusion entre les cellules // System.out.println("row num: " + srcRow.getRowNum() + // " , col: " + (short)oldCell.getColumnIndex()); CellRangeAddress mergedRegion = getMergedRegion(srcSheet, srcRow.getRowNum(),         (short) oldCell.getColumnIndex()); if (mergedRegion != null) {     // System.out.println("Selected merged region: " +     // mergedRegion.toString());     CellRangeAddress newMergedRegion = new CellRangeAddress(mergedRegion.getFirstRow(),  mergedRegion.getLastRow(), mergedRegion.getFirstColumn(), mergedRegion.getLastColumn());     // System.out.println("New merged region: " +     // newMergedRegion.toString());     CellRangeAddressWrapper wrapper = new CellRangeAddressWrapper(newMergedRegion);     if (isNewMergedRegion(wrapper, mergedRegions)) {         mergedRegions.add(wrapper);         destSheet.addMergedRegion(wrapper.range);     } }        }    }}public static void copyCell(HSSFCell oldCell, XSSFCell newCell, Map<Integer, HSSFCellStyle> styleMap) {    if (styleMap != null) {        int stHashCode = oldCell.getCellStyle().hashCode();        HSSFCellStyle sourceCellStyle = styleMap.get(stHashCode);        XSSFCellStyle destnCellStyle = newCell.getCellStyle();        if (sourceCellStyle == null) { sourceCellStyle = oldCell.getSheet().getWorkbook().createCellStyle();        }        destnCellStyle.cloneStyleFrom(oldCell.getCellStyle());        styleMap.put(stHashCode, sourceCellStyle);        newCell.setCellStyle(destnCellStyle);    }    switch (oldCell.getCellType()) {    case HSSFCell.CELL_TYPE_STRING:        newCell.setCellValue(oldCell.getStringCellValue());        break;    case HSSFCell.CELL_TYPE_NUMERIC:        newCell.setCellValue(oldCell.getNumericCellValue());        break;    case HSSFCell.CELL_TYPE_BLANK:        newCell.setCellType(HSSFCell.CELL_TYPE_BLANK);        break;    case HSSFCell.CELL_TYPE_BOOLEAN:        newCell.setCellValue(oldCell.getBooleanCellValue());        break;    case HSSFCell.CELL_TYPE_ERROR:        newCell.setCellErrorValue(oldCell.getErrorCellValue());        break;    case HSSFCell.CELL_TYPE_FORMULA:        newCell.setCellFormula(oldCell.getCellFormula());        break;    default:        break;    }}public static CellRangeAddress getMergedRegion(HSSFSheet sheet, int rowNum, short cellNum) {    for (int i = 0; i < sheet.getNumMergedRegions(); i++) {        CellRangeAddress merged = sheet.getMergedRegion(i);        if (merged.isInRange(rowNum, cellNum)) { return merged;        }    }    return null;}private static boolean isNewMergedRegion(CellRangeAddressWrapper newMergedRegion,        Set<CellRangeAddressWrapper> mergedRegions) {    return !mergedRegions.contains(newMergedRegion);}}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/407037.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号