forked from Konctantin/Excel4Delphi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExcel4Delphi.ExportHTML.pas
253 lines (225 loc) · 9.07 KB
/
Excel4Delphi.ExportHTML.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
unit Excel4Delphi.ExportHTML;
interface
uses
{$IFDEF MSWINDOWS}
Winapi.Windows,
{$ENDIF}
System.SysUtils,
System.UITypes,
System.Types,
System.Classes,
System.Math,
System.AnsiStrings,
{$IFDEF FMX}
FMX.Graphics,
{$ELSE}
Vcl.Graphics,
{$ENDIF}
Excel4Delphi,
Excel4Delphi.Xml;
type
TZExportHTML = class(TZExport)
public
procedure ExportTo(AStream: TStream; ASheets: TArray<integer>; options: TExportOptions); override;
end;
implementation
{ TZExportHTML }
procedure TZExportHTML.ExportTo(AStream: TStream; ASheets: TArray<integer>; options: TExportOptions);
var xml: TZsspXMLWriterH;
i, j, t, l, r: integer;
NumTopLeft: integer;
s, value, numformat: string;
Att: TZAttributesH;
max_width: Real;
strArray: TArray<string>;
sheet: TZSheet;
function HTMLStyleTable(name: string; const Style: TZStyle): string;
var s: string; i, l: integer;
begin
result := #13#10 + ' .' + name + '{'#13#10;
for i := 0 to 3 do begin
s := 'border-';
l := 0;
case i of
0: s := s + 'left:';
1: s := s + 'top:';
2: s := s + 'right:';
3: s := s + 'bottom:';
end;
s := s + '#' + ColorToHTMLHex(Style.Border[TZBordersPos(i)].Color);
if Style.Border[TZBordersPos(i)].Weight <> 0 then
s := s + ' ' + IntToStr(Style.Border[TZBordersPos(i)].Weight) + 'px'
else
inc(l);
case Style.Border[TZBordersPos(i)].LineStyle of
ZEContinuous: s := s + ' ' + 'solid';
ZEHair: s := s + ' ' + 'solid';
ZEDot: s := s + ' ' + 'dotted';
ZEDashDotDot: s := s + ' ' + 'dotted';
ZEDash: s := s + ' ' + 'dashed';
ZEDashDot: s := s + ' ' + 'dashed';
ZESlantDashDot: s := s + ' ' + 'dashed';
ZEDouble: s := s + ' ' + 'double';
else
inc(l);
end;
s := s + ';';
if l <> 2 then
result := result + s + #13#10;
end;
result := result + 'background:#' + ColorToHTMLHex(Style.BGColor) + ';}';
end;
function HTMLStyleFont(name: string; const Style: TZStyle): string;
begin
result := #13#10 + ' .' + name + '{'#13#10;
result := result + 'color:#' + ColorToHTMLHex(Style.Font.Color) + ';';
result := result + 'font-size:' + FloatToStr(Style.Font.Size, TFormatSettings.Invariant) + 'px;';
result := result + 'font-family:' + Style.Font.Name + ';}';
end;
begin
if Length(ASheets) = 0 then begin
SetLength(ASheets, WorkBook.Sheets.Count);
for I := 0 to WorkBook.Sheets.Count-1 do
ASheets[i] := i;
end;
xml := TZsspXMLWriterH.Create(AStream);
try
xml.TabLength := 1;
// start
xml.Attributes.Clear();
xml.WriteRaw('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">', true, false);
xml.WriteTagNode('HTML', true, true, false);
xml.WriteTagNode('HEAD', true, true, false);
xml.WriteTag('TITLE', WorkBook.Sheets[ASheets[0]].Title, true, false, false);
//styles
s := 'body {';
s := s + 'background:#' + ColorToHTMLHex(WorkBook.Styles.DefaultStyle.BGColor) + ';';
s := s + 'color:#' + ColorToHTMLHex(WorkBook.Styles.DefaultStyle.Font.Color) + ';';
s := s + 'font-size:' + FloatToStr(WorkBook.Styles.DefaultStyle.Font.Size, TFormatSettings.Invariant) + 'px;';
s := s + 'font-family:' + WorkBook.Styles.DefaultStyle.Font.Name + ';}';
s := s + HTMLStyleTable('T19', WorkBook.Styles.DefaultStyle);
s := s + HTMLStyleFont('F19', WorkBook.Styles.DefaultStyle);
for i := 0 to WorkBook.Styles.Count - 1 do begin
s := s + HTMLStyleTable('T' + IntToStr(i + 20), WorkBook.Styles[i]);
s := s + HTMLStyleFont('F' + IntToStr(i + 20), WorkBook.Styles[i]);
end;
xml.WriteTag('STYLE', s, true, true, false);
xml.Attributes.Add('HTTP-EQUIV', 'CONTENT-TYPE');
xml.Attributes.Add('CONTENT', 'TEXT/HTML; CHARSET=UTF-8');
xml.WriteTag('META', '', true, false, false);
xml.WriteEndTagNode(); // HEAD
//BODY
xml.Attributes.Clear();
xml.WriteTagNode('BODY', true, true, false);
//Table
for var si in ASheets do begin
sheet := WorkBook.Sheets[si];
max_width := 0.0;
for i := 0 to sheet.ColCount - 1 do
max_width := max_width + sheet.ColWidths[i];
xml.Attributes.Clear();
xml.Attributes.Add('cellSpacing', '0');
xml.Attributes.Add('border', '0');
xml.Attributes.Add('width', FloatToStr(max_width).Replace(',', '.'));
xml.WriteTagNode('TABLE', true, true, false);
Att := TZAttributesH.Create();
Att.Clear();
for i := 0 to sheet.RowCount - 1 do begin
xml.Attributes.Clear();
xml.Attributes.Add('height', floattostr(sheet.RowHeights[i]).Replace(',', '.'));
xml.WriteTagNode('TR', true, true, true);
xml.Attributes.Clear();
for j := 0 to sheet.ColCount - 1 do begin
var cell := sheet.Cell[j ,i];
// если ячейка входит в объединённые области и не является
// верхней левой ячейкой в этой области - пропускаем её
if not cell.IsMerged() or cell.IsLeftTopMerged() then begin
xml.Attributes.Clear();
NumTopLeft := sheet.MergeCells.InLeftTopCorner(j, i);
if NumTopLeft >= 0 then begin
t := sheet.MergeCells.Items[NumTopLeft].Right - sheet.MergeCells.Items[NumTopLeft].Left;
if t > 0 then
xml.Attributes.Add('colspan', InttOstr(t + 1));
t := sheet.MergeCells.Items[NumTopLeft].Bottom - sheet.MergeCells.Items[NumTopLeft].Top;
if t > 0 then
xml.Attributes.Add('rowspan', InttOstr(t + 1));
end;
t := cell.CellStyle;
if sheet.WorkBook.Styles[t].Alignment.Horizontal = ZHCenter then
xml.Attributes.Add('align', 'center')
else if sheet.WorkBook.Styles[t].Alignment.Horizontal = ZHRight then
xml.Attributes.Add('align', 'right')
else if sheet.WorkBook.Styles[t].Alignment.Horizontal = ZHJustify then
xml.Attributes.Add('align', 'justify');
numformat := sheet.WorkBook.Styles[t].NumberFormat;
xml.Attributes.Add('class', 'T' + IntToStr(t + 20));
xml.Attributes.Add('width', inttostr(sheet.Columns[j].WidthPix) + 'px');
xml.WriteTagNode('TD', true, false, false);
xml.Attributes.Clear();
Att.Clear();
Att.Add('class', 'F' + IntToStr(t + 20));
if fsbold in sheet.WorkBook.Styles[t].Font.Style then
xml.WriteTagNode('B', false, false, false);
if fsItalic in sheet.WorkBook.Styles[t].Font.Style then
xml.WriteTagNode('I', false, false, false);
if fsUnderline in sheet.WorkBook.Styles[t].Font.Style then
xml.WriteTagNode('U', false, false, false);
if fsStrikeOut in sheet.WorkBook.Styles[t].Font.Style then
xml.WriteTagNode('S', false, false, false);
l := Length(cell.Href);
if l > 0 then begin
xml.Attributes.Add('href', cell.Href);
//target?
xml.WriteTagNode('A', false, false, false);
xml.Attributes.Clear();
end;
value := cell.Data;
//value := value.Replace(#13#10, '<br>');
case cell.CellType of
TZCellType.ZENumber:
begin
r := numformat.IndexOf('.');
if r > -1 then begin
value := FloatToStrF(cell.AsDouble, ffNumber, 12, Min(4, Max(0, numformat.Substring(r).Length - 1)));
end
else begin
value := FloatToStr(cell.AsDouble);
end;
end;
TZCellType.ZEDateTime:
begin
// todo: make datetimeformat from cell NumberFormat
value := FormatDateTime('dd.mm.yyyy', cell.AsDateTime);
end;
end;
strArray := value.Split([#13, #10], TStringSplitOptions.ExcludeEmpty);
for r := 0 to Length(strArray) - 1 do begin
if r > 0 then
xml.WriteTag('BR', '');
xml.WriteTag('FONT', strArray[r], Att, false, false, true);
end;
if l > 0 then
xml.WriteEndTagNode(); // A
if fsbold in sheet.WorkBook.Styles[t].Font.Style then
xml.WriteEndTagNode(); // B
if fsItalic in sheet.WorkBook.Styles[t].Font.Style then
xml.WriteEndTagNode(); // I
if fsUnderline in sheet.WorkBook.Styles[t].Font.Style then
xml.WriteEndTagNode(); // U
if fsStrikeOut in sheet.WorkBook.Styles[t].Font.Style then
xml.WriteEndTagNode(); // S
xml.WriteEndTagNode(); // TD
end;
end;
xml.WriteEndTagNode(); // TR
end;
end;
xml.WriteEndTagNode(); // BODY
xml.WriteEndTagNode(); // HTML
xml.EndSaveTo();
FreeAndNil(Att);
finally
xml.Free();
end;
end;
end.