emulate saveAs

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

emulate saveAs

Rob Sargent
How does one read in an excel file then "save as" new file?

I'm transforming xlsx input to a very different xlsx output. I open the
original, do my work in a new sheet, then save to a new file. 
Unfortunately, if the original is writable by me, then my new sheet gets
saved in the original file (and in my new file too).  If the original is
not writable by me I get a nasty little stack trace about cleaning up
open file(s).  My new file has all the sheets (original and additional)
as desired.

Thanks



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: emulate saveAs

kiwiwings
Hi Rob,

although this is more memory consuming, how about opening your template file via (File-)InputStream and using Workbook.write(new FileOutputStream(..))?

Andi

On 12.11.18 22:25, Rob Sargent wrote:

> How does one read in an excel file then "save as" new file?
>
> I'm transforming xlsx input to a very different xlsx output. I open the original, do my work in a new sheet, then save to a new file.  Unfortunately, if the original is writable by me, then my new sheet gets saved in the original file (and in my new file too).  If the original is not writable by me I get a nasty little stack trace about cleaning up open file(s).  My new file has all the sheets (original and additional) as desired.
>
> Thanks
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: emulate saveAs

Greg Woolsey
Or copy the original file first, with standard Java tools, then open, edit,
and write the copy with POI.

On Mon, Nov 12, 2018, 13:34 Andreas Beeker <[hidden email] wrote:

> Hi Rob,
>
> although this is more memory consuming, how about opening your template
> file via (File-)InputStream and using Workbook.write(new
> FileOutputStream(..))?
>
> Andi
>
> On 12.11.18 22:25, Rob Sargent wrote:
> > How does one read in an excel file then "save as" new file?
> >
> > I'm transforming xlsx input to a very different xlsx output. I open the
> original, do my work in a new sheet, then save to a new file.
> Unfortunately, if the original is writable by me, then my new sheet gets
> saved in the original file (and in my new file too).  If the original is
> not writable by me I get a nasty little stack trace about cleaning up open
> file(s).  My new file has all the sheets (original and additional) as
> desired.
> >
> > Thanks
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: emulate saveAs

Rob Sargent
I believe I’ll have to resort to that, thanks.  Is this not a missing feature?

> On Nov 12, 2018, at 8:03 PM, Greg Woolsey <[hidden email]> wrote:
>
> Or copy the original file first, with standard Java tools, then open, edit,
> and write the copy with POI.
>
> On Mon, Nov 12, 2018, 13:34 Andreas Beeker <[hidden email] wrote:
>
>> Hi Rob,
>>
>> although this is more memory consuming, how about opening your template
>> file via (File-)InputStream and using Workbook.write(new
>> FileOutputStream(..))?
>>
>> Andi
>>
>> On 12.11.18 22:25, Rob Sargent wrote:
>>> How does one read in an excel file then "save as" new file?
>>>
>>> I'm transforming xlsx input to a very different xlsx output. I open the
>> original, do my work in a new sheet, then save to a new file.
>> Unfortunately, if the original is writable by me, then my new sheet gets
>> saved in the original file (and in my new file too).  If the original is
>> not writable by me I get a nasty little stack trace about cleaning up open
>> file(s).  My new file has all the sheets (original and additional) as
>> desired.
>>>
>>> Thanks
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: emulate saveAs

Andreas Reichel
Does not SXSSF Workbook have a constructor based on an existing XSSF
(template) Workbook? This will allow to create a new SXSSF workbook,
add a sheet with content and then to write out the new file w/o
tampering with the existing template?
Best regardsAndreas
On Mon, 2018-11-12 at 20:54 -0700, Rob Sargent wrote:

