AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
RightFont 8 for windows instal free2/17/2024 If (leftStyle.getBorderBottom() != rightStyle.getBorderBottom()) If (leftStyle.getWrapText() != rightStyle.getWrapText()) If (leftStyle.getLocked() != rightStyle.getLocked()) If (leftStyle.getHidden() != rightStyle.getHidden()) If (leftStyle.getAlignment() != rightStyle.getAlignment()) Private static boolean cellStylesEquals(CellStyle leftStyle, If (cellStylesEquals(currentCellStyle, // CellStyle leftStyle,ĮxpectedSrcDataFormat) // short rightDataFormat, Int totalSrcCellStyles = dstWorkbook_.getNumCellStyles() įor (int dstStyleIdx = 0 dstStyleIdx < totalSrcCellStyles ++dstStyleIdx)ĬellStyle currentCellStyle = dstWorkbook_.getCellStyleAt(dstStyleIdx) Short expectedSrcDataFormat = dstDataFormat.getFormat(srcDataFormatString) String srcDataFormatString = srcCellStyle.getDataFormatString() Private CellStyle getSameCellStyle(CellStyle srcCellStyle)ĭataFormat dstDataFormat = dstWorkbook_.createDataFormat() Private void cop圜ell(Cell srcCell, Cell dstCell)ĭtCellStyle(srcCell.getCellStyle()) ĬellStyle dstCellStyle = getDstStyleFromSrcStyle(srcCell.getCellStyle()) ĭtCellValue(srcCell.getStringCellValue()) ĭtCellValue(srcCell.getNumericCellValue()) ĭtCellValue(srcCell.getBooleanCellValue()) ĭtCellErrorValue(srcCell.getErrorCellValue()) ĭtCellFormula(srcCell.getCellFormula()) Return !mergedRegions_.contains(dstMergedRegion) Private boolean isdstMergedRegion(CellRangeAddressWrapper dstMergedRegion) Private static CellRangeAddress getMergedRegion(Sheet sheet, int rowNum, short cellNum)įor (int i = 0 i < sheet.getNumMergedRegions() i++)ĬellRangeAddress merged = sheet.getMergedRegion(i) MergedRegion.getLastRow(), mergedRegion.getFirstColumn(), mergedRegion.getLastColumn()) ĬellRangeAddressWrapper wrapper = new CellRangeAddressWrapper(dstMergedRegion) ĭstSheet_.addMergedRegion(wrapper.range) Short dh = srcSheet_.getDefaultRowHeight() įor ( j <= srcRow.getLastCellNum() j++)Ĭell srcCell = srcRow.getCell(j) // ancienne cellĬell dstCell = dstRow.getCell(j) // new cellĬellRangeAddress mergedRegion = getMergedRegion(srcSheet_, srcRow.getRowNum(),ĬellRangeAddress dstMergedRegion = new CellRangeAddress(mergedRegion.getFirstRow(), SrcToDstStyle_ = getCellStyle(srcCellStyle) ĬellStyle srcRowStyle = srcRow.getRowStyle() ĬellStyle dstRowStyle = getDstStyleFromSrcStyle(srcRowStyle) SrcToDstStyle_ = new CellStyle įor (int srcStyleIdx = 0 srcStyleIdx < totalSrcCellStyles ++srcStyleIdx)ĬellStyle srcCellStyle = srcWorkbook_.getCellStyleAt(srcStyleIdx) Int totalSrcCellStyles = srcWorkbook_.getNumCellStyles() Return srcToDstStyle_ įor (int i = srcSheet_.getFirstRowNum() i maxColumnNum)ĭstSheet_.setColumnWidth(i, srcSheet_.getColumnWidth(i)) ĬellStyle srcColStyle = srcSheet_.getColumnStyle(i) ĭstSheet_.setDefaultColumnStyle(i, getDstStyleFromSrcStyle(srcColStyle)) Private CellStyle getDstStyleFromSrcStyle(CellStyle srcStyle) PoiCopySheet poiCopySheet = new PoiCopySheet(srcSheet, dstSheet) Public static void copySheet(Sheet srcSheet, Sheet dstSheet) Private PoiCopySheet(Sheet srcSheet, Sheet dstSheet) Private Set mergedRegions_ = new TreeSet() Those could be added if you merge in the code from the coderanch post, but I didn’t take the time to do it.īelow is the java code to perform the Sheet copy using Apache POI 4.1.1 and Java 11: import The below code does not copy pictures to the new Sheet, nor does it copy print titles. pySheet(sourceSheet, destinationSheet) Ĭaveats: There are 2 things left out from the coderanch thread. Sheet destinationSheet = destinationWorkbook.createSheet("New Sheet Name") Create your destination sheet object, you may have a different sheet name Sheet sourceSheet = sourceWorkbook.getSheetAt(0) ![]() ![]() I found the code there to be broken into many different pieces, as well as having a handful of bugs and also using many deprecated (or not existing anymore) functions in Apache POI 4.1.1.Īfter fixing the bugs that impacted my use case, as well as quite a bit of refactoring for better readability and convenience, I have working java code for both XSSF and HSSF formats.īelow is an example of how to use this new code: // Get your sourceSheet object, your code may be different Various StackOverflow posts pointed to this thread on coderanch. I desired to have this copy include all styles, such as bold/underline/etc, preserve all merged cells, and preserve all Sheet properties. I recently wanted to copy an Excel worksheet using Apache POI from one workbook to another workbook.
0 Comments
Read More
Leave a Reply. |