This commit is contained in:
csh 2023-04-18 15:34:45 +08:00
parent df79a459dd
commit 77762ce275
9 changed files with 244 additions and 243 deletions

View File

@ -6,7 +6,7 @@ sheetName2 := "中文";
// OpenFile // OpenFile
excel := new TSExcelFile(); excel := new TSExcelFile();
[err, errmsg] := excel.OpenFile('', 'd:\\temp\\raw.xlsx'); [err, errmsg] := excel.OpenFile('', 'd:\\temp\\b.xlsx');
PrintInfo("OpenFile", err, errmsg); PrintInfo("OpenFile", err, errmsg);
// Save // Save
@ -14,14 +14,13 @@ PrintInfo("OpenFile", err, errmsg);
PrintInfo("Save", err, errmsg); PrintInfo("Save", err, errmsg);
excel := new TSExcelFile(); excel := new TSExcelFile();
// NewFile // NewFile
[err, errmsg] := excel.NewFile(); [err, errmsg] := excel.NewFile();
PrintInfo("NewFile", err, errmsg); PrintInfo("NewFile", err, errmsg);
// SaveAs // SaveAs
// [err, errmsg] := excel.SaveAs('', 'd:\\temp\\test.xlsx'); [err, errmsg] := excel.SaveAs('', 'd:\\temp\\test.xlsx');
// PrintInfo("SaveAs", err, errmsg); PrintInfo("SaveAs", err, errmsg);
// FileName // FileName
name := excel.FileName(); name := excel.FileName();
@ -43,6 +42,14 @@ println("[success] GetSheetName = {}", Name);
excel.NewSheet(sheetName2); excel.NewSheet(sheetName2);
println("[success] NewSheet"); println("[success] NewSheet");
// NewSheet
excel.NewSheet(sheetName, 'sheet_copy');
println("[success] NewSheet2");
// InsertSheet
excel.InsertSheet(sheetName, sheetName + 'before');
println("[success] NewSheet2");
// GetSheets // GetSheets
sheets := excel.GetSheets(); sheets := excel.GetSheets();
println("[success] sheets = {}", sheets); println("[success] sheets = {}", sheets);
@ -93,7 +100,7 @@ excel.ClearCell(sheetName, "A1");
println("[success] ClearCell"); println("[success] ClearCell");
// SetCellFormula // SetCellFormula
excel.SetCellFormula(sheetName, "A2", "=100*50"); excel.SetCellFormula(sheetName, "A2", "100*50");
println("[success] SetCellFormula"); println("[success] SetCellFormula");
// GetCellFormula // GetCellFormula
@ -134,13 +141,13 @@ println("[success] RemoveRow");
// SetRowHeight // SetRowHeight
excel.SetRowHeight(sheetName, 1, 30); excel.SetRowHeight(sheetName, 1, 30);
excel.SetRowHeight(sheetName, 15, 30); excel.SetRowHeight(sheetName, 5, 30);
println("[success] SetRowHeight"); println("[success] SetRowHeight");
// GetRowHeight // GetRowHeight
height := excel.GetRowHeight(sheetName, 2); height := excel.GetRowHeight(sheetName, 2);
println("[success] GetRowHeight = {}", height); println("[success] GetRowHeight = {}", height);
height := excel.GetRowHeight(sheetName, 15); height := excel.GetRowHeight(sheetName, 5);
println("[success] GetRowHeight = {}", height); println("[success] GetRowHeight = {}", height);
// SetColWidth // SetColWidth
@ -165,6 +172,9 @@ println("[success] GetSheetDefaultColWidth = {}", width);
excel.AddComment(sheetName, "A1", "csh", "heiheihei"); excel.AddComment(sheetName, "A1", "csh", "heiheihei");
println("[success] AddComment"); println("[success] AddComment");
[author, comment] := excel.GetComment(sheetName, 'A1');
println("[success] GetComment author = {}, comment = {}", author, comment);
// AddChart // AddChart
data := array(("Name":"Small","Apple":2,"Orange":3,"Pear":3), data := array(("Name":"Small","Apple":2,"Orange":3,"Pear":3),
("Name":"Normal","Apple":5,"Orange":2,"Pear":4), ("Name":"Normal","Apple":5,"Orange":2,"Pear":4),
@ -219,6 +229,12 @@ PrintInfo("ColumnNumberToName", err, errmsg);
[err, col, row] := excel.CellNameToCoordinates("A2"); [err, col, row] := excel.CellNameToCoordinates("A2");
if not err then println("[success] {} = col : {}, row = {}", "CellNameToCoordinates", col, row); if not err then println("[success] {} = col : {}, row = {}", "CellNameToCoordinates", col, row);
// CoordinatesToCellName
[err, cell] := excel.CoordinatesToCellName(1, 2, true);
println("[success] CoordinatesToCellName, cell = {}", cell);
[err, cell] := excel.CoordinatesToCellName(1, 2);
println("[success] CoordinatesToCellName, cell = {}", cell);
// RGBToHSL // RGBToHSL
[h, s, l] := excel.RGBToHSL(255, 15, 33); [h, s, l] := excel.RGBToHSL(255, 15, 33);
println("[success] {} = {}, {}, {}", "RGBToHSL", h, s, l); println("[success] {} = {}, {}, {}", "RGBToHSL", h, s, l);
@ -259,9 +275,11 @@ style.NumberFormat.FormatCode := "#,##0.000_ ";
//style.Fill.Pattern.PatternType := "solid"; //style.Fill.Pattern.PatternType := "solid";
//style.Fill.Pattern.ForegroundColor := "FF6699FF"; //style.Fill.Pattern.ForegroundColor := "FF6699FF";
//style.Fill.Pattern.BackgroundColor := "FFFF0000"; //style.Fill.Pattern.BackgroundColor := "FFFF0000";
style.Fill.Gradient.ThemeColor1 := 0; style.Fill.Gradient.Degree := 270;
style.Fill.Gradient.ThemeColor2 := 4; style.Fill.Gradient.Stop1.Position := 0;
style.Fill.Gradient.Shading := 1; style.Fill.Gradient.Stop1.ThemeColor := 0;
style.Fill.Gradient.Stop2.Position := 1;
style.Fill.Gradient.Stop2.ThemeColor := 4;
style.Protection.Hide := 1; style.Protection.Hide := 1;
style.Protection.Lock := 1; style.Protection.Lock := 1;
@ -274,8 +292,23 @@ println("[success] SetCellStyle");
styleid := excel.GetCellStyle(sheetName, "A1"); styleid := excel.GetCellStyle(sheetName, "A1");
println("[success] GetCellStyle = {}", styleid); println("[success] GetCellStyle = {}", styleid);
styleid := excel.GetCellStyle(sheetName, "D4");
println("[success] GetCellStyle = {}", styleid); style := excel.GetStyle(styleid);
println("font.Name = {}", style.Font.Name);
println("font.Italic = {}", style.Font.Italic);
println("border.left.Color = {}", style.border.left.Color);
println("border.Top.LineStyle = {}", style.border.Top.LineStyle);
println("border.DiagonalDown = {}", style.border.DiagonalDown);
println("numberFormat.FormatCode = {}", style.NumberFormat.FormatCode);
println("alignment.vertical = {}", style.Alignment.Vertical);
println("alignment.TextRotation = {}", style.Alignment.TextRotation);
println("protection.Hide = {}", style.Protection.Hide);
println("protection.Lock = {}", style.Protection.Lock);
//println("fill.pattern.PatternType = {}", style.Fill.Pattern.PatternType);
//println("fill.pattern.ForegroundColor = {}", style.Fill.Pattern.ForegroundColor);
println("style.Fill.Gradient.Degree = {}", style.Fill.Gradient.Degree);
println("style.Fill.Gradient.Stop1.ThemeColor = {}", style.Fill.Gradient.Stop1.ThemeColor);
println("style.Fill.Gradient.Stop2.Position = {}", style.Fill.Gradient.Stop2.Position);
// SetSheetHeaderFooter // SetSheetHeaderFooter
headerFooter := TOfficeObj("THeaderFooter"); headerFooter := TOfficeObj("THeaderFooter");
@ -290,7 +323,6 @@ headerFooter.EvenFooter := "&L456&R789";
headerFooter.FirstHeader := "&L++"; headerFooter.FirstHeader := "&L++";
headerFooter.FirstFooter := "&R--"; headerFooter.FirstFooter := "&R--";
excel.SetSheetHeaderFooter(sheetName, headerFooter); excel.SetSheetHeaderFooter(sheetName, headerFooter);
println("[success] SetSheetHeaderFooter");
// SetSheetVisible // SetSheetVisible
excel.SetSheetVisible(sheetName, True); excel.SetSheetVisible(sheetName, True);
@ -332,8 +364,8 @@ println("[success] SetPageMargins");
// GetPageMargins // GetPageMargins
margins := excel.GetPageMargins(sheetName2); margins := excel.GetPageMargins(sheetName2);
println("[success] GetPageMargins top = {}, bottom = {}, left = {}, right = {}, header = {}, footer = {}", println("[success] GetPageMargins top = {}, bottom = {}, left = {}, right = {}, header = {}, footer = {}",
margins.Value('Top'), margins.Value('Bottom'), margins.Value('Left'), margins.Value('Right'), margins.Top, margins.Bottom, margins.Left, margins.Right,
margins.Value('Header'), margins.Value('Footer')); margins.Header, margins.Footer);
// MergeCell // MergeCell
excel.SetCellStyle(sheetName2, "A4", "A4", styleid1); excel.SetCellStyle(sheetName2, "A4", "A4", styleid1);
@ -345,6 +377,7 @@ println("[success] MergeCell");
excel.UnMergeCell(sheetName2, "A4", "C4"); excel.UnMergeCell(sheetName2, "A4", "C4");
println("[success] UnMergeCell"); println("[success] UnMergeCell");
// SetSheetViewOptions // SetSheetViewOptions
sheetView := TOfficeObj('TSheetView'); sheetView := TOfficeObj('TSheetView');
sheetView.ShowGridLines := false; sheetView.ShowGridLines := false;
@ -372,6 +405,7 @@ println("[success] SetPane");
pageLayout := TOfficeObj('TPageLayout'); pageLayout := TOfficeObj('TPageLayout');
pageLayout.FitToWidth := 10; pageLayout.FitToWidth := 10;
pageLayout.FitToHeight := 10; pageLayout.FitToHeight := 10;
pageLayout.UseFirstPageNumber := true;
pageLayout.FirstPageNumber := 5; pageLayout.FirstPageNumber := 5;
pageLayout.Orientation := "portrait"; pageLayout.Orientation := "portrait";
pageLayout.BlackAndWhite := true; pageLayout.BlackAndWhite := true;
@ -412,9 +446,10 @@ hyperlink := excel.GetCellHyperLink(sheetName2, 'A2');
println("[success] GetCellHyperLink LinkType = {}, LinkUrl = {}, Tooltip = {}", hyperlink.LinkType, hyperlink.LinkUrl, hyperlink.Tooltip); println("[success] GetCellHyperLink LinkType = {}, LinkUrl = {}, Tooltip = {}", hyperlink.LinkType, hyperlink.LinkUrl, hyperlink.Tooltip);
// SetSheetBackground // SetSheetBackground
ret := readfile(rwBinary(), "", "C:\\Users\\csh05\\Pictures\\Saved Pictures\\1.jpg", 0, 1024000, data); imagepath := "C:\\Users\\csh05\\Pictures\\1png.png";
ret := readfile(rwBinary(), "", imagepath, 0, 1024000, data);
if not ret then return echo "readfile error!"; if not ret then return echo "readfile error!";
picture := TOfficeObj('TPicture'); Image := TOfficeObj('TPicture');
picture.Image := data; picture.Image := data;
excel.SetSheetBackground(sheetName, picture); excel.SetSheetBackground(sheetName, picture);
println("[success] SetSheetBackground"); println("[success] SetSheetBackground");
@ -528,30 +563,50 @@ println("[success] SetDefaultFont");
tfont := excel.GetDefaultFont(); tfont := excel.GetDefaultFont();
println("[success] GetDefaultFont , name = {}, size = {}, themecolor = {}", tfont.Name, tfont.Size, tfont.ThemeColor); println("[success] GetDefaultFont , name = {}, size = {}, themecolor = {}", tfont.Name, tfont.Size, tfont.ThemeColor);
calcpr := TOfficeObj('TCalcPr');
calcpr.CalcMode := "";
calcpr.RefMode := "R1C1";
calcpr.Iterate := 1;
calcpr.IterateCount := 300;
calcpr.IterateDelta := "3E-3";
calcpr.CalcOnSave := true;
calcpr.ConCurrentCalc := true;
calcpr.ConCurrentManualCount := 4;
calcpr.FullPrecision := 0;
excel.SetCalcOptions(calcpr);
println("[success] SetCalcOptions");
calc := excel.GetCalcOptions();
println("[success] GetCalcOptions, CalcMode = {}, RefMode = {}, Iterate = {},
IterateCount = {}, IterateDelta = {}, CalcOnSave = {}, conCurrentCalc = {},
conCurrentManualCount = {}, FullPrecision = {}", calc.CalcMode, calc.RefMode, calc.Iterate,
calc.IterateCount, calc.IterateDelta, calc.CalcOnSave, calc.conCurrentCalc,
calc.conCurrentManualCount, calc.FullPrecision);
excel.SetRowOutlineLevel(sheetName2, 1, 1);
excel.SetRowOutlineLevel(sheetName2, 2, 1);
println("[success] SetRowOutlineLevel");
level1 := excel.GetRowOutlineLevel(sheetName2, 1);
level2 := excel.GetRowOutlineLevel(sheetName2, 3);
println("[success] GetRowOutlineLevel, level1 = {}, level2 = {}", level1, level2);
excel.SetColOutlineLevel(sheetName2, 'A', 1);
excel.SetColOutlineLevel(sheetName2, 'B', 2);
excel.SetColOutlineLevel(sheetName2, 'C', 1);
println("[success] SetColOutlineLevel");
level1 := excel.GetColOutlineLevel(sheetName2, 'A');
level2 := excel.GetColOutlineLevel(sheetName2, 'B');
println("[success] GetColOutlineLevel, level1 = {}, level2 = {}", level1, level2);
[err, errmsg] := excel.saveas("", "d:\\temp\\test.xlsx"); [err, errmsg] := excel.saveas("", "d:\\temp\\test.xlsx");
println("saveas : {}", err); println("saveas : {}", err);
Function _test_background(excel);
Begin
f := 'D:\\temp\\a.xlsx';
[err, msg] := excel.OpenFile('', f);
if err then return
echo "Open Fail:", msg, '\n';
ret := readfile(rwBinary(), "", "C:\\Users\\csh05\\Pictures\\Saved Pictures\\1.png", 0, 102400, data);
if ret then pictrue := new TSImage(data);
else return echo "readfile error!";
excel.SetSheetBackground('Sheet1', pictrue);
[err, errmsg] := excel.SaveAs('', 'd:\\temp\\e.xlsx');
println('SaveAs->{}',err);
End
// ============================================================ // ============================================================
Function PrintInfo(msg, err, errinfo); Function PrintInfo(msg, err, errinfo);
Begin Begin
if err then debugreturn println("{} : errorno = {}, errorinfo = {}!", msg, err, errinfo); if err then debugreturn println("{} : errorno = {}, errorinfo = {}!", msg, err, errinfo);
println("[success] {} = {}", msg, errinfo); println("[success] {} = {}", msg, errinfo);
End End

Binary file not shown.

View File

@ -1,4 +1,4 @@
// Version 1.2.9 // Version 1.3.0
Function TOfficeObj(n); Function TOfficeObj(n);
Begin Begin

View File

@ -1,4 +1,4 @@
// Version 1.2.9 // Version 1.3.0
Type TSDocxFile = Class Type TSDocxFile = Class
///Version: V1.0 2022-09-20 ///Version: V1.0 2022-09-20

View File

@ -1,4 +1,4 @@
// Version 1.2.9 // Version 1.3.0
Type TSExcelFile = Class Type TSExcelFile = Class
///Version: V1.0 2022-08-08 ///Version: V1.0 2022-08-08

View File

@ -176,6 +176,7 @@ public
if istable(r) then Begin if istable(r) then Begin
r := r[0]; r := r[0];
node := node.FirstChildElement(r['name']); node := node.FirstChildElement(r['name']);
if not ifObj(node) and r['nodeType'] = 'empty' then return false;
if not ifObj(node) then return nil; if not ifObj(node) then return nil;
if r['nodeType'] = 'pcdata' then //返回文本串 if r['nodeType'] = 'pcdata' then //返回文本串
return node.GetText(); return node.GetText();
@ -183,6 +184,7 @@ public
return node.Data(); return node.Data();
key := r['attrName'] ? r['attrName'] : r['attrEx'] ? r['attrEx'] : 'val'; key := r['attrName'] ? r['attrName'] : r['attrEx'] ? r['attrEx'] : 'val';
value := node.GetAttribute(key); value := node.GetAttribute(key);
if r['nodeType'] = 'empty' and value = '' then return true;
return trystrtofloat(value, r) ? r : value; return trystrtofloat(value, r) ? r : value;
End; End;
return nil; return nil;

View File

@ -2,220 +2,151 @@ Type TSImage = Class
Function Create(stream); overload; Function Create(stream); overload;
Begin Begin
content_ := stream; content_ := stream;
imageDef := array(('Png', 0, (0x89, 'P', 'N', 'G', 0x0d, 0x0a, 0x1a, 0x0a),'png'), width_ := 0;
('Jfif',6,'JFIF','Jfif'), height_ := 0;
('Exif',6,'Exif','Exif'), imagetypeDef := array(
('Gif',0,'GIF87a','gif'), ('name': 'bmp', 'position': 0, 'value': array(0x42, 0x4d)),
('Gif',0,'GIF89a','gif'), ('name': 'png', 'position': 0, 'value': array(0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A)),
('Tiff',0,('M','M',0x00,'*'),'tiff'), ('name': 'gif', 'position': 0, 'value': array(0x47, 0x49, 0x46, 0x38, 0x39, 0x61)),
('Tiff',0,('I', 'I', '*', 0x00),'tiff'), ('name': 'gif', 'position': 0, 'value': array(0x47, 0x49, 0x46, 0x38, 0x37, 0x61)),
('Bmp',0,'BM','bmp')); ('name': 'jpeg', 'position': 0, 'value': array(0xFF, 0xD8, 0xFF, 0xE1)),
ind := getImageDef(stream, imageDef); ('name': 'jpg', 'position': 0, 'value': array(0xFF, 0xD8)),
if ind >= 0 then Begin ('name': 'tiff', 'position': 0, 'value': array(0x49, 0x49), 'order': 'littleEndian'),
ExtFileName := imageDef[ind][3]; ('name': 'tiff', 'position': 0, 'value': array(0x4D, 0x4D), 'order': 'bigEndian'),
case imageDef[ind][0] of );
'Gif': stringObj_ := new TMemoryStream();
[px_width, px_height, horz_dpi, vert_dpi] := gif_dimensions(stream); size := length(stream);
'Bmp': stringObj_.Write(stream[0:size-1], size);
[px_width, px_height, horz_dpi, vert_dpi] := bmp_dimensions(stream); index := getImageType(imagetypeDef);
'Tiff': ExtFileName := imagetypeDef[index]['name'];
[px_width, px_height, horz_dpi, vert_dpi] := tiff_dimensions(stream, imageDef[ind][2]); case ExtFileName of
End; 'bmp':
End; [width_, height_] := bmp_dimensions();
//println('width={},height={}',px_width, px_height); 'png':
end; [width_, height_] := png_dimensions();
'gif':
[width_, height_] := gif_dimensions();
'tiff':
[width_, height_] := tiff_dimensions(imagetypeDef[i]['order']);
'jpeg', 'jpg':
[width_, height_] := jpeg_dimensions();
end
//println("width_ = {}, height_ = {}", width_, height_);
End;
Function getImageType(def);
Begin
for i:=0 to length(def)-1 do
begin
value := def[i]['value'];
stringObj_.Seek(def[i]['position']);
for j:=0 to length(value)-1 do
begin
r := 0;
stringObj_.Read(r, 1);
if r <> value[j] then break;
if j = length(value)-1 then return i;
end
end
return -1;
End
Function Width(); Function Width();
Begin Begin
return integer((px_width / horz_dpi) * 914400); return width_;
End; End;
Function Height(); Function Height();
Begin Begin
return integer((px_height / vert_dpi) * 914400); return height_;
End;
(*
Function exif_dimensions(stream, r);
Begin
start := 0;
code := nil;
while code <> 0xd9 do Begin
[code, off] := _exif_next(start);
case code of
0xe0:
Begin
segment_length := _read_int(stream, off, '', 2);
density_units := _read_int(stream, off + 9, '', 1);
horz_dpi := read_int(stream, off + 10, '', 2);
vert_dpi := read_int(stream, off + 12, '', 2);
End;
End;
start := off + segment_length;
End;
End; End;
Function _exif_next(start); Property Content read readContent;
Function readContent();
Begin Begin
while 1 do Begin return content_;
position := _offset_of_next_ff_byte(start);
[position, byte_] = _next_non_ff_byte(position+1);
start := position + 1;
if byte_ = 0x00 then continue;
marker_code := byte_;
segment_offset := position + 1;
break;
End;
return array(marker_code, segment_offset);
End; End;
Function _offset_of_next_ff_byte(start); Function jpeg_dimensions();
Begin Begin
for i:=start to length(content_)-1 do Begin des1 := 0xff;
byte_ := ord(content_[i]); des2 := 0xc0;
if byte_ <> 0xff then return array(start - 1, byte_); flag := 1;
return array(start, 0); while flag do
begin
stringObj_.Read(byte1, 1);
stringObj_.Read(byte2, 1);
if byte1 = des1 and byte2 = des2 then flag := 0;
end
if flag then return array(0, 0);
stringObj_.Read(tmp, 3);
height := readBigEndianByte(2);
width := readBigEndianByte(2);
return array(width, height);
End; End;
Function _next_non_ff_byte(start); Function tiff_dimensions(byteOrder);
Begin Begin
for i:=start to length(content_)-1 do Begin stringObj_.seek(4);
byte_ := ord(content_[i]); if byteOrder = 'bigEndian' then
if byte_ = 0xff then return start - 1; IFD := readBigEndianByte(4);
return start; else stringObj_.Read(IFD, 4);
End; return array(0, 0);
*)
Function tiff_dimensions(stream, r);
Begin
off := _read_int(stream, 4, r[0], 4);
count := _read_int(stream, off, r[0], 2);
m := array();
for i:=0 to count-1 do Begin
dir_entry_offset := off + 2 + i*12;
tag_code := _read_int(stream, dir_entry_offset, r[0], 2);
field_type := _read_int(stream, dir_entry_offset + 2, r[0], 2);
value_count := _read_int(stream, dir_entry_offset + 4, r[0], 4);
value_offset := _read_int(stream, dir_entry_offset + 8, r[0], 4);
case field_type of
2:
val := stream[value_offset:value_offset+value_count-1];
3:
if value_count=1 then
val := _read_int(stream, dir_entry_offset + 8, r[0], 2);
4:
if value_count=1 then
val := _read_int(stream, dir_entry_offset + 8, r[0], 4);
5:
if value_count=1 then Begin
numerator := _read_int(stream, value_offset, r[0], 4);
denominator := _read_int(stream, value_offset + 4, r[0], 4);
val := numerator / denominator;
End;
End;
println('off={}, tag={},type={},value_count={},value_off={},val={}',off, tag_code,field_type,value_count,value_offset,val);
m[tag_code] := val;
End;
px_width := m[256];
px_height := m[257];
horz_dpi := _tiff_dpi(282, m);
vert_dpi := _tiff_dpi(283, m);
return array(px_width, px_height, horz_dpi, vert_dpi);
End; End;
Function _read_int(stream, off, t, size); Function bmp_dimensions();
Begin Begin
stm := new TMemoryStream(); stringObj_.Seek(0x12);
if t = 'M' then Begin width := 0;
s := ''; stringObj_.Read(width, 4);
setlength(s, 8); height := 0;
for i:=0 to size-1 do Begin stringObj_.Read(height, 4);
s[i] := stream[off + size - i - 1];
End; //stringObj_.Seek(0x26);
stm.Write(s, size); //horz_px_per_meter := 0;
End //stringObj_.Read(horz_px_per_meter, 4);
else Begin //vert_px_per_meter := 0;
stm.Write(stream[off:off + 7], size); //stringObj_.Read(vert_px_per_meter, 4);
End; //horz_dpi_ := _bmp_dpi(horz_px_per_meter);
stm.Seek(0); //vert_dpi_ := _bmp_dpi(vert_px_per_meter);
v := 0; return array(width, height);
stm.Read(v, size);
return v;
End; End;
Function bmp_dimensions(stream); Function png_dimensions();
Begin Begin
s := new TMemoryStream(); stringObj_.Seek(0x10);
s.Write(stream[0:63], 64); width := readBigEndianByte(4);
s.Seek(0x12); height := readBigEndianByte(4);
px_width := 0; return array(width, height);
s.Read(px_width, 4);
px_height := 0;
s.Read(px_height, 4);
s.Seek(0x26);
horz_px_per_meter := 0;
s.Read(horz_px_per_meter, 4);
vert_px_per_meter := 0;
s.Read(vert_px_per_meter, 4);
horz_dpi := _bmp_dpi(horz_px_per_meter);
vert_dpi := _bmp_dpi(vert_px_per_meter);
return array(px_width, px_height, horz_dpi, vert_dpi);
End; End;
Function gif_dimensions(stream); Function gif_dimensions();
Begin Begin
s := new TMemoryStream(); stringObj_.Seek(0x6);
s.Write(stream[6:9], 4); width := 0;
s.Seek(0); stringObj_.Read(width, 2);
px_width := 0; height := 0;
s.Read(px_width, 2); stringObj_.Read(height, 2);
px_height := 0; return array(width, height);
s.Read(px_height, 2);
return array(px_width, px_height, 72, 72);
End; End;
Function getImageDef(stream, imageDef); Function readBigEndianByte(num);
Begin Begin
for i:=0 to length(imageDef)-1 do Begin ret := 0;
off := imageDef[i][1]; while num do
len := length(imageDef[i][2]); begin
buf := stream[off:off+len-1]; stringObj_.Read(b, 1);
if eq(buf, imageDef[i][2]) then Begin ret := _Shl(ret, 8);
return i; ret += b;
End; num --;
End; end
return -1; return ret;
End; End;
Function eq(buf, r);
Begin
for i:=0 to length(buf)-1 do Begin
c := ifstring(r) ? r[i + 1] : r[i];
if ifint(c) then c := chr(c);
if c <> buf[i+1] then return 0;
End;
return 1;
End;
Function _tiff_dpi(tag, m);
Begin
if not ifint(m[tag]) then return 72;
v := ifint(m[296]) ? m[296] : 2;
if v = 1 then return 72;
units_per_inch := (v=2 ? 2.54 : 1);
dots_per_unit := m[tag];
return integer(round(dots_per_unit * units_per_inch));
End;
Function _bmp_dpi(px_per_meter);
Begin
if px_per_meter = 0 then return 96;
return integer(round(px_per_meter * 0.0254));
End;
Name;
content_;
px_width:integer;
px_height:integer;
horz_dpi:integer;
vert_dpi:integer;
ExtFileName:string; ExtFileName:string;
Name:string;
content_;
stringObj_;
width_:integer;
height_:integer;
End; End;

View File

@ -15,14 +15,16 @@ Type xlsxImage = Class
return new xlsxImage(sheet, excel); return new xlsxImage(sheet, excel);
End; End;
Function SetSheetBackground(picture); Function SetSheetBackground(pic);
Begin Begin
if not ifBinary(picture.Image) or length(picture.Image) = 0 then if not ifBinary(pic.Image) or length(pic.Image) = 0 then
return "Invalid Image Data."; return "Invalid Image Data.";
picture := new TSImage(pic.Image);
image_file := getImageFileName(picture); image_file := getImageFileName(picture);
rels_xmlfile := excel_.WorkBook().GetSheetRelsFile(sheetName_); rels_xmlfile := excel_.WorkBook().GetSheetRelsFile(sheetName_);
prefix := ReplaceStr(image_file, 'xl/', '../'); prefix := ReplaceStr(image_file, 'xl/', '../');
//println("image_file = {}", image_file);
[rid, target] := class(TSXml).FindRelationshipRid(rels_xmlfile, prefix); [rid, target] := class(TSXml).FindRelationshipRid(rels_xmlfile, prefix);
if target = '' then if target = '' then
begin begin
@ -30,7 +32,7 @@ Type xlsxImage = Class
class(TSXml).AddRelationshipRid(rels_xmlfile, prefix, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image', 'rId' $ rid); class(TSXml).AddRelationshipRid(rels_xmlfile, prefix, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image', 'rId' $ rid);
end end
content_xml := excel_.WorkBook().GetXmlFileObj(class(TSXml).GetFileName('Content_Types')); content_xml := excel_.WorkBook().GetXmlFileObj(class(TSXml).GetFileName('Content_Types'));
class(TSXml).AddDefaultContentType(content_xml, 'jpeg', 'image/jpeg'); class(TSXml).AddDefaultContentType(content_xml, picture.ExtFileName, 'image/' $ picture.ExtFileName);
sheet_xml := excel_.WorkBook().GetSheetXmlfile(sheetName_); sheet_xml := excel_.WorkBook().GetSheetXmlfile(sheetName_);
node := sheet_xml.FirstChildElement('worksheet'); node := sheet_xml.FirstChildElement('worksheet');
@ -44,10 +46,11 @@ Type xlsxImage = Class
picture_node.SetAttribute('r:id', 'rId' + inttostr(rid)); picture_node.SetAttribute('r:id', 'rId' + inttostr(rid));
End End
Function AddPicture(topLeft, bottomRight, picture, format); Function AddPicture(topLeft, bottomRight, pic, format);
Begin Begin
if not ifBinary(picture.Image) or length(picture.Image) = 0 then if not ifBinary(pic.Image) or length(pic.Image) = 0 then
return "Invalid Image Data."; return "Invalid Image Data.";
picture := new TSImage(pic.Image);
[err1, col1, row1] := excel_.CellNameToCoordinates(topLeft); [err1, col1, row1] := excel_.CellNameToCoordinates(topLeft);
[err2, col2, row2] := excel_.CellNameToCoordinates(bottomRight); [err2, col2, row2] := excel_.CellNameToCoordinates(bottomRight);
if err1 or err2 then return 'Invalid params.'; if err1 or err2 then return 'Invalid params.';
@ -62,7 +65,7 @@ Type xlsxImage = Class
class(TSXml).AddRelationshipRid(drawing_rels, image_target, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image', 'rId' $ rid); class(TSXml).AddRelationshipRid(drawing_rels, image_target, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image', 'rId' $ rid);
end end
content_xml := excel_.WorkBook().GetXmlFileObj(class(TSXml).GetFileName('Content_Types')); content_xml := excel_.WorkBook().GetXmlFileObj(class(TSXml).GetFileName('Content_Types'));
class(TSXml).AddDefaultContentType(content_xml, 'jpeg', 'image/jpeg'); class(TSXml).AddDefaultContentType(content_xml, picture.ExtFileName, 'image/' $ picture.ExtFileName);
sheet_xml := excel_.WorkBook().GetSheetXmlfile(sheetName_); sheet_xml := excel_.WorkBook().GetSheetXmlfile(sheetName_);
node := sheet_xml.FirstChildElement('worksheet'); node := sheet_xml.FirstChildElement('worksheet');
drawing_node := node.FirstChildElement('drawing'); drawing_node := node.FirstChildElement('drawing');
@ -149,7 +152,7 @@ private
image_file := ''; image_file := '';
for i:=0 to length(image_files)-1 do for i:=0 to length(image_files)-1 do
begin begin
if zipfile_.Diff(image_files[i], picture.Image) = 0 then if zipfile_.Diff(image_files[i], picture.Content) = 0 then
begin begin
image_file := image_files[i]; image_file := image_files[i];
break; break;
@ -158,8 +161,8 @@ private
if image_file = '' then if image_file = '' then
begin begin
image_count := length(image_files) + 1; image_count := length(image_files) + 1;
image_file := 'xl/media/image' $ image_count $ '.jpeg'; image_file := 'xl/media/image' $ image_count $ '.' $ picture.ExtFileName;
zipfile_.Add(image_file, picture.Image); zipfile_.Add(image_file, picture.Content);
end end
return image_file; return image_file;
End End

View File

@ -1,5 +1,15 @@
# 更新日志 # 更新日志
## 2023-4-18
### V1.3.0
支持常用图片的高宽自动识别,支持图片格式有`gif, png, jpeg/jpeg, bmp`
#### excel
1. 支持excel程序设置字体样式粗体斜体等此类问题的返回类型如`style.Font.Bold`返回`true 或 false`
## 2023-4-4 ## 2023-4-4
### V1.2.9 ### V1.2.9