1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <excelfilter.hxx>
22 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
23 #include <osl/diagnose.h>
24 #include <sal/log.hxx>
26 #include <excelvbaproject.hxx>
27 #include <stylesbuffer.hxx>
28 #include <themebuffer.hxx>
29 #include <workbookfragment.hxx>
30 #include <xestream.hxx>
32 #include <addressconverter.hxx>
33 #include <document.hxx>
35 #include <scerrors.hxx>
36 #include <vcl/svapp.hxx>
37 #include <vcl/weld.hxx>
38 #include <svtools/sfxecode.hxx>
39 #include <svtools/ehdl.hxx>
40 #include <tools/urlobj.hxx>
41 #include <tools/diagnose_ex.h>
46 using namespace ::com::sun::star::lang
;
47 using namespace ::com::sun::star::sheet
;
48 using namespace ::com::sun::star::uno
;
49 using namespace ::oox::core
;
51 using ::oox::drawingml::table::TableStyleListPtr
;
53 ExcelFilter::ExcelFilter( const Reference
< XComponentContext
>& rxContext
) :
54 XmlFilterBase( rxContext
),
59 ExcelFilter::~ExcelFilter()
61 OSL_ENSURE( !mpBookGlob
, "ExcelFilter::~ExcelFilter - workbook data not cleared" );
64 void ExcelFilter::registerWorkbookGlobals( WorkbookGlobals
& rBookGlob
)
66 mpBookGlob
= &rBookGlob
;
69 WorkbookGlobals
& ExcelFilter::getWorkbookGlobals() const
71 OSL_ENSURE( mpBookGlob
, "ExcelFilter::getWorkbookGlobals - missing workbook data" );
75 void ExcelFilter::unregisterWorkbookGlobals()
80 bool ExcelFilter::importDocument()
82 /* To activate the XLSX/XLSB dumper, insert the full path to the file
83 file:///<path-to-oox-module>/source/dump/xlsbdumper.ini
84 into the environment variable OOO_XLSBDUMPER and start the office with
85 this variable (nonpro only). */
86 //OOX_DUMP_FILE( ::oox::dump::xlsb::Dumper );
88 OUString aWorkbookPath
= getFragmentPathFromFirstTypeFromOfficeDoc( "officeDocument" );
89 if( aWorkbookPath
.isEmpty() )
96 importDocumentProperties();
98 catch( const Exception
& )
100 TOOLS_WARN_EXCEPTION("sc", "exception when importing document properties");
104 SAL_WARN("sc", "exception when importing document properties");
106 /* Construct the WorkbookGlobals object referred to by every instance of
107 the class WorkbookHelper, and execute the import filter by constructing
108 an instance of WorkbookFragment and loading the file. */
109 WorkbookGlobalsRef
xBookGlob(WorkbookHelper::constructGlobals(*this));
112 rtl::Reference
<FragmentHandler
> xWorkbookFragment( new WorkbookFragment(*xBookGlob
, aWorkbookPath
));
113 bool bRet
= importFragment( xWorkbookFragment
);
116 const WorkbookFragment
* pWF
= static_cast<const WorkbookFragment
*>(xWorkbookFragment
.get());
117 const AddressConverter
& rAC
= pWF
->getAddressConverter();
118 if (rAC
.isTabOverflow() || rAC
.isColOverflow() || rAC
.isRowOverflow())
120 const ScDocument
& rDoc
= pWF
->getScDocument();
121 if (rDoc
.IsUserInteractionEnabled())
123 // Show data loss warning.
125 INetURLObject
aURL( getFileUrl());
126 SfxErrorContext
aContext( ERRCTX_SFX_OPENDOC
,
127 aURL
.getName( INetURLObject::LAST_SEGMENT
, true,
128 INetURLObject::DecodeMechanism::WithCharset
),
129 nullptr, RID_ERRCTX
);
132 aContext
.GetString( ERRCODE_NONE
.MakeWarning(), aWarning
);
136 if (rAC
.isTabOverflow())
138 if (ErrorHandler::GetErrorString( SCWARN_IMPORT_SHEET_OVERFLOW
, aMsg
))
141 if (rAC
.isColOverflow())
145 if (ErrorHandler::GetErrorString( SCWARN_IMPORT_COLUMN_OVERFLOW
, aMsg
))
148 if (rAC
.isRowOverflow())
152 if (ErrorHandler::GetErrorString( SCWARN_IMPORT_ROW_OVERFLOW
, aMsg
))
156 /* XXX displaying a dialog here is ugly and should
157 * rather happen at UI level instead of at the filter
158 * level, but it seems there's no way to transport
159 * detailed information other than returning true or
160 * false at this point? */
162 std::unique_ptr
<weld::MessageDialog
> xWarn(Application::CreateMessageDialog(ScDocShell::GetActiveDialogParent(),
163 VclMessageType::Warning
, VclButtonsType::Ok
,
179 bool ExcelFilter::exportDocument() throw()
184 const ::oox::drawingml::Theme
* ExcelFilter::getCurrentTheme() const
186 return &WorkbookHelper( getWorkbookGlobals() ).getTheme();
189 ::oox::vml::Drawing
* ExcelFilter::getVmlDrawing()
194 TableStyleListPtr
ExcelFilter::getTableStyles()
196 return TableStyleListPtr();
199 ::oox::drawingml::chart::ChartConverter
* ExcelFilter::getChartConverter()
201 return WorkbookHelper( getWorkbookGlobals() ).getChartConverter();
204 void ExcelFilter::useInternalChartDataTable( bool bInternal
)
206 return WorkbookHelper( getWorkbookGlobals() ).useInternalChartDataTable( bInternal
);
209 GraphicHelper
* ExcelFilter::implCreateGraphicHelper() const
211 return new ExcelGraphicHelper( getWorkbookGlobals() );
214 ::oox::ole::VbaProject
* ExcelFilter::implCreateVbaProject() const
216 return new ExcelVbaProject( getComponentContext(), Reference
< XSpreadsheetDocument
>( getModel(), UNO_QUERY
) );
219 sal_Bool SAL_CALL
ExcelFilter::filter( const css::uno::Sequence
< css::beans::PropertyValue
>& rDescriptor
)
221 if ( XmlFilterBase::filter( rDescriptor
) )
224 if ( isExportFilter() )
226 bool bExportVBA
= exportVBA();
227 Reference
< XExporter
> xExporter(
228 new XclExpXmlStream( getComponentContext(), bExportVBA
, isExportTemplate() ) );
230 Reference
< XComponent
> xDocument
= getModel();
231 Reference
< XFilter
> xFilter( xExporter
, UNO_QUERY
);
235 xExporter
->setSourceDocument( xDocument
);
236 if ( xFilter
->filter( rDescriptor
) )
244 OUString
ExcelFilter::getImplementationName()
246 return "com.sun.star.comp.oox.xls.ExcelFilter";
253 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
254 com_sun_star_comp_oox_xls_ExcelFilter_get_implementation(css::uno::XComponentContext
* context
,
255 css::uno::Sequence
<css::uno::Any
> const &)
257 return cppu::acquire(new oox::xls::ExcelFilter(context
));
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */