Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include <sfx2/docfile.hxx>
31 : : #include <sfx2/objsh.hxx>
32 : : #include <sfx2/app.hxx>
33 : : #include <sfx2/frame.hxx>
34 : : #include <sfx2/request.hxx>
35 : : #include <sot/storage.hxx>
36 : : #include <sot/exchange.hxx>
37 : : #include <tools/globname.hxx>
38 : : #include <comphelper/mediadescriptor.hxx>
39 : : #include <comphelper/processfactory.hxx>
40 : : #include <com/sun/star/beans/NamedValue.hpp>
41 : : #include <com/sun/star/document/XFilter.hpp>
42 : : #include <com/sun/star/document/XImporter.hpp>
43 : : #include "scitems.hxx"
44 : : #include <svl/stritem.hxx>
45 : : #include "filter.hxx"
46 : : #include "document.hxx"
47 : : #include "xistream.hxx"
48 : :
49 : : #include "scerrors.hxx"
50 : : #include "root.hxx"
51 : : #include "imp_op.hxx"
52 : : #include "excimp8.hxx"
53 : : #include "exp_op.hxx"
54 : :
55 : :
56 : 58 : FltError ScFormatFilterPluginImpl::ScImportExcel( SfxMedium& rMedium, ScDocument* pDocument, const EXCIMPFORMAT eFormat )
57 : : {
58 : : // check the passed Calc document
59 : : OSL_ENSURE( pDocument, "::ScImportExcel - no document" );
60 [ - + ]: 58 : if( !pDocument ) return eERR_INTERN; // should not happen
61 : :
62 : : /* Import all BIFF versions regardless on eFormat, needed for import of
63 : : external cells (file type detection returns Excel4.0). */
64 [ + - ][ + - ]: 58 : if( (eFormat != EIF_AUTO) && (eFormat != EIF_BIFF_LE4) && (eFormat != EIF_BIFF5) && (eFormat != EIF_BIFF8) )
[ + - ][ - + ]
65 : : {
66 : : OSL_FAIL( "::ScImportExcel - wrong file format specification" );
67 : 0 : return eERR_FORMAT;
68 : : }
69 : :
70 : : // check the input stream from medium
71 [ + - ]: 58 : SvStream* pMedStrm = rMedium.GetInStream();
72 : : OSL_ENSURE( pMedStrm, "::ScImportExcel - medium without input stream" );
73 [ - + ]: 58 : if( !pMedStrm ) return eERR_OPEN; // should not happen
74 : :
75 : 58 : SvStream* pBookStrm = 0; // The "Book"/"Workbook" stream containing main data.
76 : 58 : XclBiff eBiff = EXC_BIFF_UNKNOWN; // The BIFF version of the main stream.
77 : :
78 : : // try to open an OLE storage
79 : 58 : SotStorageRef xRootStrg;
80 : 58 : SotStorageStreamRef xStrgStrm;
81 [ + - ][ + - ]: 58 : if( SotStorage::IsStorageFile( pMedStrm ) )
82 : : {
83 [ + - ][ + - ]: 58 : xRootStrg = new SotStorage( pMedStrm, false );
[ + - ]
84 [ + + ]: 58 : if( xRootStrg->GetError() )
85 [ + - ]: 3 : xRootStrg = 0;
86 : : }
87 : :
88 : : // try to open "Book" or "Workbook" stream in OLE storage
89 [ + + ]: 58 : if( xRootStrg.Is() )
90 : : {
91 : : // try to open the "Book" stream
92 [ + - ][ + - ]: 55 : SotStorageStreamRef xBookStrm = ScfTools::OpenStorageStreamRead( xRootStrg, EXC_STREAM_BOOK );
[ + - ][ + - ]
93 [ - + ][ # # ]: 55 : XclBiff eBookBiff = xBookStrm.Is() ? XclImpStream::DetectBiffVersion( *xBookStrm ) : EXC_BIFF_UNKNOWN;
94 : :
95 : : // try to open the "Workbook" stream
96 [ + - ][ + - ]: 55 : SotStorageStreamRef xWorkbookStrm = ScfTools::OpenStorageStreamRead( xRootStrg, EXC_STREAM_WORKBOOK );
[ + - ][ + - ]
97 [ + - ][ + - ]: 55 : XclBiff eWorkbookBiff = xWorkbookStrm.Is() ? XclImpStream::DetectBiffVersion( *xWorkbookStrm ) : EXC_BIFF_UNKNOWN;
98 : :
99 : : // decide which stream to use
100 [ + - ][ - + ]: 55 : if( (eWorkbookBiff != EXC_BIFF_UNKNOWN) && ((eBookBiff == EXC_BIFF_UNKNOWN) || (eWorkbookBiff > eBookBiff)) )
[ # # ]
101 : : {
102 : : /* Only "Workbook" stream exists; or both streams exist,
103 : : and "Workbook" has higher BIFF version than "Book" stream. */
104 [ + - ]: 55 : xStrgStrm = xWorkbookStrm;
105 : 55 : eBiff = eWorkbookBiff;
106 : : }
107 [ # # ]: 0 : else if( eBookBiff != EXC_BIFF_UNKNOWN )
108 : : {
109 : : /* Only "Book" stream exists; or both streams exist,
110 : : and "Book" has higher BIFF version than "Workbook" stream. */
111 [ # # ]: 0 : xStrgStrm = xBookStrm;
112 : 0 : eBiff = eBookBiff;
113 : : }
114 : :
115 [ + - ][ + - ]: 55 : pBookStrm = xStrgStrm;
116 : : }
117 : :
118 : : // no "Book" or "Workbook" stream found, try plain input stream from medium (even for BIFF5+)
119 [ + + ]: 58 : if( !pBookStrm )
120 : : {
121 [ + - ]: 3 : eBiff = XclImpStream::DetectBiffVersion( *pMedStrm );
122 [ - + ]: 3 : if( eBiff != EXC_BIFF_UNKNOWN )
123 : 0 : pBookStrm = pMedStrm;
124 : : }
125 : :
126 : : // try to import the file
127 : 58 : FltError eRet = eERR_UNKN_BIFF;
128 [ + + ]: 58 : if( pBookStrm )
129 : : {
130 [ + - ]: 55 : pBookStrm->SetBufferSize( 0x8000 ); // still needed?
131 : :
132 [ + - ][ + - ]: 55 : XclImpRootData aImpData( eBiff, rMedium, xRootStrg, *pDocument, RTL_TEXTENCODING_MS_1252 );
133 : 55 : ::std::auto_ptr< ImportExcel > xFilter;
134 [ - + - ]: 55 : switch( eBiff )
135 : : {
136 : : case EXC_BIFF2:
137 : : case EXC_BIFF3:
138 : : case EXC_BIFF4:
139 : : case EXC_BIFF5:
140 [ # # ][ # # ]: 0 : xFilter.reset( new ImportExcel( aImpData, *pBookStrm ) );
141 : 0 : break;
142 : : case EXC_BIFF8:
143 [ + - ][ + - ]: 55 : xFilter.reset( new ImportExcel8( aImpData, *pBookStrm ) );
144 : 55 : break;
145 : : default: DBG_ERROR_BIFF();
146 : : }
147 : :
148 [ + - ][ + - ]: 55 : eRet = xFilter.get() ? xFilter->Read() : eERR_INTERN;
[ + - ][ + - ]
149 : : }
150 : :
151 [ + - ][ + - ]: 58 : return eRet;
152 : : }
153 : :
154 : :
155 : 0 : static FltError lcl_ExportExcelBiff( SfxMedium& rMedium, ScDocument *pDocument,
156 : : SvStream* pMedStrm, sal_Bool bBiff8, CharSet eNach )
157 : : {
158 : : // try to open an OLE storage
159 [ # # ][ # # ]: 0 : SotStorageRef xRootStrg = new SotStorage( pMedStrm, false );
160 [ # # ]: 0 : if( xRootStrg->GetError() ) return eERR_OPEN;
161 : :
162 : : // create BIFF dependent strings
163 [ # # ][ # # ]: 0 : String aStrmName, aClipName, aClassName;
[ # # ]
164 [ # # ]: 0 : if( bBiff8 )
165 : : {
166 [ # # ][ # # ]: 0 : aStrmName = EXC_STREAM_WORKBOOK;
[ # # ]
167 [ # # ][ # # ]: 0 : aClipName = CREATE_STRING( "Biff8" );
[ # # ]
168 [ # # ][ # # ]: 0 : aClassName = CREATE_STRING( "Microsoft Excel 97-Tabelle" );
[ # # ]
169 : : }
170 : : else
171 : : {
172 [ # # ][ # # ]: 0 : aStrmName = EXC_STREAM_BOOK;
[ # # ]
173 [ # # ][ # # ]: 0 : aClipName = CREATE_STRING( "Biff5" );
[ # # ]
174 [ # # ][ # # ]: 0 : aClassName = CREATE_STRING( "Microsoft Excel 5.0-Tabelle" );
[ # # ]
175 : : }
176 : :
177 : : // open the "Book"/"Workbook" stream
178 [ # # ][ # # ]: 0 : SotStorageStreamRef xStrgStrm = ScfTools::OpenStorageStreamWrite( xRootStrg, aStrmName );
179 [ # # ][ # # ]: 0 : if( !xStrgStrm.Is() || xStrgStrm->GetError() ) return eERR_OPEN;
[ # # ]
180 : :
181 [ # # ]: 0 : xStrgStrm->SetBufferSize( 0x8000 ); // still needed?
182 : :
183 : 0 : FltError eRet = eERR_UNKN_BIFF;
184 [ # # ][ # # ]: 0 : XclExpRootData aExpData( bBiff8 ? EXC_BIFF8 : EXC_BIFF5, rMedium, xRootStrg, *pDocument, eNach );
[ # # ]
185 [ # # ]: 0 : if ( bBiff8 )
186 : : {
187 [ # # ]: 0 : ExportBiff8 aFilter( aExpData, *xStrgStrm );
188 [ # # ][ # # ]: 0 : eRet = aFilter.Write();
189 : : }
190 : : else
191 : : {
192 [ # # ]: 0 : ExportBiff5 aFilter( aExpData, *xStrgStrm );
193 [ # # ][ # # ]: 0 : eRet = aFilter.Write();
194 : : }
195 : :
196 [ # # ]: 0 : if( eRet == eERR_RNGOVRFLW )
197 : 0 : eRet = SCWARN_EXPORT_MAXROW;
198 : :
199 [ # # ]: 0 : SvGlobalName aGlobName( 0x00020810, 0x0000, 0x0000, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 );
200 [ # # ]: 0 : sal_uInt32 nClip = SotExchange::RegisterFormatName( aClipName );
201 [ # # ]: 0 : xRootStrg->SetClass( aGlobName, nClip, aClassName );
202 : :
203 [ # # ]: 0 : xStrgStrm->Commit();
204 [ # # ]: 0 : xRootStrg->Commit();
205 : :
206 [ # # ][ # # ]: 0 : return eRet;
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
207 : : }
208 : :
209 : 0 : FltError ScFormatFilterPluginImpl::ScExportExcel5( SfxMedium& rMedium, ScDocument *pDocument,
210 : : ExportFormatExcel eFormat, CharSet eNach )
211 : : {
212 [ # # ][ # # ]: 0 : if( eFormat != ExpBiff5 && eFormat != ExpBiff8 )
213 : 0 : return eERR_NI;
214 : :
215 : : // check the passed Calc document
216 : : OSL_ENSURE( pDocument, "::ScImportExcel - no document" );
217 [ # # ]: 0 : if( !pDocument ) return eERR_INTERN; // should not happen
218 : :
219 : : // check the output stream from medium
220 : 0 : SvStream* pMedStrm = rMedium.GetOutStream();
221 : : OSL_ENSURE( pMedStrm, "::ScExportExcel5 - medium without output stream" );
222 [ # # ]: 0 : if( !pMedStrm ) return eERR_OPEN; // should not happen
223 : :
224 : 0 : FltError eRet = eERR_UNKN_BIFF;
225 [ # # ][ # # ]: 0 : if( eFormat == ExpBiff5 || eFormat == ExpBiff8 )
226 : 0 : eRet = lcl_ExportExcelBiff( rMedium, pDocument, pMedStrm, eFormat == ExpBiff8, eNach );
227 : :
228 : 0 : return eRet;
229 [ + - ][ + - ]: 24 : }
230 : :
231 : :
232 : :
233 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|