Copying a sheet from an excel file to another excel file using Apache POI
Srikanth Kumar
Ranch Hand
Joined: Jun 04, 2008
Posts: 36
posted
0
Hey, I hava a requirement to copy from an excel file to another excel file where i need to copy an excel files sheet to another excel sheet. i'm able to achieve this using cell-to-cell copying but its consuming time. Is there any way to copy directly from sheet to sheet?
Srikanth Kumar
SCJP 5, SCWCD 5, SCDJWS 5
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 32767
posted
0
No, POI does not have such convenience methods.
Note that -if you end up writing code that copies a complete sheet, including the cell styles- this would make a good addition to POI, so you might think about donating that code to the project (or at least posting it here so that people can find it).
Thanks for reply Ulf. I have taken your word into consideration and tried to implement that. somehow i could able to achieve this(copying sheet-to-sheet). However it works with sheets that contains integers and decimals. If any cell contains varchar then its replacing it with #VALUE!. And while opening the merged file i'm getting an alert "File error: data may have been lost."
I'm posting the classes involved in this.
Here, book1 contains 1 sheet with integers and decimals and book2 contains varchar. i need create a merged file with two tabs numbers and varchars.
Anybody help on this. Thanks in advance. [ October 23, 2008: Message edited by: Martijn Verburg ]
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 32767
posted
0
I'm surprised it works like that at all (well, it seems it doesn't :-). I would have thought that one would need to create cells, cell contents and cell styles for each cell in each sheet. That's what I suggested.
Thanks for changing the display name, much appreciated! I just thought I'd point out a handy tip for you since you are new to Javaranch. You can UseCodeTags to make your code examples stand out more. I've done this for you in this instance.
Happy questioning!
Alex Krenvalk
Greenhorn
Joined: Dec 06, 2008
Posts: 5
posted
0
For work with excel files advise use-Excel file error: data may have been lost,because as far as i know tool is freeware,it save important information,such as graphics,statistic and mathematics,program will help you to recover valuable information and avoid its losses,tool scans your broken worksheet,then gets the data from this document,will help you to repair damaged files in Microsoft Excel sheet recognizable format,repair file Excel this file is not in a recognizable format, Excel showing this file does not in a recognizable format, or Microsoft Excel worksheet this file is not in a recognizeable format, it is an Excel file error: data may have been lost: Microsoft Excel impossible read file,tool performs a scan of your corrupt excel files not recognizable format and attempts to recover all available data.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 32767
posted
0
In the interest of full disclosure it should be noted that Alex Krenvalk seems somehow involved with the commercial tool he recommends.
Sivaraman Lakshmanan
Ranch Hand
Joined: Aug 02, 2003
Posts: 230
posted
0
Hi, Hope this code can help you in copying the sheets. Copy Sheets
Regards,
Sivaraman.L
Evgeniy Bulanov
Greenhorn
Joined: Dec 19, 2005
Posts: 23
posted
0
Thanks 4 all replays in the topic. (Personally to Srikanth Kumar and Sivaraman Lakshmanan)
I modify a bit code posted by Sivaraman Lakshmanan. Now it supports copying cellstyles.
I hope it will be helpful for somebody ;)
Computers are like air conditioners - They stop working properly when you open Windows
Ph. Loep
Greenhorn
Joined: Sep 11, 2010
Posts: 1
posted
0
Hi,
I found this old thread via Google and I am happy that the code posted by Evgeniy Bulanov works well.
But I modified the code a little bit to get rid of the deprecated classes and methods and changed the raw-types to generic-types. Now there are no warnings left and still it works fine!
I would like to copy a sheet as well, and I have done this with 1 VBA line, can it really be true that I need 100+ lines of JAVA to do this
Here is my VBA line:
Workbooks("source.xls").Sheets(1).Move Before:=Workbooks("Destination.xls").Sheets(2)
both files must be open, so that makes it 3 lines of code...
I started out using JACOB, but I could not understand the dispatch syntax, so I started looking at POI, so can any of you convert this single line of code, into POI, or JACOB?
the good thing about the VBA code, is that it takes everything including graphs...
It does seem strange that so much code is needed in the POI library - hopefully someone more knowledgeable than I can come up with an answer!
amanda jones
Greenhorn
Joined: Jul 22, 2005
Posts: 2
posted
0
Hi Ph. Loep
when you change the deprecated method you forget that CellRangeAddress does not implement Comparable so the method isNewMergedRegion does not work...
Thanks
Pierre Guilbert
Greenhorn
Joined: Apr 14, 2011
Posts: 1
posted
0
Hi,
As Amanda Jones pointed out, there is a problem. I spent some time this morning to fix some of it.
Maybe it's not pretty, but it's doing the job.
Plus you have to change this part in the copyRow method. CellRangeAddress constructor is called with the wrong arguments
Ps: It fixes some issues, but not all of them.
Ernesto Esteban
Greenhorn
Joined: May 02, 2011
Posts: 1
posted
1
Hi all,
I spent some days trying to get have something working fine, thanks to all your post. Here is what I've done :
and
I created a wrapper for the CellRangeAddress class that implements Comparable. This allows the set to work.
++
kins
hamza abd
Greenhorn
Joined: Jun 08, 2011
Posts: 1
posted
0
Ernesto Esteban wrote:Hi all,
I spent some days trying to get have something working fine, thanks to all your post. Here is what I've done :
and
I created a wrapper for the CellRangeAddress class that implements Comparable. This allows the set to work.
++
kins
Hello Ernesto,
Thank you very much for your code. I was able to copy one sheet to another. Do you know however how to preserve sheet aspect ?
my src sheet has 35% zoom and perfectly fits on an A4 page. Once copied into the new workbook it does not fit (zoom, print margins, row breaks all change)
Thanks for your help
omar kamil
Greenhorn
Joined: Sep 23, 2011
Posts: 2
posted
0
Hi,
i 'm getting this error when i copy a sheet from workbook (XSSF) to another workbook using poi3.7 and this util class.
While setting something like color for a whole row by pressing on the row number,
the "firstCellNum" is -1.
I have changed line 70 on class Util from:
for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) {
to:
int j = srcRow.getFirstCellNum();
if(j<0){j=0;}
for (; j <= srcRow.getLastCellNum(); j++) {
and the problem stoped.
fabio biscaro
Greenhorn
Joined: Mar 13, 2012
Posts: 1
posted
0
I found also another improvement to the code: if you copy rows in a different row number merged regions are still copied to the original row numbers.
I modified slightly the code adding "deltaRows"
Sartner huang
Greenhorn
Joined: Apr 19, 2012
Posts: 1
posted
0
There is some problems with the solution.
The Excel sheet render is with order;
when sheet1 finished rendering the sheet2 rendering is just started;
so
if sheet1 has a formula with relevance to sheet2. the formula will be #REF!
We need some extra code to refresh the formula when the whole excel is finished rendering
Here, I give a solution
We need a POJO to store the formula information,
like: sheet name , row index, cell index and the formula
and then
when we copy a cell to another sheet cell
if the cell type is HSSFCell.CELL_TYPE_FORMULA
we need to add the cell information to a collection as you like (of course you need to create a collection in the class)
OK~ excuse my poor English
I give the solution in the following
1. we need a POJO to save the formula infomation
I create a private inner static class named FormulaInfo
2. create a formulaInfo Collection in the class
3. in copyCell(), when you detected the cell is a FormulaType m you need to add informatiton to the formulaInfoList
4. after create the whole WorkBook, we need to refresh the formula
subject: Copying a sheet from an excel file to another excel file using Apache POI