2008-02-24 13:18:34 +00:00
|
|
|
{
|
|
|
|
fpsopendocument.pas
|
|
|
|
|
|
|
|
Writes an OpenDocument 1.0 Spreadsheet document
|
|
|
|
|
|
|
|
An OpenDocument document is a compressed ZIP file with the following files inside:
|
|
|
|
|
2009-01-29 11:30:38 +00:00
|
|
|
content.xml - Actual contents
|
|
|
|
meta.xml - Authoring data
|
|
|
|
settings.xml - User persistent viewing information, such as zoom, cursor position, etc.
|
|
|
|
styles.xml - Styles, which are the only way to do formatting
|
|
|
|
mimetype - application/vnd.oasis.opendocument.spreadsheet
|
2009-02-02 09:58:51 +00:00
|
|
|
META-INF\manifest.xml - Describes the other files in the archive
|
2008-02-24 13:18:34 +00:00
|
|
|
|
|
|
|
Specifications obtained from:
|
|
|
|
|
2009-01-28 22:36:41 +00:00
|
|
|
http://docs.oasis-open.org/office/v1.1/OS/OpenDocument-v1.1.pdf
|
2008-02-24 13:18:34 +00:00
|
|
|
|
2009-07-01 11:26:56 +00:00
|
|
|
AUTHORS: Felipe Monteiro de Carvalho / Jose Luis Jurado Rincon
|
2008-02-24 13:18:34 +00:00
|
|
|
}
|
2014-06-01 13:08:14 +00:00
|
|
|
|
|
|
|
|
2008-02-24 13:18:34 +00:00
|
|
|
unit fpsopendocument;
|
|
|
|
|
|
|
|
{$ifdef fpc}
|
|
|
|
{$mode delphi}
|
|
|
|
{$endif}
|
|
|
|
|
2014-04-08 09:53:02 +00:00
|
|
|
{.$define FPSPREADDEBUG} //used to be XLSDEBUG
|
2008-02-24 13:18:34 +00:00
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2009-02-02 09:58:51 +00:00
|
|
|
Classes, SysUtils,
|
2013-12-07 13:42:22 +00:00
|
|
|
{$IFDEF FPC_FULLVERSION >= 20701}
|
|
|
|
zipper,
|
|
|
|
{$ELSE}
|
|
|
|
fpszipper,
|
|
|
|
{$ENDIF}
|
2009-07-01 11:26:56 +00:00
|
|
|
fpspreadsheet,
|
2011-05-29 17:42:56 +00:00
|
|
|
xmlread, DOM, AVL_Tree,
|
|
|
|
math,
|
2014-03-23 11:36:36 +00:00
|
|
|
dateutils,
|
2011-05-29 17:42:56 +00:00
|
|
|
fpsutils;
|
2008-02-24 13:18:34 +00:00
|
|
|
|
|
|
|
type
|
2014-03-26 10:26:43 +00:00
|
|
|
TDateMode=(dm1899 {default for ODF; almost same as Excel 1900},
|
|
|
|
dm1900 {StarCalc legacy only},
|
|
|
|
dm1904 {e.g. Quattro Pro,Mac Excel compatibility}
|
|
|
|
);
|
2008-02-24 13:18:34 +00:00
|
|
|
|
2014-05-14 23:17:46 +00:00
|
|
|
{ TsSpreadOpenDocNumFormatList }
|
|
|
|
TsSpreadOpenDocNumFormatList = class(TsCustomNumFormatList)
|
|
|
|
protected
|
|
|
|
procedure AddBuiltinFormats; override;
|
|
|
|
public
|
|
|
|
// function FormatStringForWriting(AIndex: Integer): String; override;
|
|
|
|
end;
|
|
|
|
|
2009-07-01 11:26:56 +00:00
|
|
|
{ TsSpreadOpenDocReader }
|
|
|
|
|
|
|
|
TsSpreadOpenDocReader = class(TsCustomSpreadReader)
|
|
|
|
private
|
2014-05-28 16:23:50 +00:00
|
|
|
FCellStyleList: TFPList;
|
|
|
|
FColumnStyleList: TFPList;
|
|
|
|
FColumnList: TFPList;
|
2014-05-31 21:04:53 +00:00
|
|
|
FRowStyleList: TFPList;
|
|
|
|
FRowList: TFPList;
|
2014-06-03 22:04:11 +00:00
|
|
|
FVolatileNumFmtList: TsCustomNumFormatList;
|
2014-03-26 10:26:43 +00:00
|
|
|
FDateMode: TDateMode;
|
2014-05-31 21:04:53 +00:00
|
|
|
// Applies internally stored column widths to current worksheet
|
|
|
|
procedure ApplyColWidths;
|
2014-05-26 22:27:07 +00:00
|
|
|
// Applies a style to a cell
|
|
|
|
procedure ApplyStyleToCell(ARow, ACol: Cardinal; AStyleName: String);
|
2014-06-02 22:07:40 +00:00
|
|
|
function ExtractDateTimeFromNode(ANode: TDOMNode): TDateTime;
|
2014-05-26 22:27:07 +00:00
|
|
|
// Searches a style by its name in the StyleList
|
2014-05-28 16:23:50 +00:00
|
|
|
function FindCellStyleByName(AStyleName: String): integer;
|
|
|
|
// Searches a column style by its column index or its name in the StyleList
|
|
|
|
function FindColumnByCol(AColIndex: Integer): Integer;
|
|
|
|
function FindColStyleByName(AStyleName: String): integer;
|
2014-05-31 21:04:53 +00:00
|
|
|
function FindRowStyleByName(AStyleName: String): Integer;
|
2014-03-23 11:36:36 +00:00
|
|
|
// Gets value for the specified attribute. Returns empty string if attribute
|
|
|
|
// not found.
|
2009-07-01 11:26:56 +00:00
|
|
|
function GetAttrValue(ANode : TDOMNode; AAttrName : string) : string;
|
2014-05-28 18:58:55 +00:00
|
|
|
procedure ReadColumns(ATableNode: TDOMNode);
|
2014-05-31 21:04:53 +00:00
|
|
|
procedure ReadColumnStyle(AStyleNode: TDOMNode);
|
2014-05-20 12:36:07 +00:00
|
|
|
// Figures out the base year for times in this file (dates are unambiguous)
|
2014-03-26 10:26:43 +00:00
|
|
|
procedure ReadDateMode(SpreadSheetNode: TDOMNode);
|
2014-05-31 21:04:53 +00:00
|
|
|
procedure ReadRowsAndCells(ATableNode: TDOMNode);
|
|
|
|
procedure ReadRowStyle(AStyleNode: TDOMNode);
|
2014-04-21 11:30:22 +00:00
|
|
|
protected
|
2014-05-14 23:17:46 +00:00
|
|
|
procedure CreateNumFormatList; override;
|
2014-05-26 08:03:36 +00:00
|
|
|
procedure ReadNumFormats(AStylesNode: TDOMNode);
|
2014-05-26 22:27:07 +00:00
|
|
|
procedure ReadStyles(AStylesNode: TDOMNode);
|
2009-07-01 11:26:56 +00:00
|
|
|
{ Record writing methods }
|
2014-05-27 13:09:23 +00:00
|
|
|
procedure ReadBlank(ARow, ACol: Word; ACellNode: TDOMNode);
|
2014-06-02 22:07:40 +00:00
|
|
|
procedure ReadDateTime(ARow : Word; ACol : Word; ACellNode: TDOMNode);
|
2009-07-01 11:26:56 +00:00
|
|
|
procedure ReadFormula(ARow : Word; ACol : Word; ACellNode: TDOMNode);
|
|
|
|
procedure ReadLabel(ARow : Word; ACol : Word; ACellNode: TDOMNode);
|
|
|
|
procedure ReadNumber(ARow : Word; ACol : Word; ACellNode: TDOMNode);
|
2014-04-21 11:30:22 +00:00
|
|
|
public
|
|
|
|
{ General reading methods }
|
2014-05-26 22:27:07 +00:00
|
|
|
constructor Create(AWorkbook: TsWorkbook); override;
|
|
|
|
destructor Destroy; override;
|
2014-04-21 11:30:22 +00:00
|
|
|
procedure ReadFromFile(AFileName: string; AData: TsWorkbook); override;
|
2009-07-01 11:26:56 +00:00
|
|
|
end;
|
|
|
|
|
2008-02-24 13:18:34 +00:00
|
|
|
{ TsSpreadOpenDocWriter }
|
|
|
|
|
|
|
|
TsSpreadOpenDocWriter = class(TsCustomSpreadWriter)
|
2014-05-28 19:26:08 +00:00
|
|
|
private
|
2014-05-31 21:04:53 +00:00
|
|
|
FColumnStyleList: TFPList;
|
|
|
|
FRowStyleList: TFPList;
|
|
|
|
|
|
|
|
// Routines to write parts of files
|
|
|
|
function WriteCellStylesXMLAsString: string;
|
|
|
|
function WriteColStylesXMLAsString: String;
|
|
|
|
function WriteRowStylesXMLAsString: String;
|
|
|
|
|
|
|
|
function WriteColumnsXMLAsString(ASheet: TsWorksheet): String;
|
|
|
|
function WriteRowsAndCellsXMLAsString(ASheet: TsWorksheet): String;
|
|
|
|
|
2014-05-28 19:26:08 +00:00
|
|
|
function WriteBackgroundColorStyleXMLAsString(const AFormat: TCell): String;
|
|
|
|
function WriteBorderStyleXMLAsString(const AFormat: TCell): String;
|
2014-05-28 20:52:36 +00:00
|
|
|
function WriteHorAlignmentStyleXMLAsString(const AFormat: TCell): String;
|
2014-05-28 19:26:08 +00:00
|
|
|
function WriteTextRotationStyleXMLAsString(const AFormat: TCell): String;
|
2014-05-28 20:52:36 +00:00
|
|
|
function WriteVertAlignmentStyleXMLAsString(const AFormat: TCell): String;
|
2014-05-28 19:26:08 +00:00
|
|
|
function WriteWordwrapStyleXMLAsString(const AFormat: TCell): String;
|
2014-05-31 21:04:53 +00:00
|
|
|
|
2008-02-24 13:18:34 +00:00
|
|
|
protected
|
2012-04-27 08:01:15 +00:00
|
|
|
FPointSeparatorSettings: TFormatSettings;
|
2009-01-28 22:36:41 +00:00
|
|
|
// Strings with the contents of files
|
2014-05-31 21:04:53 +00:00
|
|
|
FMeta, FSettings, FStyles, FContent, FCellContent, FMimetype: string;
|
2009-01-28 22:36:41 +00:00
|
|
|
FMetaInfManifest: string;
|
2009-02-02 09:58:51 +00:00
|
|
|
// Streams with the contents of files
|
|
|
|
FSMeta, FSSettings, FSStyles, FSContent, FSMimetype: TStringStream;
|
|
|
|
FSMetaInfManifest: TStringStream;
|
2014-05-14 23:17:46 +00:00
|
|
|
// Helpers
|
|
|
|
procedure CreateNumFormatList; override;
|
2014-05-31 21:04:53 +00:00
|
|
|
procedure ListAllColumnStyles;
|
|
|
|
procedure ListAllRowStyles;
|
2009-01-28 22:36:41 +00:00
|
|
|
// Routines to write those files
|
2010-12-08 10:24:15 +00:00
|
|
|
procedure WriteMimetype;
|
|
|
|
procedure WriteMetaInfManifest;
|
|
|
|
procedure WriteMeta;
|
|
|
|
procedure WriteSettings;
|
|
|
|
procedure WriteStyles;
|
2014-04-23 22:29:32 +00:00
|
|
|
procedure WriteContent;
|
2009-01-29 11:30:38 +00:00
|
|
|
procedure WriteWorksheet(CurSheet: TsWorksheet);
|
2014-04-21 11:30:22 +00:00
|
|
|
{ Record writing methods }
|
2014-04-21 21:43:43 +00:00
|
|
|
procedure WriteBlank(AStream: TStream; const ARow, ACol: Cardinal;
|
|
|
|
ACell: PCell); override;
|
|
|
|
procedure WriteFormula(AStream: TStream; const ARow, ACol: Cardinal;
|
|
|
|
const AFormula: TsFormula; ACell: PCell); override;
|
|
|
|
procedure WriteLabel(AStream: TStream; const ARow, ACol: Cardinal;
|
|
|
|
const AValue: string; ACell: PCell); override;
|
|
|
|
procedure WriteNumber(AStream: TStream; const ARow, ACol: Cardinal;
|
|
|
|
const AValue: double; ACell: PCell); override;
|
|
|
|
procedure WriteDateTime(AStream: TStream; const ARow, ACol: Cardinal;
|
|
|
|
const AValue: TDateTime; ACell: PCell); override;
|
2008-02-24 13:18:34 +00:00
|
|
|
public
|
2014-04-23 22:29:32 +00:00
|
|
|
constructor Create(AWorkbook: TsWorkbook); override;
|
2014-05-31 21:04:53 +00:00
|
|
|
destructor Destroy; override;
|
2008-02-24 13:18:34 +00:00
|
|
|
{ General writing methods }
|
2009-02-02 09:58:51 +00:00
|
|
|
procedure WriteStringToFile(AString, AFileName: string);
|
2014-04-23 22:29:32 +00:00
|
|
|
procedure WriteToFile(const AFileName: string;
|
2009-11-08 19:21:23 +00:00
|
|
|
const AOverwriteExisting: Boolean = False); override;
|
2014-04-23 22:29:32 +00:00
|
|
|
procedure WriteToStream(AStream: TStream); override;
|
2008-02-24 13:18:34 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2014-05-25 22:05:37 +00:00
|
|
|
uses
|
|
|
|
StrUtils;
|
|
|
|
|
2008-02-24 13:18:34 +00:00
|
|
|
const
|
|
|
|
{ OpenDocument general XML constants }
|
|
|
|
XML_HEADER = '<?xml version="1.0" encoding="utf-8" ?>';
|
|
|
|
|
|
|
|
{ OpenDocument Directory structure constants }
|
2009-02-02 09:58:51 +00:00
|
|
|
OPENDOC_PATH_CONTENT = 'content.xml';
|
|
|
|
OPENDOC_PATH_META = 'meta.xml';
|
|
|
|
OPENDOC_PATH_SETTINGS = 'settings.xml';
|
|
|
|
OPENDOC_PATH_STYLES = 'styles.xml';
|
|
|
|
OPENDOC_PATH_MIMETYPE = 'mimetype';
|
2014-06-01 18:59:29 +00:00
|
|
|
OPENDOC_PATH_METAINF = 'META-INF' + '/';
|
2009-04-18 01:31:09 +00:00
|
|
|
OPENDOC_PATH_METAINF_MANIFEST = 'META-INF' + '/' + 'manifest.xml';
|
2008-02-24 13:18:34 +00:00
|
|
|
|
|
|
|
{ OpenDocument schemas constants }
|
|
|
|
SCHEMAS_XMLNS_OFFICE = 'urn:oasis:names:tc:opendocument:xmlns:office:1.0';
|
|
|
|
SCHEMAS_XMLNS_DCTERMS = 'http://purl.org/dc/terms/';
|
|
|
|
SCHEMAS_XMLNS_META = 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0';
|
|
|
|
SCHEMAS_XMLNS = 'http://schemas.openxmlformats.org/officeDocument/2006/extended-properties';
|
|
|
|
SCHEMAS_XMLNS_CONFIG = 'urn:oasis:names:tc:opendocument:xmlns:config:1.0';
|
|
|
|
SCHEMAS_XMLNS_OOO = 'http://openoffice.org/2004/office';
|
|
|
|
SCHEMAS_XMLNS_MANIFEST = 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0';
|
|
|
|
SCHEMAS_XMLNS_FO = 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0';
|
|
|
|
SCHEMAS_XMLNS_STYLE = 'urn:oasis:names:tc:opendocument:xmlns:style:1.0';
|
|
|
|
SCHEMAS_XMLNS_SVG = 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0';
|
|
|
|
SCHEMAS_XMLNS_TABLE = 'urn:oasis:names:tc:opendocument:xmlns:table:1.0';
|
|
|
|
SCHEMAS_XMLNS_TEXT = 'urn:oasis:names:tc:opendocument:xmlns:text:1.0';
|
|
|
|
SCHEMAS_XMLNS_V = 'urn:schemas-microsoft-com:vml';
|
|
|
|
SCHEMAS_XMLNS_NUMBER = 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0';
|
|
|
|
SCHEMAS_XMLNS_CHART = 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0';
|
|
|
|
SCHEMAS_XMLNS_DR3D = 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0';
|
|
|
|
SCHEMAS_XMLNS_MATH = 'http://www.w3.org/1998/Math/MathML';
|
|
|
|
SCHEMAS_XMLNS_FORM = 'urn:oasis:names:tc:opendocument:xmlns:form:1.0';
|
|
|
|
SCHEMAS_XMLNS_SCRIPT = 'urn:oasis:names:tc:opendocument:xmlns:script:1.0';
|
|
|
|
SCHEMAS_XMLNS_OOOW = 'http://openoffice.org/2004/writer';
|
|
|
|
SCHEMAS_XMLNS_OOOC = 'http://openoffice.org/2004/calc';
|
|
|
|
SCHEMAS_XMLNS_DOM = 'http://www.w3.org/2001/xml-events';
|
|
|
|
SCHEMAS_XMLNS_XFORMS = 'http://www.w3.org/2002/xforms';
|
|
|
|
SCHEMAS_XMLNS_XSD = 'http://www.w3.org/2001/XMLSchema';
|
|
|
|
SCHEMAS_XMLNS_XSI = 'http://www.w3.org/2001/XMLSchema-instance';
|
|
|
|
|
2014-03-26 10:26:43 +00:00
|
|
|
{ DATEMODE similar to but not the same as XLS format; used in time only values. }
|
|
|
|
DATEMODE_1899_BASE=0; //apparently 1899-12-30 for ODF in FPC DateTime;
|
|
|
|
// due to Excel's leap year bug, the date floats in the spreadsheets are the same starting
|
|
|
|
// 1900-03-01
|
|
|
|
DATEMODE_1900_BASE=2; //StarCalc compatibility, 1900-01-01 in FPC DateTime
|
2014-03-23 11:36:36 +00:00
|
|
|
DATEMODE_1904_BASE=1462; //1/1/1904 in FPC TDateTime
|
|
|
|
|
2014-05-27 22:12:48 +00:00
|
|
|
const
|
|
|
|
// lsThin, lsMedium, lsDashed, lsDotted, lsThick, lsDouble, lsHair)
|
|
|
|
BORDER_LINESTYLES: array[TsLineStyle] of string =
|
|
|
|
('solid', 'solid', 'dashed', 'fine-dashed', 'solid', 'double', 'dotted');
|
|
|
|
BORDER_LINEWIDTHS: array[TsLinestyle] of string =
|
|
|
|
('0.002cm', '2pt', '0.002cm', '0.002cm', '3pt', '0.039cm', '0.002cm');
|
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
COLWIDTH_EPS = 1e-2; // for mm
|
|
|
|
ROWHEIGHT_EPS = 1e-2; // for lines
|
|
|
|
|
2014-05-26 22:27:07 +00:00
|
|
|
type
|
2014-05-28 16:23:50 +00:00
|
|
|
{ Cell style items relevant to FPSpreadsheet. Stored in the CellStyleList of the reader. }
|
|
|
|
TCellStyleData = class
|
2014-05-26 22:27:07 +00:00
|
|
|
public
|
|
|
|
Name: String;
|
|
|
|
FontIndex: Integer;
|
|
|
|
NumFormatIndex: Integer;
|
|
|
|
HorAlignment: TsHorAlignment;
|
|
|
|
VertAlignment: TsVertAlignment;
|
|
|
|
WordWrap: Boolean;
|
|
|
|
TextRotation: TsTextRotation;
|
|
|
|
Borders: TsCellBorders;
|
|
|
|
BorderStyles: TsCellBorderStyles;
|
|
|
|
BackgroundColor: TsColor;
|
|
|
|
end;
|
|
|
|
|
2014-05-28 16:23:50 +00:00
|
|
|
{ Column style items stored in ColStyleList of the reader }
|
|
|
|
TColumnStyleData = class
|
|
|
|
public
|
|
|
|
Name: String;
|
2014-05-31 21:04:53 +00:00
|
|
|
ColWidth: Double; // in mm
|
2014-05-28 16:23:50 +00:00
|
|
|
end;
|
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
{ Column data items stored in the ColumnList }
|
2014-05-28 16:23:50 +00:00
|
|
|
TColumnData = class
|
|
|
|
public
|
|
|
|
Col: Integer;
|
|
|
|
ColStyleIndex: integer; // index into FColumnStyleList of reader
|
|
|
|
DefaultCellStyleIndex: Integer; // Index of default cell style in FCellStyleList of reader
|
|
|
|
end;
|
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
{ Row style items stored in RowStyleList of the reader }
|
|
|
|
TRowStyleData = class
|
|
|
|
public
|
|
|
|
Name: String;
|
|
|
|
RowHeight: Double; // in mm
|
|
|
|
AutoRowHeight: Boolean;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ Row data items stored in the RowList of the reader }
|
|
|
|
TRowData = class
|
|
|
|
Row: Integer;
|
|
|
|
RowStyleIndex: Integer; // index into FRowStyleList of reader
|
|
|
|
DefaultCellStyleIndex: Integer; // Index of default row style in FCellStyleList of reader
|
|
|
|
end;
|
|
|
|
|
2014-03-23 11:36:36 +00:00
|
|
|
|
2014-05-14 23:17:46 +00:00
|
|
|
{ TsSpreadOpenDocNumFormatList }
|
|
|
|
|
|
|
|
procedure TsSpreadOpenDocNumFormatList.AddBuiltinFormats;
|
|
|
|
begin
|
2014-05-25 22:05:37 +00:00
|
|
|
// there are no built-in number formats which are silently assumed to exist.
|
2014-05-14 23:17:46 +00:00
|
|
|
end;
|
|
|
|
|
2014-05-26 22:27:07 +00:00
|
|
|
|
2009-07-01 11:26:56 +00:00
|
|
|
{ TsSpreadOpenDocReader }
|
|
|
|
|
2014-05-26 22:27:07 +00:00
|
|
|
constructor TsSpreadOpenDocReader.Create(AWorkbook: TsWorkbook);
|
|
|
|
begin
|
|
|
|
inherited Create(AWorkbook);
|
2014-05-28 16:23:50 +00:00
|
|
|
FCellStyleList := TFPList.Create;
|
|
|
|
FColumnStyleList := TFPList.Create;
|
|
|
|
FColumnList := TFPList.Create;
|
2014-05-31 21:04:53 +00:00
|
|
|
FRowStyleList := TFPList.Create;
|
|
|
|
FRowList := TFPList.Create;
|
2014-06-03 22:04:11 +00:00
|
|
|
FVolatileNumFmtList := TsCustomNumFormatList.Create(Workbook);
|
2014-05-27 16:21:59 +00:00
|
|
|
// Set up the default palette in order to have the default color names correct.
|
|
|
|
Workbook.UseDefaultPalette;
|
2014-05-26 22:27:07 +00:00
|
|
|
// Initial base date in case it won't be read from file
|
|
|
|
FDateMode := dm1899;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TsSpreadOpenDocReader.Destroy;
|
|
|
|
var
|
|
|
|
j: integer;
|
|
|
|
begin
|
2014-05-28 16:23:50 +00:00
|
|
|
for j := FColumnList.Count-1 downto 0 do TObject(FColumnList[j]).Free;
|
|
|
|
FColumnList.Free;
|
|
|
|
|
|
|
|
for j := FColumnStyleList.Count-1 downto 0 do TObject(FColumnStyleList[j]).Free;
|
|
|
|
FColumnStyleList.Free;
|
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
for j := FRowList.Count-1 downto 0 do TObject(FRowList[j]).Free;
|
|
|
|
FRowList.Free;
|
|
|
|
|
|
|
|
for j := FRowStyleList.Count-1 downto 0 do TObject(FRowStyleList[j]).Free;
|
|
|
|
FRowStyleList.Free;
|
|
|
|
|
2014-05-28 16:23:50 +00:00
|
|
|
for j := FCellStyleList.Count-1 downto 0 do TObject(FCellStyleList[j]).Free;
|
|
|
|
FCellStyleList.Free;
|
|
|
|
|
2014-06-03 22:04:11 +00:00
|
|
|
FVolatileNumFmtList.Free; // automatically destroys its items.
|
|
|
|
|
2014-05-26 22:27:07 +00:00
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
{ Creates for each non-default column width stored internally in FColumnList
|
|
|
|
a TCol record in the current worksheet. }
|
|
|
|
procedure TsSpreadOpenDocReader.ApplyColWidths;
|
|
|
|
var
|
|
|
|
colIndex: Integer;
|
|
|
|
colWidth: Single;
|
|
|
|
colStyleIndex: Integer;
|
|
|
|
colStyle: TColumnStyleData;
|
|
|
|
factor: Double;
|
|
|
|
col: PCol;
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
factor := FWorkbook.GetFont(0).Size/2;
|
|
|
|
for i:=0 to FColumnList.Count-1 do begin
|
|
|
|
colIndex := TColumnData(FColumnList[i]).Col;
|
|
|
|
colStyleIndex := TColumnData(FColumnList[i]).ColStyleIndex;
|
|
|
|
colStyle := TColumnStyleData(FColumnStyleList[colStyleIndex]);
|
|
|
|
{ The column width stored in colStyle is in mm (see ReadColumnStyles).
|
|
|
|
We convert it to character count by converting it to points and then by
|
|
|
|
dividing the points by the approximate width of the '0' character which
|
|
|
|
is assumed to be 50% of the default font point size. }
|
|
|
|
colWidth := mmToPts(colStyle.ColWidth)/factor;
|
|
|
|
{ Add only column records to the worksheet if their width is different from
|
|
|
|
the default column width. }
|
|
|
|
if not SameValue(colWidth, Workbook.DefaultColWidth, COLWIDTH_EPS) then begin
|
|
|
|
col := FWorksheet.GetCol(colIndex);
|
|
|
|
col^.Width := colWidth;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-05-26 22:27:07 +00:00
|
|
|
{ Applies the style data referred to by the style name to the specified cell }
|
|
|
|
procedure TsSpreadOpenDocReader.ApplyStyleToCell(ARow, ACol: Cardinal;
|
|
|
|
AStyleName: String);
|
|
|
|
var
|
|
|
|
cell: PCell;
|
2014-05-28 16:23:50 +00:00
|
|
|
styleData: TCellStyleData;
|
2014-05-26 22:27:07 +00:00
|
|
|
styleIndex: Integer;
|
|
|
|
numFmtData: TsNumFormatData;
|
2014-05-28 16:23:50 +00:00
|
|
|
i: Integer;
|
2014-05-26 22:27:07 +00:00
|
|
|
begin
|
|
|
|
cell := FWorksheet.GetCell(ARow, ACol);
|
2014-05-28 16:23:50 +00:00
|
|
|
if not Assigned(cell) then
|
|
|
|
exit;
|
|
|
|
|
|
|
|
// Is there a style attached to the cell?
|
|
|
|
styleIndex := -1;
|
|
|
|
if AStyleName <> '' then
|
|
|
|
styleIndex := FindCellStyleByName(AStyleName);
|
|
|
|
if (styleIndex = -1) then begin
|
|
|
|
// No - look for the style attached to the column of the cell and
|
|
|
|
// find the cell style by the DefaultCellStyleIndex stored in the column list.
|
|
|
|
i := FindColumnByCol(ACol);
|
|
|
|
if i = -1 then
|
2014-05-26 22:27:07 +00:00
|
|
|
exit;
|
2014-05-28 16:23:50 +00:00
|
|
|
styleIndex := TColumnData(FColumnList[i]).DefaultCellStyleIndex;
|
|
|
|
end;
|
2014-05-26 22:27:07 +00:00
|
|
|
|
2014-05-28 16:23:50 +00:00
|
|
|
styleData := TCellStyleData(FCellStyleList[styleIndex]);
|
2014-05-26 22:27:07 +00:00
|
|
|
|
2014-05-28 16:23:50 +00:00
|
|
|
// Now copy all style parameters from the styleData to the cell.
|
2014-05-26 22:27:07 +00:00
|
|
|
|
2014-05-28 16:23:50 +00:00
|
|
|
// Font
|
|
|
|
{
|
|
|
|
if style.FontIndex = 1 then
|
|
|
|
Include(cell^.UsedFormattingFields, uffBold)
|
|
|
|
else
|
|
|
|
if XFData.FontIndex > 1 then
|
|
|
|
Include(cell^.UsedFormattingFields, uffFont);
|
|
|
|
cell^.FontIndex := styleData.FontIndex;
|
|
|
|
}
|
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
// Word wrap
|
2014-05-28 16:23:50 +00:00
|
|
|
if styleData.WordWrap then
|
|
|
|
Include(cell^.UsedFormattingFields, uffWordWrap)
|
|
|
|
else
|
|
|
|
Exclude(cell^.UsedFormattingFields, uffWordWrap);
|
2014-05-31 21:04:53 +00:00
|
|
|
|
|
|
|
// Text rotation
|
2014-05-28 16:23:50 +00:00
|
|
|
if styleData.TextRotation > trHorizontal then
|
|
|
|
Include(cell^.UsedFormattingFields, uffTextRotation)
|
|
|
|
else
|
|
|
|
Exclude(cell^.UsedFormattingFields, uffTextRotation);
|
|
|
|
cell^.TextRotation := styledata.TextRotation;
|
2014-05-31 21:04:53 +00:00
|
|
|
|
2014-05-28 16:23:50 +00:00
|
|
|
// Text alignment
|
|
|
|
if styleData.HorAlignment <> haDefault then begin
|
|
|
|
Include(cell^.UsedFormattingFields, uffHorAlign);
|
2014-05-26 22:27:07 +00:00
|
|
|
cell^.HorAlignment := styleData.HorAlignment;
|
2014-05-28 16:23:50 +00:00
|
|
|
end else
|
|
|
|
Exclude(cell^.UsedFormattingFields, uffHorAlign);
|
|
|
|
if styleData.VertAlignment <> vaDefault then begin
|
|
|
|
Include(cell^.UsedFormattingFields, uffVertAlign);
|
2014-05-26 22:27:07 +00:00
|
|
|
cell^.VertAlignment := styleData.VertAlignment;
|
2014-05-28 16:23:50 +00:00
|
|
|
end else
|
|
|
|
Exclude(cell^.UsedFormattingFields, uffVertAlign);
|
2014-05-31 21:04:53 +00:00
|
|
|
|
2014-05-28 16:23:50 +00:00
|
|
|
// Borders
|
|
|
|
cell^.BorderStyles := styleData.BorderStyles;
|
|
|
|
if styleData.Borders <> [] then begin
|
|
|
|
Include(cell^.UsedFormattingFields, uffBorder);
|
|
|
|
cell^.Border := styleData.Borders;
|
|
|
|
end else
|
|
|
|
Exclude(cell^.UsedFormattingFields, uffBorder);
|
2014-05-31 21:04:53 +00:00
|
|
|
|
2014-05-28 16:23:50 +00:00
|
|
|
// Background color
|
|
|
|
if styleData.BackgroundColor <> scNotDefined then begin
|
|
|
|
Include(cell^.UsedFormattingFields, uffBackgroundColor);
|
|
|
|
cell^.BackgroundColor := styleData.BackgroundColor;
|
|
|
|
end;
|
2014-05-26 22:27:07 +00:00
|
|
|
|
2014-05-28 16:23:50 +00:00
|
|
|
// Number format
|
|
|
|
if styleData.NumFormatIndex > -1 then
|
2014-06-02 22:07:40 +00:00
|
|
|
if (cell^.ContentType in [cctNumber, cctDateTime]) then begin
|
2014-05-28 16:23:50 +00:00
|
|
|
numFmtData := NumFormatList[styleData.NumFormatIndex];
|
|
|
|
if numFmtData <> nil then begin
|
|
|
|
Include(cell^.UsedFormattingFields, uffNumberFormat);
|
|
|
|
cell^.NumberFormat := numFmtData.NumFormat;
|
|
|
|
cell^.NumberFormatStr := numFmtData.FormatString;
|
|
|
|
cell^.Decimals := numFmtData.Decimals;
|
|
|
|
cell^.CurrencySymbol := numFmtData.CurrencySymbol;
|
2014-05-26 22:27:07 +00:00
|
|
|
end;
|
2014-05-28 16:23:50 +00:00
|
|
|
end;
|
2014-05-26 22:27:07 +00:00
|
|
|
end;
|
|
|
|
|
2014-05-20 12:36:07 +00:00
|
|
|
{ Creates the correct version of the number format list
|
|
|
|
suited for ODS file formats. }
|
2014-05-14 23:17:46 +00:00
|
|
|
procedure TsSpreadOpenDocReader.CreateNumFormatList;
|
|
|
|
begin
|
|
|
|
FreeAndNil(FNumFormatList);
|
2014-05-20 16:13:48 +00:00
|
|
|
FNumFormatList := TsSpreadOpenDocNumFormatList.Create(Workbook);
|
2014-05-14 23:17:46 +00:00
|
|
|
end;
|
|
|
|
|
2014-06-02 22:07:40 +00:00
|
|
|
{ Extracts a date/time value from a "date-value" or "time-value" cell node.
|
|
|
|
Is called from "ReadDateTime". }
|
|
|
|
function TsSpreadOpenDocReader.ExtractDateTimeFromNode(ANode: TDOMNode): TDateTime;
|
|
|
|
var
|
|
|
|
Value: String;
|
|
|
|
Fmt : TFormatSettings;
|
|
|
|
FoundPos : integer;
|
|
|
|
Hours, Minutes, Seconds: integer;
|
|
|
|
HoursPos, MinutesPos, SecondsPos: integer;
|
|
|
|
begin
|
|
|
|
// Format expects ISO 8601 type date string or
|
|
|
|
// time string
|
|
|
|
fmt := DefaultFormatSettings;
|
|
|
|
fmt.ShortDateFormat := 'yyyy-mm-dd';
|
|
|
|
fmt.DateSeparator := '-';
|
|
|
|
fmt.LongTimeFormat := 'hh:nn:ss';
|
|
|
|
fmt.TimeSeparator := ':';
|
|
|
|
|
|
|
|
Value := GetAttrValue(ANode, 'office:date-value');
|
|
|
|
|
|
|
|
if Value <> '' then begin
|
|
|
|
// Date or date/time string
|
|
|
|
Value := StringReplace(Value,'T',' ',[rfIgnoreCase,rfReplaceAll]);
|
|
|
|
// Strip milliseconds?
|
|
|
|
FoundPos := Pos('.',Value);
|
|
|
|
if (FoundPos > 1) then
|
|
|
|
Value := Copy(Value, 1, FoundPos-1);
|
|
|
|
Result := StrToDateTime(Value, Fmt);
|
|
|
|
end else begin
|
|
|
|
// Try time only, e.g. PT23H59M59S
|
|
|
|
// 12345678901
|
|
|
|
Value := GetAttrValue(ANode, 'office:time-value');
|
|
|
|
if (Value <> '') and (Pos('PT', Value) = 1) then begin
|
|
|
|
// Get hours
|
|
|
|
HoursPos := Pos('H', Value);
|
|
|
|
if (HoursPos > 0) then
|
|
|
|
Hours := StrToInt(Copy(Value, 3, HoursPos-3))
|
|
|
|
else
|
|
|
|
Hours := 0;
|
|
|
|
|
|
|
|
// Get minutes
|
|
|
|
MinutesPos := Pos('M', Value);
|
|
|
|
if (MinutesPos > 0) and (MinutesPos > HoursPos) then
|
|
|
|
Minutes := StrToInt(Copy(Value, HoursPos+1, MinutesPos-HoursPos-1))
|
|
|
|
else
|
|
|
|
Minutes := 0;
|
|
|
|
|
|
|
|
// Get seconds
|
|
|
|
SecondsPos := Pos('S', Value);
|
|
|
|
if (SecondsPos > 0) and (SecondsPos > MinutesPos) then
|
|
|
|
Seconds := StrToInt(Copy(Value, MinutesPos+1, SecondsPos-MinutesPos-1))
|
|
|
|
else
|
|
|
|
Seconds := 0;
|
|
|
|
|
|
|
|
// Times smaller than a day can be taken as is
|
|
|
|
// Times larger than a day depend on the file's date mode.
|
|
|
|
// Convert to date/time via Unix timestamp so avoiding limits for number of
|
|
|
|
// hours etc in EncodeDateTime. Perhaps there's a faster way of doing this?
|
|
|
|
Result := UnixToDateTime( Hours*(MinsPerHour*SecsPerMin) +
|
|
|
|
Minutes*(SecsPerMin) +
|
|
|
|
Seconds
|
|
|
|
) - UnixEpoch;
|
|
|
|
if (Hours <= -24) or (Hours >= 24) then begin // Can the "Hours" be negative? Not compatible with FormatDateTime?
|
|
|
|
// A day or longer
|
|
|
|
case FDateMode of
|
|
|
|
dm1899: Result := Result + DATEMODE_1899_BASE;
|
|
|
|
dm1900: Result := Result + DATEMODE_1900_BASE;
|
|
|
|
dm1904: Result := Result + DATEMODE_1904_BASE;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-05-28 16:23:50 +00:00
|
|
|
function TsSpreadOpenDocReader.FindCellStyleByName(AStyleName: String): Integer;
|
2014-05-26 22:27:07 +00:00
|
|
|
begin
|
2014-05-28 16:23:50 +00:00
|
|
|
for Result:=0 to FCellStyleList.Count-1 do begin
|
|
|
|
if TCellStyleData(FCellStyleList[Result]).Name = AStyleName then
|
2014-05-26 22:27:07 +00:00
|
|
|
exit;
|
|
|
|
end;
|
|
|
|
Result := -1;
|
|
|
|
end;
|
|
|
|
|
2014-05-28 16:23:50 +00:00
|
|
|
function TsSpreadOpenDocReader.FindColumnByCol(AColIndex: Integer): Integer;
|
|
|
|
begin
|
|
|
|
for Result := 0 to FColumnList.Count-1 do
|
|
|
|
if TColumnData(FColumnList[Result]).Col = AColIndex then
|
|
|
|
exit;
|
|
|
|
Result := -1;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TsSpreadOpenDocReader.FindColStyleByName(AStyleName: String): Integer;
|
|
|
|
begin
|
|
|
|
for Result := 0 to FColumnStyleList.Count-1 do
|
|
|
|
if TColumnStyleData(FColumnStyleList[Result]).Name = AStyleName then
|
|
|
|
exit;
|
|
|
|
Result := -1;
|
|
|
|
end;
|
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
function TsSpreadOpenDocReader.FindRowStyleByName(AStyleName: String): Integer;
|
|
|
|
begin
|
|
|
|
for Result := 0 to FRowStyleList.Count-1 do
|
|
|
|
if TRowStyleData(FRowStyleList[Result]).Name = AStyleName then
|
|
|
|
exit;
|
|
|
|
Result := -1;
|
|
|
|
end;
|
|
|
|
|
2009-07-01 11:26:56 +00:00
|
|
|
function TsSpreadOpenDocReader.GetAttrValue(ANode : TDOMNode; AAttrName : string) : string;
|
|
|
|
var
|
|
|
|
i : integer;
|
|
|
|
Found : Boolean;
|
|
|
|
begin
|
|
|
|
Found:=false;
|
|
|
|
i:=0;
|
|
|
|
Result:='';
|
|
|
|
while not Found and (i<ANode.Attributes.Length) do begin
|
|
|
|
if ANode.Attributes.Item[i].NodeName=AAttrName then begin
|
|
|
|
Found:=true;
|
|
|
|
Result:=ANode.Attributes.Item[i].NodeValue;
|
|
|
|
end;
|
|
|
|
inc(i);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-05-27 13:09:23 +00:00
|
|
|
procedure TsSpreadOpenDocReader.ReadBlank(ARow, ACol: Word; ACellNode: TDOMNode);
|
|
|
|
var
|
|
|
|
styleName: String;
|
|
|
|
begin
|
|
|
|
FWorkSheet.WriteBlank(ARow, ACol);
|
|
|
|
|
|
|
|
styleName := GetAttrValue(ACellNode, 'table:style-name');
|
|
|
|
ApplyStyleToCell(ARow, ACol, stylename);
|
|
|
|
end;
|
|
|
|
|
2014-05-28 18:58:55 +00:00
|
|
|
{ Collection columns used in the given table. The columns contain links to
|
|
|
|
styles that must be used when cells in that columns are without styles. }
|
|
|
|
procedure TsSpreadOpenDocReader.ReadColumns(ATableNode: TDOMNode);
|
|
|
|
var
|
|
|
|
col: Integer;
|
|
|
|
colNode: TDOMNode;
|
|
|
|
s: String;
|
2014-05-31 21:04:53 +00:00
|
|
|
defCellStyleIndex: Integer;
|
2014-05-28 18:58:55 +00:00
|
|
|
colStyleIndex: Integer;
|
|
|
|
colStyleData: TColumnStyleData;
|
|
|
|
colData: TColumnData;
|
2014-05-31 21:04:53 +00:00
|
|
|
colsRepeated: Integer;
|
|
|
|
j: Integer;
|
2014-05-28 18:58:55 +00:00
|
|
|
begin
|
2014-05-31 21:04:53 +00:00
|
|
|
// clear previous column list (from other sheets)
|
|
|
|
for j:=FColumnList.Count-1 downto 0 do TObject(FColumnList[j]).Free;
|
|
|
|
FColumnList.Clear;
|
|
|
|
|
2014-05-28 18:58:55 +00:00
|
|
|
col := 0;
|
|
|
|
colNode := ATableNode.FindNode('table:table-column');
|
|
|
|
while Assigned(colNode) do begin
|
|
|
|
if colNode.NodeName = 'table:table-column' then begin;
|
|
|
|
s := GetAttrValue(colNode, 'table:style-name');
|
|
|
|
colStyleIndex := FindColStyleByName(s);
|
|
|
|
if colStyleIndex <> -1 then begin
|
2014-05-31 21:04:53 +00:00
|
|
|
defCellStyleIndex := -1;
|
2014-05-28 18:58:55 +00:00
|
|
|
colStyleData := TColumnStyleData(FColumnStyleList[colStyleIndex]);
|
|
|
|
s := GetAttrValue(ColNode, 'table:default-cell-style-name');
|
|
|
|
if s <> '' then begin
|
2014-05-31 21:04:53 +00:00
|
|
|
defCellStyleIndex := FindCellStyleByName(s);
|
2014-05-28 18:58:55 +00:00
|
|
|
colData := TColumnData.Create;
|
|
|
|
colData.Col := col;
|
|
|
|
colData.ColStyleIndex := colStyleIndex;
|
2014-05-31 21:04:53 +00:00
|
|
|
colData.DefaultCellStyleIndex := defCellStyleIndex;
|
2014-05-28 18:58:55 +00:00
|
|
|
FColumnList.Add(colData);
|
|
|
|
end;
|
2014-05-31 21:04:53 +00:00
|
|
|
s := GetAttrValue(ColNode, 'table:number-columns-repeated');
|
|
|
|
if s = '' then
|
|
|
|
inc(col)
|
|
|
|
else begin
|
|
|
|
colsRepeated := StrToInt(s);
|
|
|
|
if defCellStyleIndex > -1 then
|
|
|
|
for j:=1 to colsRepeated-1 do begin
|
|
|
|
colData := TColumnData.Create;
|
|
|
|
colData.Col := col + j;
|
|
|
|
colData.ColStyleIndex := colStyleIndex;
|
|
|
|
colData.DefaultCellStyleIndex := defCellStyleIndex;
|
|
|
|
FColumnList.Add(colData);
|
|
|
|
end;
|
|
|
|
inc(col, colsRepeated);
|
|
|
|
end;
|
2014-05-28 18:58:55 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
colNode := colNode.NextSibling;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
{ Reads the column styles and stores them in the FColumnStyleList for later use }
|
|
|
|
procedure TsSpreadOpenDocReader.ReadColumnStyle(AStyleNode: TDOMNode);
|
|
|
|
var
|
|
|
|
colStyle: TColumnStyleData;
|
|
|
|
styleName: String;
|
|
|
|
styleChildNode: TDOMNode;
|
|
|
|
colWidth: double;
|
|
|
|
s: String;
|
|
|
|
begin
|
|
|
|
styleName := GetAttrValue(AStyleNode, 'style:name');
|
|
|
|
styleChildNode := AStyleNode.FirstChild;
|
|
|
|
colWidth := -1;
|
|
|
|
|
|
|
|
while Assigned(styleChildNode) do begin
|
|
|
|
if styleChildNode.NodeName = 'style:table-column-properties' then begin
|
|
|
|
s := GetAttrValue(styleChildNode, 'style:column-width');
|
|
|
|
if s <> '' then begin
|
|
|
|
colWidth := PtsToMM(HTMLLengthStrToPts(s)); // convert to mm
|
|
|
|
break;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
styleChildNode := styleChildNode.NextSibling;
|
|
|
|
end;
|
|
|
|
|
|
|
|
colStyle := TColumnStyleData.Create;
|
|
|
|
colStyle.Name := styleName;
|
|
|
|
colStyle.ColWidth := colWidth;
|
|
|
|
FColumnStyleList.Add(colStyle);
|
|
|
|
end;
|
|
|
|
|
2014-03-26 10:26:43 +00:00
|
|
|
procedure TsSpreadOpenDocReader.ReadDateMode(SpreadSheetNode: TDOMNode);
|
|
|
|
var
|
|
|
|
CalcSettingsNode, NullDateNode: TDOMNode;
|
|
|
|
NullDateSetting: string;
|
|
|
|
begin
|
|
|
|
// Default datemode for ODF:
|
|
|
|
NullDateSetting:='1899-12-30';
|
|
|
|
CalcSettingsNode:=SpreadsheetNode.FindNode('table:calculation-settings');
|
|
|
|
if Assigned(CalcSettingsNode) then
|
|
|
|
begin
|
|
|
|
NullDateNode:=CalcSettingsNode.FindNode('table:null-date');
|
|
|
|
if Assigned(NullDateNode) then
|
|
|
|
NullDateSetting:=GetAttrValue(NullDateNode,'table:date-value');
|
|
|
|
end;
|
|
|
|
if NullDateSetting='1899-12-30' then
|
|
|
|
FDateMode := dm1899
|
|
|
|
else if NullDateSetting='1900-01-01' then
|
|
|
|
FDateMode := dm1900
|
|
|
|
else if NullDateSetting='1904-01-01' then
|
|
|
|
FDateMode := dm1904
|
|
|
|
else
|
|
|
|
raise Exception.CreateFmt('Spreadsheet file corrupt: cannot handle null-date format %s', [NullDateSetting]);
|
|
|
|
end;
|
|
|
|
|
2009-07-01 11:26:56 +00:00
|
|
|
procedure TsSpreadOpenDocReader.ReadFromFile(AFileName: string; AData: TsWorkbook);
|
|
|
|
var
|
2014-05-26 15:00:43 +00:00
|
|
|
Doc : TXMLDocument;
|
2009-07-01 11:26:56 +00:00
|
|
|
FilePath : string;
|
|
|
|
UnZip : TUnZipper;
|
|
|
|
FileList : TStringList;
|
2014-05-28 18:58:55 +00:00
|
|
|
BodyNode, SpreadSheetNode, TableNode: TDOMNode;
|
2014-05-26 08:03:36 +00:00
|
|
|
StylesNode: TDOMNode;
|
2014-06-01 18:59:29 +00:00
|
|
|
|
|
|
|
{ We have to use our own ReadXMLFile procedure (there is one in xmlread)
|
|
|
|
because we have to preserve spaces in element text for date/time separator.
|
|
|
|
As a side-effect we have to skip leading spaces by ourselves. }
|
|
|
|
procedure ReadXMLFile(out ADoc: TXMLDocument; AFileName: String);
|
|
|
|
var
|
|
|
|
parser: TDOMParser;
|
|
|
|
src: TXMLInputSource;
|
|
|
|
stream: TStream;
|
|
|
|
begin
|
|
|
|
stream := TFileStream.Create(AFileName, fmOpenRead + fmShareDenyWrite);
|
|
|
|
try
|
|
|
|
parser := TDOMParser.Create;
|
|
|
|
try
|
|
|
|
parser.Options.PreserveWhiteSpace := true; // This preserves spaces!
|
|
|
|
src := TXMLInputSource.Create(stream);
|
|
|
|
try
|
|
|
|
parser.Parse(src, ADoc);
|
|
|
|
finally
|
|
|
|
src.Free;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
parser.Free;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
stream.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2009-07-01 11:26:56 +00:00
|
|
|
begin
|
|
|
|
//unzip content.xml into AFileName path
|
2014-05-26 22:27:07 +00:00
|
|
|
FilePath := GetTempDir(false);
|
|
|
|
UnZip := TUnZipper.Create;
|
|
|
|
UnZip.OutputPath := FilePath;
|
|
|
|
FileList := TStringList.Create;
|
2014-05-26 08:03:36 +00:00
|
|
|
FileList.Add('styles.xml');
|
2009-07-01 11:26:56 +00:00
|
|
|
FileList.Add('content.xml');
|
|
|
|
try
|
|
|
|
Unzip.UnZipFiles(AFileName,FileList);
|
2009-11-12 20:26:02 +00:00
|
|
|
finally
|
|
|
|
FreeAndNil(FileList);
|
|
|
|
FreeAndNil(UnZip);
|
2009-07-01 11:26:56 +00:00
|
|
|
end; //try
|
2009-11-12 20:26:02 +00:00
|
|
|
|
2014-05-26 15:00:43 +00:00
|
|
|
Doc := nil;
|
2009-11-12 20:26:02 +00:00
|
|
|
try
|
2014-05-26 08:03:36 +00:00
|
|
|
// process the styles.xml file
|
2014-05-26 15:00:43 +00:00
|
|
|
ReadXMLFile(Doc, FilePath+'styles.xml');
|
2014-05-26 08:03:36 +00:00
|
|
|
DeleteFile(FilePath+'styles.xml');
|
|
|
|
|
2014-05-26 15:00:43 +00:00
|
|
|
StylesNode := Doc.DocumentElement.FindNode('office:styles');
|
2014-05-26 08:03:36 +00:00
|
|
|
ReadNumFormats(StylesNode);
|
2014-05-26 22:27:07 +00:00
|
|
|
ReadStyles(StylesNode);
|
2014-05-26 08:03:36 +00:00
|
|
|
|
2014-06-01 18:59:29 +00:00
|
|
|
Doc.Free;
|
|
|
|
|
2014-05-26 08:03:36 +00:00
|
|
|
//process the content.xml file
|
2014-05-26 15:00:43 +00:00
|
|
|
ReadXMLFile(Doc, FilePath+'content.xml');
|
2009-11-12 20:26:02 +00:00
|
|
|
DeleteFile(FilePath+'content.xml');
|
|
|
|
|
2014-05-26 15:00:43 +00:00
|
|
|
StylesNode := Doc.DocumentElement.FindNode('office:automatic-styles');
|
2014-05-26 08:03:36 +00:00
|
|
|
ReadNumFormats(StylesNode);
|
2014-05-26 22:27:07 +00:00
|
|
|
ReadStyles(StylesNode);
|
2014-05-25 22:05:37 +00:00
|
|
|
|
2014-05-26 15:00:43 +00:00
|
|
|
BodyNode := Doc.DocumentElement.FindNode('office:body');
|
2009-11-12 20:26:02 +00:00
|
|
|
if not Assigned(BodyNode) then Exit;
|
|
|
|
|
2014-05-26 15:00:43 +00:00
|
|
|
SpreadSheetNode := BodyNode.FindNode('office:spreadsheet');
|
2009-11-12 20:26:02 +00:00
|
|
|
if not Assigned(SpreadSheetNode) then Exit;
|
|
|
|
|
2014-03-26 10:26:43 +00:00
|
|
|
ReadDateMode(SpreadSheetNode);
|
|
|
|
|
2009-11-12 20:26:02 +00:00
|
|
|
//process each table (sheet)
|
2014-05-26 15:00:43 +00:00
|
|
|
TableNode := SpreadSheetNode.FindNode('table:table');
|
2009-11-12 20:26:02 +00:00
|
|
|
while Assigned(TableNode) do begin
|
2014-06-01 18:59:29 +00:00
|
|
|
// These nodes occur due to leading spaces which are not skipped
|
|
|
|
// automatically any more due to PreserveWhiteSpace option applied
|
|
|
|
// to ReadXMLFile
|
|
|
|
if TableNode.NodeName = '#text' then begin
|
|
|
|
TableNode := TableNode.NextSibling;
|
|
|
|
continue;
|
|
|
|
end;
|
2014-05-26 15:00:43 +00:00
|
|
|
FWorkSheet := aData.AddWorksheet(GetAttrValue(TableNode,'table:name'));
|
2014-05-28 16:23:50 +00:00
|
|
|
// Collect column styles used
|
2014-05-28 18:58:55 +00:00
|
|
|
ReadColumns(TableNode);
|
|
|
|
// Process each row inside the sheet and process each cell of the row
|
2014-05-31 21:04:53 +00:00
|
|
|
ReadRowsAndCells(TableNode);
|
|
|
|
ApplyColWidths;
|
2014-05-28 18:58:55 +00:00
|
|
|
// Continue with next table
|
2014-05-26 15:00:43 +00:00
|
|
|
TableNode := TableNode.NextSibling;
|
2009-11-12 20:26:02 +00:00
|
|
|
end; //while Assigned(TableNode)
|
2014-05-26 15:00:43 +00:00
|
|
|
|
2009-11-12 20:26:02 +00:00
|
|
|
finally
|
2014-06-01 18:59:29 +00:00
|
|
|
if Assigned(Doc) then Doc.Free;
|
2009-11-12 20:26:02 +00:00
|
|
|
end;
|
2009-07-01 11:26:56 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TsSpreadOpenDocReader.ReadFormula(ARow: Word; ACol : Word; ACellNode : TDOMNode);
|
|
|
|
begin
|
2010-05-25 09:11:02 +00:00
|
|
|
// For now just read the number
|
|
|
|
ReadNumber(ARow, ACol, ACellNode);
|
2009-07-01 11:26:56 +00:00
|
|
|
end;
|
|
|
|
|
2014-05-27 13:09:23 +00:00
|
|
|
procedure TsSpreadOpenDocReader.ReadLabel(ARow: Word; ACol: Word; ACellNode: TDOMNode);
|
|
|
|
var
|
2014-05-27 13:58:57 +00:00
|
|
|
cellText: String;
|
2014-05-27 13:09:23 +00:00
|
|
|
styleName: String;
|
2009-07-01 11:26:56 +00:00
|
|
|
begin
|
2014-05-27 13:58:57 +00:00
|
|
|
cellText := UTF8Encode(ACellNode.TextContent);
|
|
|
|
FWorkSheet.WriteUTF8Text(ARow, ACol, cellText);
|
2014-05-27 13:09:23 +00:00
|
|
|
|
|
|
|
styleName := GetAttrValue(ACellNode, 'table:style-name');
|
|
|
|
ApplyStyleToCell(ARow, ACol, stylename);
|
2009-07-01 11:26:56 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TsSpreadOpenDocReader.ReadNumber(ARow: Word; ACol : Word; ACellNode : TDOMNode);
|
2009-09-02 01:22:46 +00:00
|
|
|
var
|
|
|
|
FSettings: TFormatSettings;
|
2010-05-25 09:11:02 +00:00
|
|
|
Value, Str: String;
|
|
|
|
lNumber: Double;
|
2014-05-26 22:27:07 +00:00
|
|
|
styleName: String;
|
2014-06-03 22:04:11 +00:00
|
|
|
lCell: PCell;
|
2009-07-01 11:26:56 +00:00
|
|
|
begin
|
2014-04-24 22:31:01 +00:00
|
|
|
FSettings := DefaultFormatSettings;
|
2009-09-02 01:22:46 +00:00
|
|
|
FSettings.DecimalSeparator:='.';
|
2014-06-03 22:04:11 +00:00
|
|
|
|
2014-05-26 22:27:07 +00:00
|
|
|
Value := GetAttrValue(ACellNode,'office:value');
|
2010-05-25 09:11:02 +00:00
|
|
|
if UpperCase(Value)='1.#INF' then
|
2014-06-03 22:04:11 +00:00
|
|
|
FWorkSheet.WriteNumber(Arow,ACol,1.0/0.0)
|
2010-05-25 09:11:02 +00:00
|
|
|
else
|
|
|
|
begin
|
|
|
|
// Don't merge, or else we can't debug
|
|
|
|
Str := GetAttrValue(ACellNode,'office:value');
|
|
|
|
lNumber := StrToFloat(Str,FSettings);
|
2014-03-23 13:06:19 +00:00
|
|
|
FWorkSheet.WriteNumber(ARow,ACol,lNumber);
|
2009-11-12 20:26:02 +00:00
|
|
|
end;
|
2014-05-26 22:27:07 +00:00
|
|
|
|
|
|
|
styleName := GetAttrValue(ACellNode, 'table:style-name');
|
|
|
|
ApplyStyleToCell(ARow, ACol, stylename);
|
2014-06-03 22:04:11 +00:00
|
|
|
|
|
|
|
// Sometimes date/time cells are marked as "float"...
|
|
|
|
lCell := FWorksheet.FindCell(ARow, ACol);
|
|
|
|
if IsDateTimeFormat(lCell^.NumberFormat) then begin
|
|
|
|
lCell^.ContentType := cctDateTime;
|
|
|
|
lCell^.DateTimeValue := lCell^.NumberValue;
|
|
|
|
end;
|
2009-07-01 11:26:56 +00:00
|
|
|
end;
|
|
|
|
|
2014-06-02 22:07:40 +00:00
|
|
|
procedure TsSpreadOpenDocReader.ReadDateTime(ARow: Word; ACol : Word;
|
|
|
|
ACellNode : TDOMNode);
|
2012-12-11 16:46:25 +00:00
|
|
|
var
|
2014-03-24 07:34:00 +00:00
|
|
|
dt: TDateTime;
|
2014-06-02 22:07:40 +00:00
|
|
|
styleName: String;
|
2012-12-11 16:46:25 +00:00
|
|
|
begin
|
2014-06-02 22:07:40 +00:00
|
|
|
dt := ExtractDateTimeFromNode(ACellNode);
|
|
|
|
FWorkSheet.WriteDateTime(ARow, ACol, dt);
|
2014-03-23 11:36:36 +00:00
|
|
|
|
2014-06-02 22:07:40 +00:00
|
|
|
styleName := GetAttrValue(ACellNode, 'table:style-name');
|
|
|
|
ApplyStyleToCell(ARow, ACol, stylename);
|
2012-12-11 16:46:25 +00:00
|
|
|
end;
|
|
|
|
|
2014-05-26 08:03:36 +00:00
|
|
|
procedure TsSpreadOpenDocReader.ReadNumFormats(AStylesNode: TDOMNode);
|
2014-05-25 22:05:37 +00:00
|
|
|
|
2014-06-03 22:04:11 +00:00
|
|
|
procedure ReadStyleMap(ANode: TDOMNode; var ANumFormat: TsNumberFormat;
|
|
|
|
var AFormatStr: String);
|
|
|
|
var
|
|
|
|
condition: String;
|
|
|
|
stylename: String;
|
|
|
|
styleindex: Integer;
|
|
|
|
fmt: String;
|
|
|
|
posfmt, negfmt, zerofmt: String;
|
|
|
|
isPos, isNeg, isZero: Boolean;
|
|
|
|
begin
|
|
|
|
posfmt := '';
|
|
|
|
negfmt := '';
|
|
|
|
zerofmt := '';
|
|
|
|
|
|
|
|
// These are indicators which part of the format is currently being read.
|
|
|
|
// Needed to assign text elements correctly.
|
|
|
|
isPos := false;
|
|
|
|
isNeg := false;
|
|
|
|
isZero := false;
|
|
|
|
|
|
|
|
while ANode <> nil do begin
|
|
|
|
condition := ANode.NodeName;
|
|
|
|
|
|
|
|
if (ANode.NodeName = '#text') or not ANode.HasAttributes then begin
|
|
|
|
ANode := ANode.NextSibling;
|
|
|
|
Continue;
|
|
|
|
end;
|
2014-06-01 18:59:29 +00:00
|
|
|
|
2014-06-03 22:04:11 +00:00
|
|
|
condition := GetAttrValue(ANode, 'style:condition');
|
|
|
|
stylename := GetAttrValue(ANode, 'style:apply-style-name');
|
|
|
|
if (condition = '') or (stylename = '') then begin
|
|
|
|
ANode := ANode.NextSibling;
|
|
|
|
continue;
|
|
|
|
end;
|
|
|
|
|
|
|
|
Delete(condition, 1, Length('value()'));
|
|
|
|
styleindex := -1;
|
|
|
|
styleindex := FNumFormatList.FindByName(stylename);
|
|
|
|
if (styleindex = -1) or (condition = '') then begin
|
|
|
|
ANode := ANode.NextSibling;
|
|
|
|
continue;
|
|
|
|
end;
|
|
|
|
|
|
|
|
fmt := FNumFormatList[styleindex].FormatString;
|
|
|
|
case condition[1] of
|
|
|
|
'<': begin
|
|
|
|
negfmt := fmt;
|
|
|
|
isneg := true;
|
|
|
|
ispos := false;
|
|
|
|
if (Length(condition) > 1) and (condition[2] = '=') then begin
|
|
|
|
zerofmt := fmt;
|
|
|
|
iszero := true;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
'>': begin
|
|
|
|
posfmt := fmt;
|
|
|
|
ispos := true;
|
|
|
|
isneg := false;
|
|
|
|
if (Length(condition) > 1) and (condition[2] = '=') then begin
|
|
|
|
zerofmt := fmt;
|
|
|
|
iszero := true;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
'=': begin
|
|
|
|
zerofmt := fmt;
|
|
|
|
ispos := false;
|
|
|
|
isneg := false;
|
|
|
|
iszero := true;
|
|
|
|
end;
|
2014-05-25 22:05:37 +00:00
|
|
|
end;
|
2014-06-03 22:04:11 +00:00
|
|
|
ANode := ANode.NextSibling;
|
|
|
|
end;
|
|
|
|
if posfmt = '' then posfmt := AFormatStr;
|
|
|
|
if negfmt = '' then negfmt := AFormatStr;
|
|
|
|
|
|
|
|
AFormatStr := posFmt;
|
|
|
|
if negfmt <> '' then AFormatStr := AFormatStr + ';' + negfmt;
|
|
|
|
if zerofmt <> '' then AFormatStr := AFormatStr + ';' + zerofmt;
|
|
|
|
|
|
|
|
if ANumFormat <> nfFmtDateTime then
|
|
|
|
ANumFormat := nfCustom;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure ReadNumberStyle(ANumFormatNode: TDOMNode; ANumFormatName: String);
|
|
|
|
var
|
|
|
|
node, childNode: TDOMNode;
|
|
|
|
fmtName, nodeName: String;
|
|
|
|
fmt: String;
|
|
|
|
nf: TsNumberFormat;
|
|
|
|
decs: Byte;
|
|
|
|
s: String;
|
|
|
|
grouping: Boolean;
|
|
|
|
nex: Integer;
|
|
|
|
begin
|
|
|
|
fmt := '';
|
|
|
|
node := ANumFormatNode.FirstChild;
|
|
|
|
while Assigned(node) do begin
|
|
|
|
nodeName := node.NodeName;
|
|
|
|
if nodeName = '#text' then begin
|
|
|
|
node := node.NextSibling;
|
|
|
|
Continue;
|
|
|
|
end else
|
|
|
|
if nodeName = 'number:number' then begin
|
|
|
|
s := GetAttrValue(node, 'number:decimal-places');
|
|
|
|
if s <> '' then decs := StrToInt(s) else decs := 0;
|
|
|
|
grouping := GetAttrValue(node, 'number:grouping') = 'true';
|
|
|
|
nf := IfThen(grouping, nfFixedTh, nfFixed);
|
|
|
|
fmt := fmt + BuildNumberFormatString(nf, Workbook.FormatSettings, decs);
|
|
|
|
end else
|
|
|
|
if nodeName = 'number:scientific-number' then begin
|
2014-05-25 22:05:37 +00:00
|
|
|
nf := nfExp;
|
2014-06-03 22:04:11 +00:00
|
|
|
s := GetAttrValue(node, 'number:decimal-places');
|
|
|
|
if s <> '' then decs := StrToInt(s) else decs := 0;
|
|
|
|
s := GetAttrValue(node, 'number:min-exponent-digits');
|
|
|
|
if s <> '' then nex := StrToInt(s) else nex := 1;
|
|
|
|
fmt := fmt + BuildNumberFormatString(nfFixed, Workbook.FormatSettings, decs);
|
2014-05-25 22:05:37 +00:00
|
|
|
fmt := fmt + 'E+' + DupeString('0', nex);
|
2014-06-03 22:04:11 +00:00
|
|
|
end else
|
|
|
|
if nodeName = 'number:text' then begin
|
|
|
|
childNode := node.FirstChild;
|
|
|
|
while childNode <> nil do begin
|
|
|
|
fmt := fmt + childNode.NodeValue;
|
|
|
|
childNode := childNode.NextSibling;
|
|
|
|
end;
|
2014-05-25 22:05:37 +00:00
|
|
|
end;
|
2014-06-03 22:04:11 +00:00
|
|
|
node := node.NextSibling;
|
|
|
|
end;
|
|
|
|
|
|
|
|
node := ANumFormatNode.FindNode('style:map');
|
|
|
|
if node <> nil then
|
|
|
|
ReadStyleMap(node, nf, fmt);
|
|
|
|
|
|
|
|
NumFormatList.AddFormat(ANumFormatName, fmt, nf, decs);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure ReadPercentageStyle(ANumFormatNode: TDOMNode; ANumFormatName: String);
|
|
|
|
var
|
|
|
|
node, childNode: TDOMNode;
|
|
|
|
fmtName, nodeName: String;
|
|
|
|
nf: TsNumberFormat;
|
|
|
|
decs: Byte;
|
|
|
|
fmt: String;
|
|
|
|
s: String;
|
|
|
|
begin
|
|
|
|
fmt := '';
|
|
|
|
node := ANumFormatNode.FirstChild;
|
|
|
|
while Assigned(node) do begin
|
|
|
|
nodeName := node.NodeName;
|
|
|
|
if nodeName = '#text' then begin
|
|
|
|
node := node.NextSibling;
|
|
|
|
Continue;
|
|
|
|
end else
|
|
|
|
if nodeName = 'number:number' then begin
|
2014-05-25 22:05:37 +00:00
|
|
|
nf := nfPercentage;
|
2014-06-03 22:04:11 +00:00
|
|
|
s := GetAttrValue(node, 'number:decimal-places');
|
|
|
|
if s <> '' then decs := StrToInt(s) else decs := 0;
|
|
|
|
fmt := fmt + BuildNumberFormatString(nfFixed, Workbook.FormatSettings, decs);
|
|
|
|
// The percent sign has already been added --> nFixed instead of nfPercentage
|
|
|
|
end else
|
|
|
|
if nodeName = 'number:text' then begin
|
|
|
|
childNode := node.FirstChild;
|
|
|
|
while childNode <> nil do begin
|
|
|
|
fmt := fmt + childNode.NodeValue;
|
|
|
|
childNode := childNode.NextSibling;
|
|
|
|
end;
|
2014-05-25 22:05:37 +00:00
|
|
|
end;
|
2014-06-03 22:04:11 +00:00
|
|
|
node := node.NextSibling;
|
|
|
|
end;
|
|
|
|
|
|
|
|
node := ANumFormatNode.FindNode('style:map');
|
|
|
|
if node <> nil then
|
|
|
|
ReadStyleMap(node, nf, fmt);
|
|
|
|
|
|
|
|
NumFormatList.AddFormat(ANumFormatName, fmt, nf, decs);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure ReadDateTimeStyle(ANumFormatNode: TDOMNode; ANumFormatName: String);
|
|
|
|
var
|
|
|
|
node, childNode: TDOMNode;
|
|
|
|
nf: TsNumberFormat;
|
|
|
|
fmt: String;
|
|
|
|
nodeName: String;
|
|
|
|
s, stxt, sovr: String;
|
|
|
|
begin
|
|
|
|
fmt := '';
|
|
|
|
sovr := GetAttrValue(ANumFormatNode, 'number:truncate-on-overflow');
|
|
|
|
node := ANumFormatNode.FirstChild;
|
|
|
|
while Assigned(node) do begin
|
|
|
|
nodeName := node.NodeName;
|
|
|
|
if nodeName = '#text' then begin
|
|
|
|
node := node.NextSibling;
|
|
|
|
Continue;
|
|
|
|
end else
|
|
|
|
if nodeName = 'number:year' then begin
|
|
|
|
s := GetAttrValue(node, 'number:style');
|
|
|
|
if s = 'long' then fmt := fmt + 'yyyy'
|
|
|
|
else if s = '' then fmt := fmt + 'yy';
|
|
|
|
end else
|
|
|
|
if nodeName = 'number:month' then begin
|
|
|
|
s := GetAttrValue(node, 'number:style');
|
|
|
|
stxt := GetAttrValue(node, 'number:textual');
|
|
|
|
if (stxt = 'true') then begin // Month as text
|
|
|
|
if (s = 'long') then fmt := fmt + 'mmmm' else fmt := fmt + 'mmm';
|
|
|
|
end else begin // Month as number
|
|
|
|
if (s = 'long') then fmt := fmt + 'mm' else fmt := fmt + 'm';
|
|
|
|
end;
|
|
|
|
end else
|
|
|
|
if nodeName = 'number:day' then begin
|
|
|
|
s := GetAttrValue(node, 'number:style');
|
|
|
|
stxt := GetAttrValue(node, 'number:textual');
|
|
|
|
if (stxt = 'true') then begin // day as text
|
|
|
|
if (s = 'long') then fmt := fmt + 'dddd' else fmt := fmt + 'ddd';
|
|
|
|
end else begin // day as number
|
|
|
|
if (s = 'long') then fmt := fmt + 'dd' else fmt := fmt + 'd';
|
2014-06-01 13:08:14 +00:00
|
|
|
end;
|
2014-06-03 22:04:11 +00:00
|
|
|
end;
|
|
|
|
if nodeName = 'number:day-of-week' then begin
|
|
|
|
s := GetAttrValue(node, 'number:stye');
|
|
|
|
if (s = 'long') then fmt := fmt + 'dddddd' else fmt := fmt + 'ddddd';
|
|
|
|
end else
|
|
|
|
if nodeName = 'number:hours' then begin
|
|
|
|
s := GetAttrValue(node, 'number:style');
|
|
|
|
if (sovr = 'false') then begin
|
|
|
|
if (s = 'long') then fmt := fmt + '[hh]' else fmt := fmt + '[h]';
|
|
|
|
end else begin
|
|
|
|
if (s = 'long') then fmt := fmt + 'hh' else fmt := fmt + 'h';
|
|
|
|
end;
|
|
|
|
sovr := '';
|
|
|
|
end else
|
|
|
|
if nodeName = 'number:minutes' then begin
|
|
|
|
s := GetAttrValue(node, 'number:style');
|
|
|
|
if (sovr = 'false') then begin
|
|
|
|
if (s = 'long') then fmt := fmt + '[nn]' else fmt := fmt + '[n]';
|
|
|
|
end else begin
|
|
|
|
if (s = 'long') then fmt := fmt + 'nn' else fmt := fmt + 'n';
|
|
|
|
end;
|
|
|
|
sovr := '';
|
|
|
|
end else
|
|
|
|
if nodeName = 'number:seconds' then begin
|
|
|
|
s := GetAttrValue(node, 'number:style');
|
|
|
|
if (sovr = 'false') then begin
|
|
|
|
if (s = 'long') then fmt := fmt + '[ss]' else fmt := fmt + '[s]';
|
|
|
|
end else begin
|
|
|
|
if (s = 'long') then fmt := fmt + 'ss' else fmt := fmt + 's';
|
|
|
|
sovr := '';
|
|
|
|
end;
|
|
|
|
s := GetAttrValue(node, 'number:decimal-places');
|
|
|
|
if (s <> '') and (s <> '0') then
|
|
|
|
fmt := fmt + '.' + DupeString('0', StrToInt(s));
|
|
|
|
end else
|
|
|
|
if nodeName = 'number:am-pm' then
|
|
|
|
fmt := fmt + 'AM/PM'
|
|
|
|
else
|
|
|
|
if nodeName = 'number:text' then begin
|
|
|
|
childnode := node.FirstChild;
|
|
|
|
if childnode <> nil then
|
|
|
|
fmt := fmt + childnode.NodeValue;
|
|
|
|
end;
|
|
|
|
node := node.NextSibling;
|
|
|
|
end;
|
|
|
|
|
|
|
|
nf := nfFmtDateTime;
|
|
|
|
node := ANumFormatNode.FindNode('style:map');
|
|
|
|
if node <> nil then
|
|
|
|
ReadStyleMap(node, nf, fmt);
|
|
|
|
|
|
|
|
NumFormatList.AddFormat(ANumFormatName, fmt, nf);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure ReadTextStyle(ANumFormatNode: TDOMNode; ANumFormatName: String);
|
|
|
|
var
|
|
|
|
node, childNode: TDOMNode;
|
|
|
|
nf: TsNumberFormat;
|
|
|
|
fmt: String;
|
|
|
|
nodeName: String;
|
|
|
|
s: String;
|
|
|
|
begin
|
|
|
|
fmt := '';
|
|
|
|
node := ANumFormatNode.FirstChild;
|
|
|
|
while Assigned(node) do begin
|
|
|
|
nodeName := node.NodeName;
|
|
|
|
if nodeName = '#text' then begin
|
2014-05-26 19:43:05 +00:00
|
|
|
node := node.NextSibling;
|
2014-06-03 22:04:11 +00:00
|
|
|
Continue;
|
|
|
|
end else
|
|
|
|
if nodeName = 'number:text-content' then begin
|
|
|
|
// ???
|
|
|
|
end else
|
|
|
|
if nodeName = 'number:text' then begin
|
|
|
|
childnode := node.FirstChild;
|
|
|
|
if childnode <> nil then
|
|
|
|
fmt := fmt + childnode.NodeValue;
|
2014-05-26 19:43:05 +00:00
|
|
|
end;
|
2014-06-03 22:04:11 +00:00
|
|
|
node := node.NextSibling;
|
2014-05-25 22:05:37 +00:00
|
|
|
end;
|
2014-06-03 22:04:11 +00:00
|
|
|
|
|
|
|
node := ANumFormatNode.FindNode('style:map');
|
|
|
|
if node <> nil then
|
|
|
|
ReadStyleMap(node, nf, fmt);
|
|
|
|
if IsDateTimeFormat(fmt) then
|
|
|
|
nf := nfFmtDateTime
|
|
|
|
else
|
|
|
|
nf := nfCustom;
|
|
|
|
|
|
|
|
NumFormatList.AddFormat(ANumFormatName, fmt, nf);
|
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
|
|
|
NumFormatNode: TDOMNode;
|
|
|
|
numfmt_nodename, numfmtname: String;
|
|
|
|
|
|
|
|
begin
|
|
|
|
if not Assigned(AStylesNode) then
|
|
|
|
exit;
|
|
|
|
|
|
|
|
NumFormatNode := AStylesNode.FirstChild;
|
|
|
|
while Assigned(NumFormatNode) do begin
|
|
|
|
numfmt_nodename := NumFormatNode.NodeName;
|
|
|
|
|
|
|
|
if NumFormatNode.HasAttributes then
|
|
|
|
numfmtName := GetAttrValue(NumFormatNode, 'style:name') else
|
|
|
|
numfmtName := '';
|
|
|
|
|
|
|
|
// Numbers (nfFixed, nfFixedTh, nfExp)
|
|
|
|
if numfmt_nodename = 'number:number-style' then
|
|
|
|
ReadNumberStyle(NumFormatNode, numfmtName);
|
|
|
|
|
|
|
|
// Percentage
|
|
|
|
if numfmt_nodename = 'number:percentage-style' then
|
|
|
|
ReadPercentageStyle(NumFormatNode, numfmtName);
|
|
|
|
|
|
|
|
// Date/time values
|
|
|
|
if (numfmt_nodename = 'number:date-style') or (numfmt_nodename = 'number:time-style') then
|
|
|
|
ReadDateTimeStyle(NumFormatNode, numfmtName);
|
|
|
|
|
|
|
|
// Text values
|
|
|
|
if (numfmt_nodename = 'number:text-style') then
|
|
|
|
ReadTextStyle(NumFormatNode, numfmtName);
|
|
|
|
|
|
|
|
// Next node
|
2014-05-25 22:05:37 +00:00
|
|
|
NumFormatNode := NumFormatNode.NextSibling;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
{ Reads the cells in the given table. Loops through all rows, and then finds all
|
|
|
|
cells of each row. }
|
|
|
|
procedure TsSpreadOpenDocReader.ReadRowsAndCells(ATableNode: TDOMNode);
|
|
|
|
var
|
|
|
|
row: Integer;
|
|
|
|
col: Integer;
|
|
|
|
cellNode, rowNode: TDOMNode;
|
|
|
|
paramValueType, paramFormula, tableStyleName: String;
|
|
|
|
paramColsRepeated, paramRowsRepeated: String;
|
|
|
|
colsRepeated, rowsRepeated: Integer;
|
|
|
|
rowStyleName: String;
|
|
|
|
rowStyleIndex: Integer;
|
|
|
|
rowStyle: TRowStyleData;
|
|
|
|
rowHeight: Single;
|
|
|
|
autoRowHeight: Boolean;
|
|
|
|
i: Integer;
|
|
|
|
lRow: PRow;
|
2014-06-01 18:59:29 +00:00
|
|
|
s: String;
|
2014-05-31 21:04:53 +00:00
|
|
|
begin
|
|
|
|
rowsRepeated := 0;
|
|
|
|
row := 0;
|
2014-06-01 18:59:29 +00:00
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
rowNode := ATableNode.FindNode('table:table-row');
|
|
|
|
while Assigned(rowNode) do begin
|
2014-06-01 18:59:29 +00:00
|
|
|
// These nodes occur due to indentation spaces which are not skipped
|
|
|
|
// automatically any more due to PreserveWhiteSpace option applied
|
|
|
|
// to ReadXMLFile
|
|
|
|
if rowNode.NodeName = '#text' then begin
|
|
|
|
rowNode := rowNode.NextSibling;
|
|
|
|
Continue;
|
|
|
|
end;
|
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
// Read rowstyle
|
|
|
|
rowStyleName := GetAttrValue(rowNode, 'table:style-name');
|
|
|
|
rowStyleIndex := FindRowStyleByName(rowStyleName);
|
|
|
|
rowStyle := TRowStyleData(FRowStyleList[rowStyleIndex]);
|
|
|
|
rowHeight := rowStyle.RowHeight; // in mm (see ReadRowStyles)
|
|
|
|
rowHeight := mmToPts(rowHeight) / Workbook.GetDefaultFontSize;
|
|
|
|
if rowHeight > ROW_HEIGHT_CORRECTION
|
|
|
|
then rowHeight := rowHeight - ROW_HEIGHT_CORRECTION // in "lines"
|
|
|
|
else rowHeight := 0;
|
|
|
|
autoRowHeight := rowStyle.AutoRowHeight;
|
|
|
|
|
|
|
|
col := 0;
|
|
|
|
|
|
|
|
//process each cell of the row
|
|
|
|
cellNode := rowNode.FindNode('table:table-cell');
|
|
|
|
while Assigned(cellNode) do begin
|
2014-06-01 18:59:29 +00:00
|
|
|
|
|
|
|
// These nodes occur due to indentation spaces which are not skipped
|
|
|
|
// automatically any more due to PreserveWhiteSpace option applied
|
|
|
|
// to ReadXMLFile
|
|
|
|
if cellNode.NodeName = '#text' then begin
|
|
|
|
cellNode := cellNode.NextSibling;
|
|
|
|
Continue;
|
|
|
|
end;
|
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
// select this cell value's type
|
|
|
|
paramValueType := GetAttrValue(CellNode, 'office:value-type');
|
|
|
|
paramFormula := GetAttrValue(CellNode, 'table:formula');
|
|
|
|
tableStyleName := GetAttrValue(CellNode, 'table:style-name');
|
|
|
|
|
|
|
|
if paramValueType = 'string' then
|
|
|
|
ReadLabel(row, col, cellNode)
|
|
|
|
else if (paramValueType = 'float') or (paramValueType = 'percentage') then
|
|
|
|
ReadNumber(row, col, cellNode)
|
|
|
|
else if (paramValueType = 'date') or (paramValueType = 'time') then
|
2014-06-02 22:07:40 +00:00
|
|
|
ReadDateTime(row, col, cellNode)
|
2014-05-31 21:04:53 +00:00
|
|
|
else if (paramValueType = '') and (tableStyleName <> '') then
|
|
|
|
ReadBlank(row, col, cellNode)
|
|
|
|
else if ParamFormula <> '' then
|
|
|
|
ReadLabel(row, col, cellNode);
|
|
|
|
|
|
|
|
paramColsRepeated := GetAttrValue(cellNode, 'table:number-columns-repeated');
|
|
|
|
if paramColsRepeated = '' then paramColsRepeated := '1';
|
|
|
|
col := col + StrToInt(paramColsRepeated);
|
|
|
|
|
|
|
|
cellNode := cellNode.NextSibling;
|
|
|
|
end; //while Assigned(cellNode)
|
|
|
|
|
|
|
|
paramRowsRepeated := GetAttrValue(RowNode, 'table:number-rows-repeated');
|
|
|
|
if paramRowsRepeated = '' then
|
|
|
|
rowsRepeated := 1
|
|
|
|
else
|
|
|
|
rowsRepeated := StrToInt(paramRowsRepeated);
|
|
|
|
|
|
|
|
// Transfer non-default row heights to sheet's rows
|
|
|
|
if not autoRowHeight then
|
|
|
|
for i:=1 to rowsRepeated do
|
|
|
|
FWorksheet.WriteRowHeight(row + i - 1, rowHeight);
|
|
|
|
|
|
|
|
row := row + rowsRepeated;
|
|
|
|
|
|
|
|
rowNode := rowNode.NextSibling;
|
|
|
|
end; // while Assigned(rowNode)
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TsSpreadOpenDocReader.ReadRowStyle(AStyleNode: TDOMNode);
|
|
|
|
var
|
|
|
|
styleName: String;
|
|
|
|
styleChildNode: TDOMNode;
|
|
|
|
rowHeight: Double;
|
|
|
|
auto: Boolean;
|
|
|
|
s: String;
|
|
|
|
rowStyle: TRowStyleData;
|
|
|
|
begin
|
|
|
|
styleName := GetAttrValue(AStyleNode, 'style:name');
|
|
|
|
styleChildNode := AStyleNode.FirstChild;
|
|
|
|
rowHeight := -1;
|
|
|
|
auto := false;
|
|
|
|
|
|
|
|
while Assigned(styleChildNode) do begin
|
|
|
|
if styleChildNode.NodeName = 'style:table-row-properties' then begin
|
|
|
|
s := GetAttrValue(styleChildNode, 'style:row-height');
|
|
|
|
if s <> '' then
|
|
|
|
rowHeight := PtsToMm(HTMLLengthStrToPts(s)); // convert to mm
|
|
|
|
s := GetAttrValue(styleChildNode, 'style:use-optimal-row-height');
|
|
|
|
if s = 'true' then
|
|
|
|
auto := true;
|
|
|
|
end;
|
|
|
|
styleChildNode := styleChildNode.NextSibling;
|
|
|
|
end;
|
|
|
|
|
|
|
|
rowStyle := TRowStyleData.Create;
|
|
|
|
rowStyle.Name := styleName;
|
|
|
|
rowStyle.RowHeight := rowHeight;
|
|
|
|
rowStyle.AutoRowHeight := auto;
|
|
|
|
FRowStyleList.Add(rowStyle);
|
|
|
|
end;
|
|
|
|
|
2014-05-26 22:27:07 +00:00
|
|
|
procedure TsSpreadOpenDocReader.ReadStyles(AStylesNode: TDOMNode);
|
|
|
|
var
|
2014-05-27 13:09:23 +00:00
|
|
|
fs: TFormatSettings;
|
2014-05-28 16:23:50 +00:00
|
|
|
style: TCellStyleData;
|
2014-05-26 22:27:07 +00:00
|
|
|
styleNode: TDOMNode;
|
|
|
|
styleChildNode: TDOMNode;
|
2014-05-28 16:23:50 +00:00
|
|
|
colStyle: TColumnStyleData;
|
|
|
|
colWidth: Double;
|
2014-05-26 22:27:07 +00:00
|
|
|
family: String;
|
|
|
|
styleName: String;
|
|
|
|
styleIndex: Integer;
|
|
|
|
numFmtName: String;
|
|
|
|
numFmtIndex: Integer;
|
|
|
|
numFmtIndexDefault: Integer;
|
|
|
|
wrap: Boolean;
|
2014-05-28 07:53:50 +00:00
|
|
|
txtRot: TsTextRotation;
|
2014-05-28 16:23:50 +00:00
|
|
|
vertAlign: TsVertAlignment;
|
|
|
|
horAlign: TsHorAlignment;
|
2014-05-26 22:27:07 +00:00
|
|
|
borders: TsCellBorders;
|
2014-05-27 13:09:23 +00:00
|
|
|
borderStyles: TsCellBorderStyles;
|
2014-05-27 22:12:48 +00:00
|
|
|
bkClr: TsColorValue;
|
2014-05-26 22:27:07 +00:00
|
|
|
s: String;
|
2014-05-27 13:09:23 +00:00
|
|
|
|
|
|
|
procedure SetBorderStyle(ABorder: TsCellBorder; AStyleValue: String);
|
2014-05-27 22:12:48 +00:00
|
|
|
const
|
|
|
|
EPS = 0.1; // takes care of rounding errors for line widths
|
2014-05-27 13:09:23 +00:00
|
|
|
var
|
|
|
|
L: TStringList;
|
|
|
|
i: Integer;
|
|
|
|
isSolid: boolean;
|
|
|
|
s: String;
|
|
|
|
wid: Double;
|
|
|
|
linestyle: String;
|
2014-05-27 22:12:48 +00:00
|
|
|
rgb: TsColorValue;
|
2014-05-27 13:09:23 +00:00
|
|
|
p: Integer;
|
|
|
|
begin
|
|
|
|
L := TStringList.Create;
|
|
|
|
try
|
|
|
|
L.Delimiter := ' ';
|
|
|
|
L.StrictDelimiter := true;
|
|
|
|
L.DelimitedText := AStyleValue;
|
|
|
|
wid := 0;
|
2014-05-27 22:12:48 +00:00
|
|
|
rgb := TsColorValue(-1);
|
2014-05-27 13:09:23 +00:00
|
|
|
linestyle := '';
|
|
|
|
for i:=0 to L.Count-1 do begin
|
|
|
|
s := L[i];
|
2014-05-27 22:12:48 +00:00
|
|
|
if (s = 'solid') or (s = 'dashed') or (s = 'fine-dashed') or (s = 'dotted') or (s = 'double')
|
|
|
|
then begin
|
|
|
|
linestyle := s;
|
|
|
|
continue;
|
2014-05-27 13:09:23 +00:00
|
|
|
end;
|
|
|
|
p := pos('pt', s);
|
|
|
|
if p = Length(s)-1 then begin
|
|
|
|
wid := StrToFloat(copy(s, 1, p-1), fs);
|
|
|
|
continue;
|
|
|
|
end;
|
|
|
|
p := pos('mm', s);
|
|
|
|
if p = Length(s)-1 then begin
|
|
|
|
wid := mmToPts(StrToFloat(copy(s, 1, p-1), fs));
|
|
|
|
Continue;
|
|
|
|
end;
|
|
|
|
p := pos('cm', s);
|
|
|
|
if p = Length(s)-1 then begin
|
|
|
|
wid := cmToPts(StrToFloat(copy(s, 1, p-1), fs));
|
|
|
|
Continue;
|
|
|
|
end;
|
2014-05-27 22:12:48 +00:00
|
|
|
rgb := HTMLColorStrToColor(s);
|
2014-05-27 13:09:23 +00:00
|
|
|
end;
|
|
|
|
borderStyles[ABorder].LineStyle := lsThin;
|
|
|
|
if (linestyle = 'solid') then begin
|
2014-05-27 22:12:48 +00:00
|
|
|
if (wid >= 3 - EPS) then borderStyles[ABorder].LineStyle := lsThick
|
|
|
|
else if (wid >= 2 - EPS) then borderStyles[ABorder].LineStyle := lsMedium
|
2014-05-27 13:09:23 +00:00
|
|
|
end else
|
|
|
|
if (linestyle = 'dotted') then
|
|
|
|
borderStyles[ABorder].LineStyle := lsHair
|
|
|
|
else
|
|
|
|
if (linestyle = 'dashed') then
|
|
|
|
borderStyles[ABorder].LineStyle := lsDashed
|
|
|
|
else
|
|
|
|
if (linestyle = 'fine-dashed') then
|
2014-05-27 22:12:48 +00:00
|
|
|
borderStyles[ABorder].LineStyle := lsDotted
|
|
|
|
else
|
|
|
|
if (linestyle = 'double') then
|
|
|
|
borderStyles[ABorder].LineStyle := lsDouble;
|
|
|
|
borderStyles[ABorder].Color := IfThen(rgb = TsColorValue(-1),
|
|
|
|
scBlack, Workbook.AddColorToPalette(rgb));
|
2014-05-27 13:09:23 +00:00
|
|
|
finally
|
|
|
|
L.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-05-26 22:27:07 +00:00
|
|
|
begin
|
|
|
|
if not Assigned(AStylesNode) then
|
|
|
|
exit;
|
|
|
|
|
2014-05-27 13:09:23 +00:00
|
|
|
fs := DefaultFormatSettings;
|
|
|
|
fs.DecimalSeparator := '.';
|
|
|
|
|
2014-05-26 22:27:07 +00:00
|
|
|
numFmtIndexDefault := NumFormatList.FindByName('N0');
|
|
|
|
|
|
|
|
styleNode := AStylesNode.FirstChild;
|
|
|
|
while Assigned(styleNode) do begin
|
|
|
|
if styleNode.NodeName = 'style:style' then begin
|
|
|
|
family := GetAttrValue(styleNode, 'style:family');
|
2014-05-28 16:23:50 +00:00
|
|
|
|
|
|
|
// Column styles
|
2014-05-31 21:04:53 +00:00
|
|
|
if family = 'table-column' then
|
|
|
|
ReadColumnStyle(styleNode);
|
|
|
|
|
|
|
|
// Row styles
|
|
|
|
if family = 'table-row' then
|
|
|
|
ReadRowStyle(styleNode);
|
2014-05-28 16:23:50 +00:00
|
|
|
|
|
|
|
// Cell styles
|
2014-05-26 22:27:07 +00:00
|
|
|
if family = 'table-cell' then begin
|
|
|
|
styleName := GetAttrValue(styleNode, 'style:name');
|
|
|
|
numFmtName := GetAttrValue(styleNode, 'style:data-style-name');
|
|
|
|
numFmtIndex := -1;
|
|
|
|
if numFmtName <> '' then numFmtIndex := NumFormatList.FindByName(numFmtName);
|
|
|
|
if numFmtIndex = -1 then numFmtIndex := numFmtIndexDefault;
|
|
|
|
|
|
|
|
borders := [];
|
|
|
|
wrap := false;
|
2014-05-27 22:12:48 +00:00
|
|
|
bkClr := TsColorValue(-1);
|
2014-05-28 07:53:50 +00:00
|
|
|
txtRot := trHorizontal;
|
2014-05-28 16:23:50 +00:00
|
|
|
horAlign := haDefault;
|
|
|
|
vertAlign := vaDefault;
|
2014-05-26 22:27:07 +00:00
|
|
|
|
|
|
|
styleChildNode := styleNode.FirstChild;
|
|
|
|
while Assigned(styleChildNode) do begin
|
|
|
|
if styleChildNode.NodeName = 'style:table-cell-properties' then begin
|
2014-05-27 13:58:57 +00:00
|
|
|
// Background color
|
|
|
|
s := GetAttrValue(styleChildNode, 'fo:background-color');
|
2014-05-27 22:12:48 +00:00
|
|
|
if (s <> '') and (s <> 'transparent') then
|
|
|
|
bkClr := HTMLColorStrToColor(s);
|
2014-05-26 22:27:07 +00:00
|
|
|
// Borders
|
2014-05-27 13:09:23 +00:00
|
|
|
s := GetAttrValue(styleChildNode, 'fo:border');
|
|
|
|
if (s <>'') then begin
|
|
|
|
borders := borders + [cbNorth, cbSouth, cbEast, cbWest];
|
|
|
|
SetBorderStyle(cbNorth, s);
|
|
|
|
SetBorderStyle(cbSouth, s);
|
|
|
|
SetBorderStyle(cbEast, s);
|
|
|
|
SetBorderStyle(cbWest, s);
|
|
|
|
end;
|
2014-05-26 22:27:07 +00:00
|
|
|
s := GetAttrValue(styleChildNode, 'fo:border-top');
|
2014-05-27 13:09:23 +00:00
|
|
|
if (s <> '') and (s <> 'none') then begin
|
|
|
|
Include(borders, cbNorth);
|
|
|
|
SetBorderStyle(cbNorth, s);
|
|
|
|
end;
|
2014-05-26 22:27:07 +00:00
|
|
|
s := GetAttrValue(styleChildNode, 'fo:border-right');
|
2014-05-27 13:09:23 +00:00
|
|
|
if (s <> '') and (s <> 'none') then begin
|
|
|
|
Include(borders, cbEast);
|
|
|
|
SetBorderStyle(cbEast, s);
|
|
|
|
end;
|
2014-05-26 22:27:07 +00:00
|
|
|
s := GetAttrValue(styleChildNode, 'fo:border-bottom');
|
2014-05-27 13:09:23 +00:00
|
|
|
if (s <> '') and (s <> 'none') then begin
|
|
|
|
Include(borders, cbSouth);
|
|
|
|
SetBorderStyle(cbSouth, s);
|
|
|
|
end;
|
2014-05-26 22:27:07 +00:00
|
|
|
s := GetAttrValue(styleChildNode, 'fo:border-left');
|
2014-05-27 13:09:23 +00:00
|
|
|
if (s <> '') and (s <> 'none') then begin
|
|
|
|
Include(borders, cbWest);
|
|
|
|
SetBorderStyle(cbWest, s);
|
|
|
|
end;
|
2014-05-26 22:27:07 +00:00
|
|
|
|
|
|
|
// Text wrap
|
|
|
|
s := GetAttrValue(styleChildNode, 'fo:wrap-option');
|
|
|
|
wrap := (s='wrap');
|
2014-05-28 07:53:50 +00:00
|
|
|
|
|
|
|
// Test rotation
|
|
|
|
s := GetAttrValue(styleChildNode, 'style:rotation-angle');
|
|
|
|
if s = '90' then
|
|
|
|
txtRot := rt90DegreeCounterClockwiseRotation
|
|
|
|
else if s = '270' then
|
|
|
|
txtRot := rt90DegreeClockwiseRotation;
|
|
|
|
s := GetAttrValue(styleChildNode, 'style:direction');
|
|
|
|
if s = 'ttb' then
|
|
|
|
txtRot := rtStacked;
|
|
|
|
|
2014-05-28 16:23:50 +00:00
|
|
|
// Vertical text alignment
|
|
|
|
s := GetAttrValue(styleChildNode, 'style:vertical-align');
|
|
|
|
if s = 'top' then
|
|
|
|
vertAlign := vaTop
|
|
|
|
else if s = 'middle' then
|
|
|
|
vertAlign := vaCenter
|
|
|
|
else if s = 'bottom' then
|
|
|
|
vertAlign := vaBottom;
|
|
|
|
|
2014-05-26 22:27:07 +00:00
|
|
|
end else
|
|
|
|
if styleChildNode.NodeName = 'style:paragraph-properties' then begin
|
2014-05-28 16:23:50 +00:00
|
|
|
// Horizontal text alignment
|
|
|
|
s := GetAttrValue(styleChildNode, 'fo:text-align');
|
|
|
|
if s = 'start' then
|
|
|
|
horAlign := haLeft
|
|
|
|
else if s = 'end' then
|
|
|
|
horAlign := haRight
|
|
|
|
else if s = 'center' then
|
|
|
|
horAlign := haCenter;
|
2014-05-26 22:27:07 +00:00
|
|
|
end;
|
|
|
|
styleChildNode := styleChildNode.NextSibling;
|
|
|
|
end;
|
|
|
|
|
2014-05-28 16:23:50 +00:00
|
|
|
style := TCellStyleData.Create;
|
2014-05-26 22:27:07 +00:00
|
|
|
style.Name := stylename;
|
|
|
|
style.FontIndex := 0;
|
|
|
|
style.NumFormatIndex := numFmtIndex;
|
2014-05-28 16:23:50 +00:00
|
|
|
style.HorAlignment := horAlign;
|
|
|
|
style.VertAlignment := vertAlign;
|
2014-05-26 22:27:07 +00:00
|
|
|
style.WordWrap := wrap;
|
2014-05-28 07:53:50 +00:00
|
|
|
style.TextRotation := txtRot;
|
2014-05-26 22:27:07 +00:00
|
|
|
style.Borders := borders;
|
2014-05-27 13:09:23 +00:00
|
|
|
style.BorderStyles := borderStyles;
|
2014-05-27 22:12:48 +00:00
|
|
|
style.BackgroundColor := IfThen(bkClr = TsColorValue(-1), scNotDefined,
|
|
|
|
Workbook.AddColorToPalette(bkClr));
|
2014-05-26 22:27:07 +00:00
|
|
|
|
2014-05-28 16:23:50 +00:00
|
|
|
styleIndex := FCellStyleList.Add(style);
|
2014-05-26 22:27:07 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
styleNode := styleNode.NextSibling;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-05-25 22:05:37 +00:00
|
|
|
|
2008-02-24 13:18:34 +00:00
|
|
|
{ TsSpreadOpenDocWriter }
|
|
|
|
|
2014-05-14 23:17:46 +00:00
|
|
|
procedure TsSpreadOpenDocWriter.CreateNumFormatList;
|
|
|
|
begin
|
|
|
|
FreeAndNil(FNumFormatList);
|
2014-05-20 16:13:48 +00:00
|
|
|
FNumFormatList := TsSpreadOpenDocNumFormatList.Create(Workbook);
|
2014-05-14 23:17:46 +00:00
|
|
|
end;
|
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
procedure TsSpreadOpenDocWriter.ListAllColumnStyles;
|
|
|
|
var
|
|
|
|
i, j, c: Integer;
|
|
|
|
sheet: TsWorksheet;
|
|
|
|
found: Boolean;
|
|
|
|
colstyle: TColumnStyleData;
|
|
|
|
w: Double;
|
|
|
|
multiplier: Double;
|
|
|
|
begin
|
|
|
|
{ At first, add the default column width }
|
|
|
|
colStyle := TColumnStyleData.Create;
|
|
|
|
colStyle.Name := 'co1';
|
|
|
|
colStyle.ColWidth := Workbook.DefaultColWidth;
|
|
|
|
FColumnStyleList.Add(colStyle);
|
|
|
|
|
|
|
|
for i:=0 to Workbook.GetWorksheetCount-1 do begin
|
|
|
|
sheet := Workbook.GetWorksheetByIndex(i);
|
|
|
|
for c:=0 to sheet.GetLastColIndex do begin
|
|
|
|
w := sheet.GetColWidth(c);
|
|
|
|
// Look for this width in the current ColumnStyleList
|
|
|
|
found := false;
|
|
|
|
for j := 0 to FColumnStyleList.Count-1 do
|
|
|
|
if SameValue(TColumnStyleData(FColumnStyleList[j]).ColWidth, w, COLWIDTH_EPS)
|
|
|
|
then begin
|
|
|
|
found := true;
|
|
|
|
break;
|
|
|
|
end;
|
|
|
|
// Not found? Then add the column as new column style
|
|
|
|
if not found then begin
|
|
|
|
colStyle := TColumnStyleData.Create;
|
|
|
|
colStyle.Name := Format('co%d', [FColumnStyleList.Count+1]);
|
|
|
|
colStyle.ColWidth := w;
|
|
|
|
FColumnStyleList.Add(colStyle);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ fpspreadsheet's column width is the count of '0' characters of the
|
|
|
|
default font. On average, the width of the '0' is about half of the
|
|
|
|
point size of the font. --> we can convert the fps col width to pts and
|
|
|
|
then to millimeters. }
|
|
|
|
multiplier := Workbook.GetFont(0).Size / 2;
|
|
|
|
for i:=0 to FColumnStyleList.Count-1 do begin
|
|
|
|
w := TColumnStyleData(FColumnStyleList[i]).ColWidth * multiplier;
|
|
|
|
TColumnStyleData(FColumnStyleList[i]).ColWidth := PtsToMM(w);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TsSpreadOpenDocWriter.ListAllRowStyles;
|
|
|
|
var
|
|
|
|
i, j, r: Integer;
|
|
|
|
sheet: TsWorksheet;
|
|
|
|
row: PRow;
|
|
|
|
found: Boolean;
|
|
|
|
rowstyle: TRowStyleData;
|
|
|
|
h, multiplier: Double;
|
|
|
|
begin
|
|
|
|
{ At first, add the default row height }
|
|
|
|
{ Initially, row height units will be the same as in the sheet, i.e. in "lines" }
|
|
|
|
rowStyle := TRowStyleData.Create;
|
|
|
|
rowStyle.Name := 'ro1';
|
|
|
|
rowStyle.RowHeight := Workbook.DefaultRowHeight;
|
|
|
|
rowStyle.AutoRowHeight := true;
|
|
|
|
FRowStyleList.Add(rowStyle);
|
|
|
|
|
|
|
|
for i:=0 to Workbook.GetWorksheetCount-1 do begin
|
|
|
|
sheet := Workbook.GetWorksheetByIndex(i);
|
|
|
|
for r:=0 to sheet.GetLastRowIndex do begin
|
|
|
|
row := sheet.FindRow(r);
|
|
|
|
if row <> nil then begin
|
|
|
|
h := sheet.GetRowHeight(r);
|
|
|
|
// Look for this height in the current RowStyleList
|
|
|
|
found := false;
|
|
|
|
for j:=0 to FRowStyleList.Count-1 do
|
|
|
|
if SameValue(TRowStyleData(FRowStyleList[j]).RowHeight, h, ROWHEIGHT_EPS) and
|
|
|
|
(not TRowStyleData(FRowStyleList[j]).AutoRowHeight)
|
|
|
|
then begin
|
|
|
|
found := true;
|
|
|
|
break;
|
|
|
|
end;
|
|
|
|
// Not found? Then add the row as a new row style
|
|
|
|
if not found then begin
|
|
|
|
rowStyle := TRowStyleData.Create;
|
|
|
|
rowStyle.Name := Format('ro%d', [FRowStyleList.Count+1]);
|
|
|
|
rowStyle.RowHeight := h;
|
|
|
|
rowStyle.AutoRowHeight := false;
|
|
|
|
FRowStyleList.Add(rowStyle);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ fpspreadsheet's row heights are measured as line count of the default font.
|
|
|
|
Using the default font size (which is in points) we convert the line count
|
|
|
|
to points and then to millimeters as needed by ods. }
|
|
|
|
multiplier := Workbook.GetDefaultFontSize;;
|
|
|
|
for i:=0 to FRowStyleList.Count-1 do begin
|
|
|
|
h := (TRowStyleData(FRowStyleList[i]).RowHeight + ROW_HEIGHT_CORRECTION) * multiplier;
|
|
|
|
TRowStyleData(FRowStyleList[i]).RowHeight := PtsToMM(h);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2010-12-08 10:24:15 +00:00
|
|
|
procedure TsSpreadOpenDocWriter.WriteMimetype;
|
2008-02-24 13:18:34 +00:00
|
|
|
begin
|
2009-01-28 22:36:41 +00:00
|
|
|
FMimetype := 'application/vnd.oasis.opendocument.spreadsheet';
|
2010-12-08 10:24:15 +00:00
|
|
|
end;
|
2008-02-24 13:18:34 +00:00
|
|
|
|
2010-12-08 10:24:15 +00:00
|
|
|
procedure TsSpreadOpenDocWriter.WriteMetaInfManifest;
|
|
|
|
begin
|
2008-02-24 13:18:34 +00:00
|
|
|
FMetaInfManifest :=
|
|
|
|
XML_HEADER + LineEnding +
|
|
|
|
'<manifest:manifest xmlns:manifest="' + SCHEMAS_XMLNS_MANIFEST + '">' + LineEnding +
|
|
|
|
' <manifest:file-entry manifest:media-type="application/vnd.oasis.opendocument.spreadsheet" manifest:full-path="/" />' + LineEnding +
|
|
|
|
' <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="content.xml" />' + LineEnding +
|
|
|
|
' <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="styles.xml" />' + LineEnding +
|
|
|
|
' <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="meta.xml" />' + LineEnding +
|
|
|
|
' <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="settings.xml" />' + LineEnding +
|
|
|
|
'</manifest:manifest>';
|
2010-12-08 10:24:15 +00:00
|
|
|
end;
|
2009-01-28 22:36:41 +00:00
|
|
|
|
2010-12-08 10:24:15 +00:00
|
|
|
procedure TsSpreadOpenDocWriter.WriteMeta;
|
|
|
|
begin
|
2008-02-24 13:18:34 +00:00
|
|
|
FMeta :=
|
|
|
|
XML_HEADER + LineEnding +
|
|
|
|
'<office:document-meta xmlns:office="' + SCHEMAS_XMLNS_OFFICE +
|
|
|
|
'" xmlns:dcterms="' + SCHEMAS_XMLNS_DCTERMS +
|
|
|
|
'" xmlns:meta="' + SCHEMAS_XMLNS_META +
|
|
|
|
'" xmlns="' + SCHEMAS_XMLNS +
|
|
|
|
'" xmlns:ex="' + SCHEMAS_XMLNS + '">' + LineEnding +
|
|
|
|
' <office:meta>' + LineEnding +
|
|
|
|
' <meta:generator>FPSpreadsheet Library</meta:generator>' + LineEnding +
|
|
|
|
' <meta:document-statistic />' + LineEnding +
|
|
|
|
' </office:meta>' + LineEnding +
|
|
|
|
'</office:document-meta>';
|
2010-12-08 10:24:15 +00:00
|
|
|
end;
|
2008-02-24 13:18:34 +00:00
|
|
|
|
2010-12-08 10:24:15 +00:00
|
|
|
procedure TsSpreadOpenDocWriter.WriteSettings;
|
|
|
|
begin
|
2008-02-24 13:18:34 +00:00
|
|
|
FSettings :=
|
|
|
|
XML_HEADER + LineEnding +
|
|
|
|
'<office:document-settings xmlns:office="' + SCHEMAS_XMLNS_OFFICE +
|
|
|
|
'" xmlns:config="' + SCHEMAS_XMLNS_CONFIG +
|
|
|
|
'" xmlns:ooo="' + SCHEMAS_XMLNS_OOO + '">' + LineEnding +
|
|
|
|
'<office:settings>' + LineEnding +
|
|
|
|
' <config:config-item-set config:name="ooo:view-settings">' + LineEnding +
|
|
|
|
' <config:config-item-map-indexed config:name="Views">' + LineEnding +
|
|
|
|
' <config:config-item-map-entry>' + LineEnding +
|
|
|
|
' <config:config-item config:name="ActiveTable" config:type="string">Tabelle1</config:config-item>' + LineEnding +
|
|
|
|
' <config:config-item config:name="ZoomValue" config:type="int">100</config:config-item>' + LineEnding +
|
|
|
|
' <config:config-item config:name="PageViewZoomValue" config:type="int">100</config:config-item>' + LineEnding +
|
|
|
|
' <config:config-item config:name="ShowPageBreakPreview" config:type="boolean">false</config:config-item>' + LineEnding +
|
|
|
|
' <config:config-item config:name="HasColumnRowHeaders" config:type="boolean">true</config:config-item>' + LineEnding +
|
|
|
|
' <config:config-item-map-named config:name="Tables">' + LineEnding +
|
|
|
|
' <config:config-item-map-entry config:name="Tabelle1">' + LineEnding +
|
|
|
|
' <config:config-item config:name="CursorPositionX" config:type="int">3</config:config-item>' + LineEnding +
|
|
|
|
' <config:config-item config:name="CursorPositionY" config:type="int">2</config:config-item>' + LineEnding +
|
|
|
|
' </config:config-item-map-entry>' + LineEnding +
|
|
|
|
' </config:config-item-map-named>' + LineEnding +
|
|
|
|
' </config:config-item-map-entry>' + LineEnding +
|
|
|
|
' </config:config-item-map-indexed>' + LineEnding +
|
|
|
|
' </config:config-item-set>' + LineEnding +
|
|
|
|
' </office:settings>' + LineEnding +
|
|
|
|
'</office:document-settings>';
|
2010-12-08 10:24:15 +00:00
|
|
|
end;
|
2009-01-28 22:36:41 +00:00
|
|
|
|
2010-12-08 10:24:15 +00:00
|
|
|
procedure TsSpreadOpenDocWriter.WriteStyles;
|
|
|
|
begin
|
2008-02-24 13:18:34 +00:00
|
|
|
FStyles :=
|
|
|
|
XML_HEADER + LineEnding +
|
|
|
|
'<office:document-styles xmlns:office="' + SCHEMAS_XMLNS_OFFICE +
|
|
|
|
'" xmlns:fo="' + SCHEMAS_XMLNS_FO +
|
|
|
|
'" xmlns:style="' + SCHEMAS_XMLNS_STYLE +
|
|
|
|
'" xmlns:svg="' + SCHEMAS_XMLNS_SVG +
|
|
|
|
'" xmlns:table="' + SCHEMAS_XMLNS_TABLE +
|
|
|
|
'" xmlns:text="' + SCHEMAS_XMLNS_TEXT +
|
|
|
|
'" xmlns:v="' + SCHEMAS_XMLNS_V + '">' + LineEnding +
|
|
|
|
'<office:font-face-decls>' + LineEnding +
|
2010-12-08 10:24:15 +00:00
|
|
|
' <style:font-face style:name="Arial" svg:font-family="Arial" />' + LineEnding +
|
2008-02-24 13:18:34 +00:00
|
|
|
'</office:font-face-decls>' + LineEnding +
|
|
|
|
'<office:styles>' + LineEnding +
|
2010-12-08 10:24:15 +00:00
|
|
|
' <style:style style:name="Default" style:family="table-cell">' + LineEnding +
|
|
|
|
' <style:text-properties fo:font-size="10" style:font-name="Arial" />' + LineEnding +
|
|
|
|
' </style:style>' + LineEnding +
|
2008-02-24 13:18:34 +00:00
|
|
|
'</office:styles>' + LineEnding +
|
|
|
|
'<office:automatic-styles>' + LineEnding +
|
2010-12-08 10:24:15 +00:00
|
|
|
' <style:page-layout style:name="pm1">' + LineEnding +
|
|
|
|
' <style:page-layout-properties fo:margin-top="1.25cm" fo:margin-bottom="1.25cm" fo:margin-left="1.905cm" fo:margin-right="1.905cm" />' + LineEnding +
|
|
|
|
' <style:header-style>' + LineEnding +
|
|
|
|
' <style:header-footer-properties fo:min-height="0.751cm" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-bottom="0.25cm" fo:margin-top="0cm" />' + LineEnding +
|
|
|
|
' </style:header-style>' + LineEnding +
|
|
|
|
' <style:footer-style>' + LineEnding +
|
|
|
|
' <style:header-footer-properties fo:min-height="0.751cm" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0.25cm" fo:margin-bottom="0cm" />' + LineEnding +
|
|
|
|
' </style:footer-style>' + LineEnding +
|
|
|
|
' </style:page-layout>' + LineEnding +
|
2008-02-24 13:18:34 +00:00
|
|
|
'</office:automatic-styles>' + LineEnding +
|
|
|
|
'<office:master-styles>' + LineEnding +
|
2010-12-08 10:24:15 +00:00
|
|
|
' <style:master-page style:name="Default" style:page-layout-name="pm1">' + LineEnding +
|
|
|
|
' <style:header />' + LineEnding +
|
|
|
|
' <style:header-left style:display="false" />' + LineEnding +
|
|
|
|
' <style:footer />' + LineEnding +
|
|
|
|
' <style:footer-left style:display="false" />' + LineEnding +
|
|
|
|
' </style:master-page>' + LineEnding +
|
2008-02-24 13:18:34 +00:00
|
|
|
'</office:master-styles>' + LineEnding +
|
|
|
|
'</office:document-styles>';
|
2009-01-28 22:36:41 +00:00
|
|
|
end;
|
|
|
|
|
2014-04-23 22:29:32 +00:00
|
|
|
procedure TsSpreadOpenDocWriter.WriteContent;
|
2009-01-28 22:36:41 +00:00
|
|
|
var
|
2009-01-29 11:30:38 +00:00
|
|
|
i: Integer;
|
2014-05-31 21:04:53 +00:00
|
|
|
lCellStylesCode: string;
|
|
|
|
lColStylesCode: String;
|
|
|
|
lRowStylesCode: String;
|
2009-01-28 22:36:41 +00:00
|
|
|
begin
|
2014-05-31 21:04:53 +00:00
|
|
|
ListAllColumnStyles;
|
|
|
|
ListAllRowStyles;
|
2014-04-23 22:29:32 +00:00
|
|
|
ListAllFormattingStyles;
|
2011-05-27 13:45:30 +00:00
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
lColStylesCode := WriteColStylesXMLAsString;
|
|
|
|
if lColStylesCode = '' then lColStylesCode :=
|
|
|
|
' <style:style style:name="co1" style:family="table-column">' + LineEnding +
|
|
|
|
' <style:table-column-properties fo:break-before="auto" style:column-width="2.267cm"/>' + LineEnding +
|
|
|
|
' </style:style>' + LineEnding;
|
|
|
|
|
|
|
|
lRowStylesCode := WriteRowStylesXMLAsString;
|
|
|
|
if lRowStylesCode = '' then lRowStylesCode :=
|
|
|
|
' <style:style style:name="ro1" style:family="table-row">' + LineEnding +
|
|
|
|
' <style:table-row-properties style:row-height="0.416cm" fo:break-before="auto" style:use-optimal-row-height="true"/>' + LineEnding +
|
|
|
|
' </style:style>' + LineEnding;
|
|
|
|
|
|
|
|
lCellStylesCode := WriteCellStylesXMLAsString;
|
2011-05-27 13:45:30 +00:00
|
|
|
|
2008-02-24 13:18:34 +00:00
|
|
|
FContent :=
|
|
|
|
XML_HEADER + LineEnding +
|
|
|
|
'<office:document-content xmlns:office="' + SCHEMAS_XMLNS_OFFICE +
|
|
|
|
'" xmlns:fo="' + SCHEMAS_XMLNS_FO +
|
|
|
|
'" xmlns:style="' + SCHEMAS_XMLNS_STYLE +
|
|
|
|
'" xmlns:text="' + SCHEMAS_XMLNS_TEXT +
|
|
|
|
'" xmlns:table="' + SCHEMAS_XMLNS_TABLE +
|
|
|
|
'" xmlns:svg="' + SCHEMAS_XMLNS_SVG +
|
|
|
|
'" xmlns:number="' + SCHEMAS_XMLNS_NUMBER +
|
|
|
|
'" xmlns:meta="' + SCHEMAS_XMLNS_META +
|
|
|
|
'" xmlns:chart="' + SCHEMAS_XMLNS_CHART +
|
|
|
|
'" xmlns:dr3d="' + SCHEMAS_XMLNS_DR3D +
|
|
|
|
'" xmlns:math="' + SCHEMAS_XMLNS_MATH +
|
|
|
|
'" xmlns:form="' + SCHEMAS_XMLNS_FORM +
|
|
|
|
'" xmlns:script="' + SCHEMAS_XMLNS_SCRIPT +
|
|
|
|
'" xmlns:ooo="' + SCHEMAS_XMLNS_OOO +
|
|
|
|
'" xmlns:ooow="' + SCHEMAS_XMLNS_OOOW +
|
|
|
|
'" xmlns:oooc="' + SCHEMAS_XMLNS_OOOC +
|
|
|
|
'" xmlns:dom="' + SCHEMAS_XMLNS_DOM +
|
|
|
|
'" xmlns:xforms="' + SCHEMAS_XMLNS_XFORMS +
|
|
|
|
'" xmlns:xsd="' + SCHEMAS_XMLNS_XSD +
|
|
|
|
'" xmlns:xsi="' + SCHEMAS_XMLNS_XSI + '">' + LineEnding +
|
2009-01-28 22:36:41 +00:00
|
|
|
' <office:scripts />' + LineEnding +
|
|
|
|
|
|
|
|
// Fonts
|
|
|
|
' <office:font-face-decls>' + LineEnding +
|
|
|
|
' <style:font-face style:name="Arial" svg:font-family="Arial" xmlns:v="urn:schemas-microsoft-com:vml" />' + LineEnding +
|
|
|
|
' </office:font-face-decls>' + LineEnding +
|
|
|
|
|
|
|
|
// Automatic styles
|
2009-01-29 11:30:38 +00:00
|
|
|
' <office:automatic-styles>' + LineEnding +
|
2014-05-31 21:04:53 +00:00
|
|
|
lColStylesCode +
|
|
|
|
lRowStylesCode +
|
2009-01-29 11:30:38 +00:00
|
|
|
' <style:style style:name="ta1" style:family="table" style:master-page-name="Default">' + LineEnding +
|
|
|
|
' <style:table-properties table:display="true" style:writing-mode="lr-tb"/>' + LineEnding +
|
|
|
|
' </style:style>' + LineEnding +
|
2014-05-31 21:04:53 +00:00
|
|
|
|
2011-05-27 13:45:30 +00:00
|
|
|
// Automatically Generated Styles
|
2014-05-31 21:04:53 +00:00
|
|
|
lCellStylesCode +
|
2009-01-29 11:30:38 +00:00
|
|
|
' </office:automatic-styles>' + LineEnding +
|
|
|
|
|
|
|
|
// Body
|
|
|
|
' <office:body>' + LineEnding +
|
|
|
|
' <office:spreadsheet>' + LineEnding;
|
|
|
|
|
2009-02-02 09:58:51 +00:00
|
|
|
// Write all worksheets
|
2014-04-23 22:29:32 +00:00
|
|
|
for i := 0 to Workbook.GetWorksheetCount - 1 do
|
|
|
|
WriteWorksheet(Workbook.GetWorksheetByIndex(i));
|
2009-01-28 22:36:41 +00:00
|
|
|
|
|
|
|
FContent := FContent +
|
|
|
|
' </office:spreadsheet>' + LineEnding +
|
|
|
|
' </office:body>' + LineEnding +
|
2008-02-24 13:18:34 +00:00
|
|
|
'</office:document-content>';
|
|
|
|
end;
|
|
|
|
|
2009-01-29 11:30:38 +00:00
|
|
|
procedure TsSpreadOpenDocWriter.WriteWorksheet(CurSheet: TsWorksheet);
|
|
|
|
var
|
|
|
|
j, k: Integer;
|
|
|
|
CurCell: PCell;
|
2009-01-29 11:54:07 +00:00
|
|
|
CurRow: array of PCell;
|
2014-05-26 15:27:35 +00:00
|
|
|
LastColIndex: Cardinal;
|
2009-09-03 12:04:04 +00:00
|
|
|
LCell: TCell;
|
2009-09-02 22:03:01 +00:00
|
|
|
AVLNode: TAVLTreeNode;
|
2014-05-31 21:04:53 +00:00
|
|
|
defFontSize: Single;
|
|
|
|
h, h_mm: Double;
|
|
|
|
styleName: String;
|
|
|
|
rowStyleData: TRowStyleData;
|
|
|
|
row: PRow;
|
2009-01-29 11:30:38 +00:00
|
|
|
begin
|
2014-05-26 15:27:35 +00:00
|
|
|
LastColIndex := CurSheet.GetLastColIndex;
|
2014-05-31 21:04:53 +00:00
|
|
|
defFontSize := Workbook.GetFont(0).Size;
|
2009-01-29 11:54:07 +00:00
|
|
|
|
2009-01-29 11:30:38 +00:00
|
|
|
// Header
|
|
|
|
FContent := FContent +
|
2014-05-31 21:04:53 +00:00
|
|
|
' <table:table table:name="' + CurSheet.Name + '" table:style-name="ta1">' + LineEnding;
|
2009-01-29 11:30:38 +00:00
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
// columns
|
|
|
|
FContent := FContent + WriteColumnsXMLAsString(CurSheet);
|
2009-01-29 11:54:07 +00:00
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
// rows and cells
|
|
|
|
// The cells need to be written in order, row by row, cell by cell
|
|
|
|
FContent := FContent + WriteRowsAndCellsXMLAsString(CurSheet);
|
2009-01-29 11:30:38 +00:00
|
|
|
|
|
|
|
// Footer
|
2009-01-29 11:54:07 +00:00
|
|
|
FContent := FContent +
|
|
|
|
' </table:table>' + LineEnding;
|
2009-01-29 11:30:38 +00:00
|
|
|
end;
|
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
function TsSpreadOpenDocWriter.WriteCellStylesXMLAsString: string;
|
2011-05-27 13:45:30 +00:00
|
|
|
var
|
|
|
|
i: Integer;
|
2014-05-31 21:04:53 +00:00
|
|
|
s: String;
|
2011-05-27 13:45:30 +00:00
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
|
|
|
|
for i := 0 to Length(FFormattingStyles) - 1 do
|
|
|
|
begin
|
|
|
|
// Start and Name
|
|
|
|
Result := Result +
|
|
|
|
' <style:style style:name="ce' + IntToStr(i) + '" style:family="table-cell" style:parent-style-name="Default">' + LineEnding;
|
|
|
|
|
|
|
|
// Fields
|
2012-06-13 07:21:00 +00:00
|
|
|
|
|
|
|
// style:text-properties
|
2011-05-27 13:45:30 +00:00
|
|
|
if uffBold in FFormattingStyles[i].UsedFormattingFields then
|
|
|
|
Result := Result +
|
|
|
|
' <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>' + LineEnding;
|
|
|
|
|
2012-06-13 07:21:00 +00:00
|
|
|
// style:table-cell-properties
|
2014-05-31 21:04:53 +00:00
|
|
|
s := WriteBorderStyleXMLAsString(FFormattingStyles[i]) +
|
|
|
|
WriteBackgroundColorStyleXMLAsString(FFormattingStyles[i]) +
|
|
|
|
WriteWordwrapStyleXMLAsString(FFormattingStyles[i]) +
|
|
|
|
WriteTextRotationStyleXMLAsString(FFormattingStyles[i]) +
|
|
|
|
WriteVertAlignmentStyleXMLAsString(FFormattingStyles[i]);
|
|
|
|
if s <> '' then
|
2014-05-28 19:26:08 +00:00
|
|
|
Result := Result +
|
2014-05-31 21:04:53 +00:00
|
|
|
' <style:table-cell-properties ' + s + '/>' + LineEnding;
|
2011-05-27 13:45:30 +00:00
|
|
|
|
2014-05-28 20:52:36 +00:00
|
|
|
// style:paragraph-properties
|
2014-05-31 21:04:53 +00:00
|
|
|
s := WriteHorAlignmentStyleXMLAsString(FFormattingStyles[i]);
|
|
|
|
if s <> '' then
|
2014-05-28 20:52:36 +00:00
|
|
|
Result := Result +
|
2014-05-31 21:04:53 +00:00
|
|
|
' <style:paragraph-properties ' + s + '/>' + LineEnding;
|
|
|
|
|
|
|
|
// End
|
|
|
|
Result := Result +
|
|
|
|
' </style:style>' + LineEnding;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TsSpreadOpenDocWriter.WriteColStylesXMLAsString: string;
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
s: String;
|
|
|
|
colstyle: TColumnStyleData;
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
|
|
|
|
for i := 0 to FColumnStyleList.Count-1 do begin
|
|
|
|
colStyle := TColumnStyleData(FColumnStyleList[i]);
|
|
|
|
|
|
|
|
// Start and Name
|
|
|
|
Result := Result +
|
|
|
|
' <style:style style:name="%s" style:family="table-column">' + LineEnding;
|
|
|
|
|
|
|
|
// Column width
|
|
|
|
Result := Result +
|
|
|
|
' <style:table-column-properties style:column-width="%.3fmm" fo:break-before="auto"/>' + LineEnding;
|
|
|
|
|
|
|
|
// End
|
|
|
|
Result := Result +
|
|
|
|
' </style:style>' + LineEnding;
|
|
|
|
|
|
|
|
Result := Format(Result, [colStyle.Name, colStyle.ColWidth], FPointSeparatorSettings);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TsSpreadOpenDocWriter.WriteColumnsXMLAsString(ASheet: TsWorksheet): String;
|
|
|
|
var
|
|
|
|
lastCol: Integer;
|
|
|
|
j, k: Integer;
|
|
|
|
w, w_mm: Double;
|
|
|
|
widthMultiplier: Double;
|
|
|
|
styleName: String;
|
|
|
|
colsRepeated: Integer;
|
|
|
|
colsRepeatedStr: String;
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
|
|
|
|
widthMultiplier := Workbook.GetFont(0).Size / 2;
|
|
|
|
lastCol := ASheet.GetLastColIndex;
|
|
|
|
|
|
|
|
j := 0;
|
|
|
|
while (j <= lastCol) do begin
|
|
|
|
w := ASheet.GetColWidth(j);
|
|
|
|
// Convert to mm
|
|
|
|
w_mm := PtsToMM(w * widthMultiplier);
|
|
|
|
|
|
|
|
// Find width in ColumnStyleList to retrieve corresponding style name
|
|
|
|
styleName := '';
|
|
|
|
for k := 0 to FColumnStyleList.Count-1 do
|
|
|
|
if SameValue(TColumnStyleData(FColumnStyleList[k]).ColWidth, w_mm, COLWIDTH_EPS) then begin
|
|
|
|
styleName := TColumnStyleData(FColumnStyleList[k]).Name;
|
|
|
|
break;
|
|
|
|
end;
|
|
|
|
if stylename = '' then
|
|
|
|
raise Exception.Create('Column style not found.');
|
|
|
|
|
|
|
|
// Determine value for "number-columns-repeated"
|
|
|
|
colsRepeated := 1;
|
|
|
|
k := j+1;
|
|
|
|
while (k <= lastCol) do begin
|
|
|
|
if ASheet.GetColWidth(k) = w then
|
|
|
|
inc(colsRepeated)
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
inc(k);
|
2014-05-28 20:52:36 +00:00
|
|
|
end;
|
2014-05-31 21:04:53 +00:00
|
|
|
colsRepeatedStr := IfThen(colsRepeated = 1, '', Format(' table:number-columns-repeated="%d"', [colsRepeated]));
|
2014-05-28 20:52:36 +00:00
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
Result := Result + Format(
|
|
|
|
' <table:table-column table:style-name="%s"%s table:default-cell-style-name="Default"/>',
|
|
|
|
[styleName, colsRepeatedStr]) + LineEnding;
|
|
|
|
|
|
|
|
j := j + colsRepeated;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TsSpreadOpenDocWriter.WriteRowsAndCellsXMLAsString(ASheet: TsWorksheet): String;
|
|
|
|
var
|
|
|
|
r, rr: Cardinal; // row index in sheet
|
|
|
|
c, cc: Cardinal; // column index in sheet
|
|
|
|
row: PRow; // sheet row record
|
|
|
|
cell: PCell; // current cell
|
|
|
|
styleName: String;
|
|
|
|
k: Integer;
|
|
|
|
h, h_mm: Single; // row height in "lines" and millimeters, respectively
|
|
|
|
h1: Single;
|
|
|
|
colsRepeated: Integer;
|
|
|
|
rowsRepeated: Integer;
|
|
|
|
colsRepeatedStr: String;
|
|
|
|
rowsRepeatedStr: String;
|
|
|
|
lastCol, lastRow: Cardinal;
|
|
|
|
rowStyleData: TRowStyleData;
|
|
|
|
colData: TColumnData;
|
|
|
|
colStyleData: TColumnStyleData;
|
|
|
|
defFontSize: Single;
|
|
|
|
sameRowStyle: Boolean;
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
|
|
|
|
// some abbreviations...
|
|
|
|
lastCol := ASheet.GetLastColIndex;
|
|
|
|
lastRow := ASheet.GetLastRowIndex;
|
|
|
|
defFontSize := Workbook.GetFont(0).Size;
|
|
|
|
|
|
|
|
// Now loop through all rows
|
|
|
|
r := 0;
|
|
|
|
while (r <= lastRow) do begin
|
|
|
|
// Look for the row style of the current row (r)
|
|
|
|
row := ASheet.FindRow(r);
|
|
|
|
if row = nil then
|
|
|
|
styleName := 'ro1'
|
|
|
|
else begin
|
|
|
|
styleName := '';
|
|
|
|
|
|
|
|
h := row^.Height; // row height in "lines"
|
|
|
|
h_mm := PtsToMM((h + ROW_HEIGHT_CORRECTION) * defFontSize); // in mm
|
|
|
|
for k := 0 to FRowStyleList.Count-1 do begin
|
|
|
|
rowStyleData := TRowStyleData(FRowStyleList[k]);
|
|
|
|
// Compare row heights, but be aware of rounding errors
|
|
|
|
if SameValue(rowStyleData.RowHeight, h_mm, 1E-3) then begin
|
|
|
|
styleName := rowStyleData.Name;
|
|
|
|
break;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
if styleName = '' then
|
|
|
|
raise Exception.Create('Row style not found.');
|
|
|
|
end;
|
|
|
|
|
|
|
|
// Look for empty rows with the same style, they need the "number-rows-repeated" element.
|
|
|
|
rowsRepeated := 1;
|
|
|
|
if ASheet.GetCellCountInRow(r) = 0 then begin
|
|
|
|
rr := r + 1;
|
|
|
|
while (rr <= lastRow) do begin
|
|
|
|
if ASheet.GetCellCountInRow(rr) > 0 then begin
|
|
|
|
break;
|
|
|
|
end;
|
|
|
|
h1 := ASheet.GetRowHeight(rr);
|
|
|
|
if not SameValue(h, h1, ROWHEIGHT_EPS) then
|
|
|
|
break;
|
|
|
|
inc(rr);
|
|
|
|
end;
|
|
|
|
rowsRepeated := rr - r;
|
|
|
|
rowsRepeatedStr := IfThen(rowsRepeated = 1, '',
|
|
|
|
Format('table:number-rows-repeated="%d"', [rowsRepeated]));
|
|
|
|
colsRepeated := lastCol+1;
|
|
|
|
colsRepeatedStr := IfThen(colsRepeated = 1, '',
|
|
|
|
Format('table:number-columns-repeated="%d"', [colsRepeated]));
|
|
|
|
Result := Result + Format(
|
|
|
|
' <table:table-row table:style-name="%s" %s>' + LineEnding +
|
|
|
|
' <table:table-cell %s/>' + LineEnding +
|
|
|
|
' </table:table-row>' + LineEnding,
|
|
|
|
[styleName, rowsRepeatedStr, colsRepeatedStr]);
|
|
|
|
r := rr;
|
|
|
|
continue;
|
|
|
|
end;
|
|
|
|
|
|
|
|
// Now we know that there are cells.
|
|
|
|
// Write the row XML
|
|
|
|
Result := Result + Format(
|
|
|
|
' <table:table-row table:style-name="%s">', [styleName]) + LineEnding;
|
|
|
|
|
|
|
|
// Loop along the row and find the cells.
|
|
|
|
c := 0;
|
|
|
|
while c <= lastCol do begin
|
|
|
|
// Get the cell from the sheet
|
|
|
|
cell := ASheet.FindCell(r, c);
|
|
|
|
// Empty cell? Need to count how many to add "table:number-columns-repeated"
|
|
|
|
colsRepeated := 1;
|
|
|
|
if cell = nil then begin
|
|
|
|
cc := c + 1;
|
|
|
|
while (cc <= lastCol) do begin
|
|
|
|
cell := ASheet.FindCell(r, cc);
|
|
|
|
if cell <> nil then
|
|
|
|
break;
|
|
|
|
inc(cc)
|
|
|
|
end;
|
|
|
|
colsRepeated := cc - c;
|
|
|
|
colsRepeatedStr := IfThen(colsRepeated = 1, '',
|
|
|
|
Format('table:number-columns-repeated="%d"', [colsRepeated]));
|
|
|
|
Result := Result + Format(
|
|
|
|
' <table:table-cell %s/>', [colsRepeatedStr]) + LineEnding;
|
|
|
|
end
|
|
|
|
else begin
|
|
|
|
WriteCellCallback(cell, nil);
|
|
|
|
Result := Result + FCellContent;
|
|
|
|
end;
|
|
|
|
inc(c, colsRepeated);
|
|
|
|
end;
|
|
|
|
|
|
|
|
Result := Result +
|
|
|
|
' </table:table-row>' + LineEnding;
|
|
|
|
|
|
|
|
// Next row
|
|
|
|
inc(r, rowsRepeated);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TsSpreadOpenDocWriter.WriteRowStylesXMLAsString: string;
|
|
|
|
const
|
|
|
|
FALSE_TRUE: array[boolean] of string = ('false', 'true');
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
s: String;
|
|
|
|
rowstyle: TRowStyleData;
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
|
|
|
|
for i := 0 to FRowStyleList.Count-1 do begin
|
|
|
|
rowStyle := TRowStyleData(FRowStyleList[i]);
|
|
|
|
|
|
|
|
// Start and Name
|
|
|
|
Result := Result +
|
|
|
|
' <style:style style:name="%s" style:family="table-row">' + LineEnding;
|
|
|
|
|
|
|
|
// Column width
|
|
|
|
Result := Result +
|
|
|
|
' <style:table-row-properties style:row-height="%.3gmm" style:use-optimal-row-height="%s" fo:break-before="auto"/>' + LineEnding;
|
2014-05-28 20:52:36 +00:00
|
|
|
|
2011-05-27 13:45:30 +00:00
|
|
|
// End
|
|
|
|
Result := Result +
|
|
|
|
' </style:style>' + LineEnding;
|
2014-05-31 21:04:53 +00:00
|
|
|
|
|
|
|
Result := Format(Result,
|
|
|
|
[rowStyle.Name, rowStyle.RowHeight, FALSE_TRUE[rowStyle.AutoRowHeight]],
|
|
|
|
FPointSeparatorSettings
|
|
|
|
);
|
2011-05-27 13:45:30 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
|
2014-04-23 22:29:32 +00:00
|
|
|
constructor TsSpreadOpenDocWriter.Create(AWorkbook: TsWorkbook);
|
2012-04-27 08:01:15 +00:00
|
|
|
begin
|
2014-04-23 22:29:32 +00:00
|
|
|
inherited Create(AWorkbook);
|
2012-04-27 08:01:15 +00:00
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
FColumnStyleList := TFPList.Create;
|
|
|
|
FRowStyleList := TFPList.Create;
|
|
|
|
|
2012-04-27 08:01:15 +00:00
|
|
|
FPointSeparatorSettings := SysUtils.DefaultFormatSettings;
|
|
|
|
FPointSeparatorSettings.DecimalSeparator:='.';
|
|
|
|
end;
|
|
|
|
|
2014-05-31 21:04:53 +00:00
|
|
|
destructor TsSpreadOpenDocWriter.Destroy;
|
|
|
|
var
|
|
|
|
j: Integer;
|
|
|
|
begin
|
|
|
|
for j:=FColumnStyleList.Count-1 downto 0 do TObject(FColumnStyleList[j]).Free;
|
|
|
|
FColumnStyleList.Free;
|
|
|
|
|
|
|
|
for j:=FRowStyleList.Count-1 downto 0 do TObject(FRowStyleList[j]).Free;
|
|
|
|
FRowStyleList.Free;
|
|
|
|
end;
|
|
|
|
|
2009-02-02 09:58:51 +00:00
|
|
|
{
|
|
|
|
Writes a string to a file. Helper convenience method.
|
|
|
|
}
|
|
|
|
procedure TsSpreadOpenDocWriter.WriteStringToFile(AString, AFileName: string);
|
2009-01-28 22:36:41 +00:00
|
|
|
var
|
|
|
|
TheStream : TFileStream;
|
|
|
|
S : String;
|
|
|
|
begin
|
|
|
|
TheStream := TFileStream.Create(AFileName, fmCreate);
|
|
|
|
S:=AString;
|
|
|
|
TheStream.WriteBuffer(Pointer(S)^,Length(S));
|
|
|
|
TheStream.Free;
|
|
|
|
end;
|
|
|
|
|
2009-02-02 09:58:51 +00:00
|
|
|
{
|
|
|
|
Writes an OOXML document to the disc.
|
|
|
|
}
|
2009-11-08 19:21:23 +00:00
|
|
|
procedure TsSpreadOpenDocWriter.WriteToFile(const AFileName: string;
|
2014-04-23 22:29:32 +00:00
|
|
|
const AOverwriteExisting: Boolean);
|
2008-02-24 13:18:34 +00:00
|
|
|
var
|
2009-02-02 09:58:51 +00:00
|
|
|
FZip: TZipper;
|
2008-02-24 13:18:34 +00:00
|
|
|
begin
|
2009-02-02 09:58:51 +00:00
|
|
|
{ Fill the strings with the contents of the files }
|
2009-01-28 22:36:41 +00:00
|
|
|
|
2010-12-08 10:24:15 +00:00
|
|
|
WriteMimetype();
|
|
|
|
WriteMetaInfManifest();
|
|
|
|
WriteMeta();
|
|
|
|
WriteSettings();
|
|
|
|
WriteStyles();
|
2014-04-23 22:29:32 +00:00
|
|
|
WriteContent;
|
2008-02-24 13:18:34 +00:00
|
|
|
|
2009-02-02 09:58:51 +00:00
|
|
|
{ Write the data to streams }
|
|
|
|
|
|
|
|
FSMeta := TStringStream.Create(FMeta);
|
|
|
|
FSSettings := TStringStream.Create(FSettings);
|
|
|
|
FSStyles := TStringStream.Create(FStyles);
|
|
|
|
FSContent := TStringStream.Create(FContent);
|
|
|
|
FSMimetype := TStringStream.Create(FMimetype);
|
|
|
|
FSMetaInfManifest := TStringStream.Create(FMetaInfManifest);
|
|
|
|
|
|
|
|
{ Now compress the files }
|
|
|
|
|
|
|
|
FZip := TZipper.Create;
|
|
|
|
try
|
|
|
|
FZip.FileName := AFileName;
|
|
|
|
|
|
|
|
FZip.Entries.AddFileEntry(FSMeta, OPENDOC_PATH_META);
|
|
|
|
FZip.Entries.AddFileEntry(FSSettings, OPENDOC_PATH_SETTINGS);
|
|
|
|
FZip.Entries.AddFileEntry(FSStyles, OPENDOC_PATH_STYLES);
|
|
|
|
FZip.Entries.AddFileEntry(FSContent, OPENDOC_PATH_CONTENT);
|
|
|
|
FZip.Entries.AddFileEntry(FSMimetype, OPENDOC_PATH_MIMETYPE);
|
|
|
|
FZip.Entries.AddFileEntry(FSMetaInfManifest, OPENDOC_PATH_METAINF_MANIFEST);
|
|
|
|
|
|
|
|
FZip.ZipAllFiles;
|
|
|
|
finally
|
|
|
|
FZip.Free;
|
|
|
|
FSMeta.Free;
|
|
|
|
FSSettings.Free;
|
|
|
|
FSStyles.Free;
|
|
|
|
FSContent.Free;
|
|
|
|
FSMimetype.Free;
|
|
|
|
FSMetaInfManifest.Free;
|
|
|
|
end;
|
2008-02-24 13:18:34 +00:00
|
|
|
end;
|
|
|
|
|
2009-02-02 09:58:51 +00:00
|
|
|
|
2014-04-23 22:29:32 +00:00
|
|
|
procedure TsSpreadOpenDocWriter.WriteToStream(AStream: TStream);
|
2009-01-28 22:36:41 +00:00
|
|
|
begin
|
2009-02-02 09:58:51 +00:00
|
|
|
// Not supported at the moment
|
|
|
|
raise Exception.Create('TsSpreadOpenDocWriter.WriteToStream not supported');
|
2009-01-28 22:36:41 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TsSpreadOpenDocWriter.WriteFormula(AStream: TStream; const ARow,
|
2013-12-07 13:42:22 +00:00
|
|
|
ACol: Cardinal; const AFormula: TsFormula; ACell: PCell);
|
2008-02-24 13:18:34 +00:00
|
|
|
begin
|
2009-02-02 09:58:51 +00:00
|
|
|
{ // The row should already be the correct one
|
|
|
|
FContent := FContent +
|
|
|
|
' <table:table-cell office:value-type="string">' + LineEnding +
|
|
|
|
' <text:p>' + AFormula.DoubleValue + '</text:p>' + LineEnding +
|
|
|
|
' </table:table-cell>' + LineEnding;
|
|
|
|
<table:table-cell table:formula="of:=[.A1]+[.B2]" office:value-type="float" office:value="1833">
|
|
|
|
<text:p>1833</text:p>
|
|
|
|
</table:table-cell>}
|
2009-01-28 22:36:41 +00:00
|
|
|
end;
|
2008-02-24 13:18:34 +00:00
|
|
|
|
2014-04-21 21:43:43 +00:00
|
|
|
{
|
|
|
|
Writes an empty cell
|
|
|
|
|
|
|
|
Not clear whether this is needed for ods, but the inherited procedure is abstract.
|
|
|
|
}
|
|
|
|
procedure TsSpreadOpenDocWriter.WriteBlank(AStream: TStream;
|
|
|
|
const ARow, ACol: Cardinal; ACell: PCell);
|
2014-05-27 22:12:48 +00:00
|
|
|
var
|
|
|
|
lStyle: String = '';
|
|
|
|
lIndex: Integer;
|
2014-04-21 21:43:43 +00:00
|
|
|
begin
|
2014-05-27 22:12:48 +00:00
|
|
|
// Write empty cell only if it has formatting
|
|
|
|
if ACell^.UsedFormattingFields <> [] then begin
|
|
|
|
lIndex := FindFormattingInList(ACell);
|
|
|
|
lStyle := ' table:style-name="ce' + IntToStr(lIndex) + '" ';
|
2014-05-31 21:04:53 +00:00
|
|
|
FCellContent :=
|
2014-05-27 22:12:48 +00:00
|
|
|
' <table:table-cell ' + lStyle + '>' + LineEnding +
|
|
|
|
' </table:table-cell>' + LineEnding;
|
2014-05-31 21:04:53 +00:00
|
|
|
end else
|
|
|
|
FCellContent := '';
|
2014-04-21 21:43:43 +00:00
|
|
|
end;
|
|
|
|
|
2014-05-28 19:26:08 +00:00
|
|
|
{ Creates an XML string for inclusion of the background color into the
|
|
|
|
written file from the backgroundcolor setting in the format cell.
|
|
|
|
Is called from WriteStyles (via WriteStylesXMLAsString). }
|
|
|
|
function TsSpreadOpenDocWriter.WriteBackgroundColorStyleXMLAsString(
|
|
|
|
const AFormat: TCell): String;
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
|
|
|
|
if not (uffBackgroundColor in AFormat.UsedFormattingFields) then
|
|
|
|
exit;
|
|
|
|
|
|
|
|
Result := Format('fo:background-color="%s" ', [
|
|
|
|
Workbook.GetPaletteColorAsHTMLStr(AFormat.BackgroundColor)
|
|
|
|
]);
|
|
|
|
// + Workbook.FPSColorToHexString(FFormattingStyles[i].BackgroundColor, FFormattingStyles[i].RGBBackgroundColor) +'" ';
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ Creates an XML string for inclusion of borders and border styles into the
|
|
|
|
written file from the border settings in the format cell.
|
|
|
|
Is called from WriteStyles (via WriteStylesXMLAsString). }
|
|
|
|
function TsSpreadOpenDocWriter.WriteBorderStyleXMLAsString(const AFormat: TCell): String;
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
|
|
|
|
if not (uffBorder in AFormat.UsedFormattingFields) then
|
|
|
|
exit;
|
|
|
|
|
|
|
|
if cbSouth in AFormat.Border then begin
|
|
|
|
Result := Result + Format('fo:border-bottom="%s %s %s" ', [
|
|
|
|
BORDER_LINEWIDTHS[AFormat.BorderStyles[cbSouth].LineStyle],
|
|
|
|
BORDER_LINESTYLES[AFormat.BorderStyles[cbSouth].LineStyle],
|
|
|
|
Workbook.GetPaletteColorAsHTMLStr(AFormat.BorderStyles[cbSouth].Color)
|
|
|
|
]);
|
|
|
|
if AFormat.BorderStyles[cbSouth].LineStyle = lsDouble then
|
|
|
|
Result := Result + 'style:border-linewidth-bottom="0.002cm 0.035cm 0.002cm" ';
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Result := Result + 'fo:border-bottom="none" ';
|
|
|
|
|
|
|
|
if cbWest in AFormat.Border then begin
|
|
|
|
Result := Result + Format('fo:border-left="%s %s %s" ', [
|
|
|
|
BORDER_LINEWIDTHS[AFormat.BorderStyles[cbWest].LineStyle],
|
|
|
|
BORDER_LINESTYLES[AFormat.BorderStyles[cbWest].LineStyle],
|
|
|
|
Workbook.GetPaletteColorAsHTMLStr(AFormat.BorderStyles[cbWest].Color)
|
|
|
|
]);
|
|
|
|
if AFormat.BorderStyles[cbWest].LineStyle = lsDouble then
|
|
|
|
Result := Result + 'style:border-linewidth-left="0.002cm 0.035cm 0.002cm" ';
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Result := Result + 'fo:border-left="none" ';
|
|
|
|
|
|
|
|
if cbEast in AFormat.Border then begin
|
|
|
|
Result := Result + Format('fo:border-right="%s %s %s" ', [
|
|
|
|
BORDER_LINEWIDTHS[AFormat.BorderStyles[cbEast].LineStyle],
|
|
|
|
BORDER_LINESTYLES[AFormat.BorderStyles[cbEast].LineStyle],
|
|
|
|
Workbook.GetPaletteColorAsHTMLStr(AFormat.BorderStyles[cbEast].Color)
|
|
|
|
]);
|
|
|
|
if AFormat.BorderStyles[cbSouth].LineStyle = lsDouble then
|
|
|
|
Result := Result + 'style:border-linewidth-right="0.002cm 0.035cm 0.002cm" ';
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Result := Result + 'fo:border-right="none" ';
|
|
|
|
|
|
|
|
if cbNorth in AFormat.Border then begin
|
|
|
|
Result := Result + Format('fo:border-top="%s %s %s" ', [
|
|
|
|
BORDER_LINEWIDTHS[AFormat.BorderStyles[cbNorth].LineStyle],
|
|
|
|
BORDER_LINESTYLES[AFormat.BorderStyles[cbNorth].LineStyle],
|
|
|
|
Workbook.GetPaletteColorAsHTMLStr(AFormat.BorderStyles[cbNorth].Color)
|
|
|
|
]);
|
|
|
|
if AFormat.BorderStyles[cbSouth].LineStyle = lsDouble then
|
|
|
|
Result := Result + 'style:border-linewidth-top="0.002cm 0.035cm 0.002cm" ';
|
|
|
|
end else
|
|
|
|
Result := Result + 'fo:border-top="none" ';
|
|
|
|
end;
|
|
|
|
|
2014-05-28 20:52:36 +00:00
|
|
|
{ Creates an XML string for inclusion of the horizontal alignment into the
|
|
|
|
written file from the horizontal alignment setting in the format cell.
|
|
|
|
Is called from WriteStyles (via WriteStylesXMLAsString). }
|
|
|
|
function TsSpreadOpenDocWriter.WriteHorAlignmentStyleXMLAsString(
|
|
|
|
const AFormat: TCell): String;
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
if not (uffHorAlign in AFormat.UsedFormattingFields) then
|
|
|
|
exit;
|
|
|
|
case AFormat.HorAlignment of
|
|
|
|
haLeft : Result := 'fo:text-align="start" ';
|
|
|
|
haCenter : Result := 'fo:text-align="center" ';
|
|
|
|
haRight : Result := 'fo:text-align="end" ';
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-05-28 19:26:08 +00:00
|
|
|
{ Creates an XML string for inclusion of the textrotation style option into the
|
|
|
|
written file from the textrotation setting in the format cell.
|
|
|
|
Is called from WriteStyles (via WriteStylesXMLAsString). }
|
|
|
|
function TsSpreadOpenDocWriter.WriteTextRotationStyleXMLAsString(
|
|
|
|
const AFormat: TCell): String;
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
if not (uffTextRotation in AFormat.UsedFormattingFields) then
|
|
|
|
exit;
|
|
|
|
|
|
|
|
case AFormat.TextRotation of
|
|
|
|
rt90DegreeClockwiseRotation : Result := 'style:rotation-angle="270" ';
|
|
|
|
rt90DegreeCounterClockwiseRotation : Result := 'style:rotation-angle="90" ';
|
|
|
|
rtStacked : Result := 'style:direction="ttb" ';
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-05-28 20:52:36 +00:00
|
|
|
{ Creates an XML string for inclusion of the vertical alignment into the
|
|
|
|
written file from the vertical alignment setting in the format cell.
|
|
|
|
Is called from WriteStyles (via WriteStylesXMLAsString). }
|
|
|
|
function TsSpreadOpenDocWriter.WriteVertAlignmentStyleXMLAsString(
|
|
|
|
const AFormat: TCell): String;
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
if not (uffVertAlign in AFormat.UsedFormattingFields) then
|
|
|
|
exit;
|
|
|
|
case AFormat.VertAlignment of
|
|
|
|
vaTop : Result := 'style:vertical-align="top" ';
|
|
|
|
vaCenter : Result := 'style:vertical-align="middle" ';
|
|
|
|
vaBottom : Result := 'style:vertical-align="bottom" ';
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-05-28 19:26:08 +00:00
|
|
|
{ Creates an XML string for inclusion of the wordwrap option into the
|
|
|
|
written file from the wordwrap setting in the format cell.
|
|
|
|
Is called from WriteStyles (via WriteStylesXMLAsString). }
|
2014-05-28 20:52:36 +00:00
|
|
|
function TsSpreadOpenDocWriter.WriteWordwrapStyleXMLAsString(
|
|
|
|
const AFormat: TCell): String;
|
2014-05-28 19:26:08 +00:00
|
|
|
begin
|
|
|
|
if (uffWordWrap in AFormat.UsedFormattingFields) then
|
|
|
|
Result := 'fo:wrap-option="wrap" '
|
|
|
|
else
|
|
|
|
Result := '';
|
|
|
|
end;
|
|
|
|
|
2011-05-30 07:03:56 +00:00
|
|
|
{
|
|
|
|
Writes a cell with text content
|
|
|
|
|
|
|
|
The UTF8 Text needs to be converted, because some chars are invalid in XML
|
|
|
|
See bug with patch 19422
|
|
|
|
}
|
2009-01-28 22:36:41 +00:00
|
|
|
procedure TsSpreadOpenDocWriter.WriteLabel(AStream: TStream; const ARow,
|
2013-12-07 13:42:22 +00:00
|
|
|
ACol: Cardinal; const AValue: string; ACell: PCell);
|
2010-12-08 10:24:15 +00:00
|
|
|
var
|
|
|
|
lStyle: string = '';
|
2011-05-27 13:45:30 +00:00
|
|
|
lIndex: Integer;
|
2009-01-28 22:36:41 +00:00
|
|
|
begin
|
2014-05-31 21:04:53 +00:00
|
|
|
if ACell^.UsedFormattingFields <> [] then begin
|
2011-05-27 13:45:30 +00:00
|
|
|
lIndex := FindFormattingInList(ACell);
|
|
|
|
lStyle := ' table:style-name="ce' + IntToStr(lIndex) + '" ';
|
2014-05-31 21:04:53 +00:00
|
|
|
end else
|
|
|
|
lStyle := '';
|
2010-12-08 10:24:15 +00:00
|
|
|
|
2009-01-29 13:24:37 +00:00
|
|
|
// The row should already be the correct one
|
2014-05-31 21:04:53 +00:00
|
|
|
FCellContent :=
|
2010-12-08 10:24:15 +00:00
|
|
|
' <table:table-cell office:value-type="string"' + lStyle + '>' + LineEnding +
|
2011-05-29 17:42:56 +00:00
|
|
|
' <text:p>' + UTF8TextToXMLText(AValue) + '</text:p>' + LineEnding +
|
2009-01-29 13:24:37 +00:00
|
|
|
' </table:table-cell>' + LineEnding;
|
2009-01-28 22:36:41 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TsSpreadOpenDocWriter.WriteNumber(AStream: TStream; const ARow,
|
2010-12-08 10:24:15 +00:00
|
|
|
ACol: Cardinal; const AValue: double; ACell: PCell);
|
2009-07-01 14:01:44 +00:00
|
|
|
var
|
|
|
|
StrValue: string;
|
2009-11-12 20:26:02 +00:00
|
|
|
DisplayStr: string;
|
2010-12-08 10:24:15 +00:00
|
|
|
lStyle: string = '';
|
2012-06-13 07:21:00 +00:00
|
|
|
lIndex: Integer;
|
2009-01-28 22:36:41 +00:00
|
|
|
begin
|
2014-05-31 21:04:53 +00:00
|
|
|
if ACell^.UsedFormattingFields <> [] then begin
|
2012-06-13 07:21:00 +00:00
|
|
|
lIndex := FindFormattingInList(ACell);
|
|
|
|
lStyle := ' table:style-name="ce' + IntToStr(lIndex) + '" ';
|
2014-05-31 21:04:53 +00:00
|
|
|
end else
|
|
|
|
lStyle := '';
|
2010-12-08 10:24:15 +00:00
|
|
|
|
2009-01-28 22:36:41 +00:00
|
|
|
// The row should already be the correct one
|
2009-11-12 20:26:02 +00:00
|
|
|
if IsInfinite(AValue) then begin
|
|
|
|
StrValue:='1.#INF';
|
|
|
|
DisplayStr:='1.#INF';
|
|
|
|
end else begin
|
2012-04-27 08:01:15 +00:00
|
|
|
StrValue:=FloatToStr(AValue,FPointSeparatorSettings); //Uses '.' as decimal separator
|
2009-11-14 18:48:17 +00:00
|
|
|
DisplayStr:=FloatToStr(AValue); // Uses locale decimal separator
|
2009-11-12 20:26:02 +00:00
|
|
|
end;
|
2014-05-31 21:04:53 +00:00
|
|
|
FCellContent :=
|
2010-12-08 10:24:15 +00:00
|
|
|
' <table:table-cell office:value-type="float" office:value="' + StrValue + '"' + lStyle + '>' + LineEnding +
|
2009-11-12 20:26:02 +00:00
|
|
|
' <text:p>' + DisplayStr + '</text:p>' + LineEnding +
|
2009-01-28 23:20:25 +00:00
|
|
|
' </table:table-cell>' + LineEnding;
|
2008-02-24 13:18:34 +00:00
|
|
|
end;
|
|
|
|
|
2013-12-23 12:11:20 +00:00
|
|
|
{*******************************************************************
|
|
|
|
* TsSpreadOpenDocWriter.WriteDateTime ()
|
|
|
|
*
|
2013-12-23 13:35:36 +00:00
|
|
|
* DESCRIPTION: Writes a date/time value
|
2013-12-23 12:11:20 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
*******************************************************************}
|
2013-12-22 14:02:04 +00:00
|
|
|
procedure TsSpreadOpenDocWriter.WriteDateTime(AStream: TStream;
|
|
|
|
const ARow, ACol: Cardinal; const AValue: TDateTime; ACell: PCell);
|
2013-12-23 13:35:36 +00:00
|
|
|
var
|
|
|
|
lStyle: string = '';
|
|
|
|
lIndex: Integer;
|
2013-12-22 14:02:04 +00:00
|
|
|
begin
|
2014-05-31 21:04:53 +00:00
|
|
|
if ACell^.UsedFormattingFields <> [] then begin
|
2013-12-23 13:35:36 +00:00
|
|
|
lIndex := FindFormattingInList(ACell);
|
|
|
|
lStyle := ' table:style-name="ce' + IntToStr(lIndex) + '" ';
|
2014-05-31 21:04:53 +00:00
|
|
|
end else
|
|
|
|
lStyle := '';
|
2013-12-23 13:35:36 +00:00
|
|
|
|
|
|
|
// The row should already be the correct one
|
2014-05-31 21:04:53 +00:00
|
|
|
FCellContent :=
|
2013-12-23 13:35:36 +00:00
|
|
|
' <table:table-cell office:value-type="date" office:date-value="' + FormatDateTime(ISO8601FormatExtended, AValue) + '"' + lStyle + '>' + LineEnding +
|
|
|
|
' </table:table-cell>' + LineEnding;
|
2013-12-22 14:02:04 +00:00
|
|
|
end;
|
|
|
|
|
2009-02-02 09:58:51 +00:00
|
|
|
{
|
|
|
|
Registers this reader / writer on fpSpreadsheet
|
|
|
|
}
|
2008-02-24 13:18:34 +00:00
|
|
|
initialization
|
|
|
|
|
2009-07-01 11:26:56 +00:00
|
|
|
RegisterSpreadFormat(TsSpreadOpenDocReader, TsSpreadOpenDocWriter, sfOpenDocument);
|
2008-02-24 13:18:34 +00:00
|
|
|
|
|
|
|
end.
|
|
|
|
|