> I believe I’ll have to resort to that, thanks.  Is this not a missing
> feature?
> > On Nov 12, 2018, at 8:03 PM, Greg Woolsey <[hidden email]>
> > wrote:
> > Or copy the original file first, with standard Java tools, then
> > open, edit,and write the copy with POI.
> > On Mon, Nov 12, 2018, 13:34 Andreas Beeker <[hidden email]
> > wrote:
> > > Hi Rob,
> > > although this is more memory consuming, how about opening your
> > > templatefile via (File-)InputStream and using
> > > Workbook.write(newFileOutputStream(..))?
> > > Andi
> > > On 12.11.18 22:25, Rob Sargent wrote:
> > > > How does one read in an excel file then "save as" new file?
> > > > I'm transforming xlsx input to a very different xlsx output. I
> > > > open the
> > > original, do my work in a new sheet, then save to a new
> > > file.Unfortunately, if the original is writable by me, then my
> > > new sheet getssaved in the original file (and in my new file
> > > too).  If the original isnot writable by me I get a nasty little
> > > stack trace about cleaning up openfile(s).  My new file has all
> > > the sheets (original and additional) asdesired.
> > > > Thanks
> > > >
> > > >
> > > > -------------------------------------------------------------
> > > > --------To unsubscribe, e-mail: [hidden email]
> > > > For additional commands, e-mail: [hidden email]
> > > >
> > > >
> > >
> > > ---------------------------------------------------------------
> > > ------To unsubscribe, e-mail: [hidden email]
> > > For additional commands, e-mail: [hidden email]
> > >
> > >
>
> -------------------------------------------------------------------
> --To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
Reply | Threaded
Open this post in threaded view
|

Re: emulate saveAs

Greg Woolsey
In reply to this post by Rob Sargent
I see your point now - in fact, POIXMLDocument line 234 inside
write(OutputStream) has this comment:

        //force all children to commit their changes into the underlying
OOXML Package
        // TODO Shouldn't they be committing to the new one instead?

which indicates someone at some point thought it strange that
write(OutputStream) also updates the original source, but only if the
workbook was created from a file.  I suspect that's a side effect of using
the Zip objects underneath for memory and performance when opening directly
from a File rather than an InputStream, and not a specifically implemented
"feature."

However, what the semantics _should_ be seems tricky - are you meaning
changes since opening should only be saved in the stream passed in?
Write() has other side effects specific to certain package part objects,
how should those be handled? Do we update the in-memory objects, but not
the original file?  How does that get updated?  Would we need a separate
save() no-arg method?  Would that be a no-op for stream based documents?
Should it throw a warning or exception when there is not original File to
update?

I'm not criticizing at all, more musing at the end of the day. I agree it's
a bit awkward, and having side effects that depend on original state is not
ideal.  At least the JavaDoc is accurate as far as it goes.

Patches and ideas always welcome, the project typically gets better mostly
in areas that bug people enough to get involved :)

On Mon, Nov 12, 2018, 19:54 Rob Sargent <[hidden email] wrote:

> I believe I’ll have to resort to that, thanks.  Is this not a missing
> feature?
>
> > On Nov 12, 2018, at 8:03 PM, Greg Woolsey <[hidden email]>
> wrote:
> >
> > Or copy the original file first, with standard Java tools, then open,
> edit,
> > and write the copy with POI.
> >
> > On Mon, Nov 12, 2018, 13:34 Andreas Beeker <[hidden email] wrote:
> >
> >> Hi Rob,
> >>
> >> although this is more memory consuming, how about opening your template
> >> file via (File-)InputStream and using Workbook.write(new
> >> FileOutputStream(..))?
> >>
> >> Andi
> >>
> >> On 12.11.18 22:25, Rob Sargent wrote:
> >>> How does one read in an excel file then "save as" new file?
> >>>
> >>> I'm transforming xlsx input to a very different xlsx output. I open the
> >> original, do my work in a new sheet, then save to a new file.
> >> Unfortunately, if the original is writable by me, then my new sheet gets
> >> saved in the original file (and in my new file too).  If the original is
> >> not writable by me I get a nasty little stack trace about cleaning up
> open
> >> file(s).  My new file has all the sheets (original and additional) as
> >> desired.
> >>>
> >>> Thanks
> >>>
> >>>
> >>>
> >>> ---------------------------------------------------------------------
> >>> To unsubscribe, e-mail: [hidden email]
> >>> For additional commands, e-mail: [hidden email]
> >>>
> >>>
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: [hidden email]
> >> For additional commands, e-mail: [hidden email]
> >>
> >>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>