![]() ![]() ![]() 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
Leave a Reply. |