Compare commits

..
1 Commits
Author SHA1 Message Date
csh 4a2961a635 Merge branch 'word' 2023-12-07 13:41:38 +08:00
35 changed files with 13160 additions and 14151 deletions
-4
View File
@@ -1,4 +0,0 @@
*.swp
*.swo
*.cmd
script.ps1
-3
View File
@@ -1,3 +0,0 @@
[submodule "funcext/TSOffice/Extensions/OfficeVba"]
path = funcext/TSOffice/Extensions/OfficeVba
url = https://git.mytsl.cn/tinysoft/OfficeVba.git
+1 -1
View File
@@ -73,7 +73,7 @@ content := docx.AddTableContent(paragraphTitle, 1, 3);
docx.AddPageBreak(content.node_);
v := docx.SaveAs('', 'DocxFile使用帮助.docx');
v := docx.SaveAs('', TOfficeApi().CurCodePageToGBK('DocxFile使用帮助.docx'));
println('Test Over!\n Save {}: {},time={}秒', file, v, mtoc);
//info := GetProfilerInfo(true);
+3 -5
View File
@@ -1,8 +1,8 @@
path := ExtractFileDir(ExtractFileDir(PluginPath()));
{$IFNDEF Win32}
path := path + '/funcext/TSOffice/TSXlsxFile.tsf';
{$ELSE}
path := path + '\\funcext\\TSOffice\\TSXlsxFile.tsf';
{$ELSE}
path := path + '/funcext/TSOffice/TSXlsxFile.tsf';
{$ENDIF}
funcs := LoadClassInfo(path);
@@ -43,7 +43,6 @@ PrintSuccess("GetCellRichText", testing++);
file.MergeCell('XlsxFile', 'A1', 'C1');
PrintSuccess("MergeCell", testing++);
file.SetRowHeight('XlsxFile', 1, 60);
style := TOfficeObj('TStyle');
@@ -139,7 +138,7 @@ End
file.SetColWidth('Functions', 'A', 'A', 120);
PrintSuccess('SetColWidth', testing++);
saveName := "XlsxFile使用帮助.xlsx";
saveName := utf8toansi("XlsxFile使用帮助.xlsx");
[err, errinfo] := file.SaveAs('', saveName);
if err then
return PrintError("SaveAs", err, errinfo);
@@ -321,7 +320,6 @@ Begin
excel.SetCellValue(sheet_name, cell_1, name);
excel.MergeCell(sheet_name, cell_1, cell_2);
excel.SetCellStyle(sheet_name, cell_1, cell_2, styleid1);
excel.SetRowHeight(sheet_name, row, 25);
row++;
desc_arr := obj_info[i]['Desc'];
Binary file not shown.
Binary file not shown.
+5 -12
View File
@@ -1,4 +1,4 @@
# TSOffice
# OfficePlugin
TSOffice 项目:纯 TSL 代码实现 excel、word 文件读写
@@ -6,19 +6,19 @@ TSOffice 项目:纯 TSL 代码实现 excel、word 文件读写
### windows
对应文件夹的 dll 文件按如下要求
对应文件夹的dll文件按如下要求
- `office_plugin` 放入 tsl 安装根目录下 Plugin 文件夹
可选:
- `fmt_pubkrnl_plugin` 放入 tsl 安装根目录下 Plugin 文件夹,若需要执行 Demo 文件夹下的[XlsxHelp.tsl](./Demo/XlsxHelp.tsl)和[DocxHelp.tsl](./Demo/DocxHelp.tsl),需要部署改动态库
- `fmt_pubkrnl_plugin` 放入 tsl 安装根目录下 Plugin 文件夹,若需要执行Demo文件夹下的[XlsxHelp.tsl](./Demo/XlsxHelp.tsl)和[DocxHelp.tsl](./Demo/DocxHelp.tsl),需要部署改动态库
### Linux
#### Ubuntu-18.04
根据架构选择 aarch64x86 版本将对应文件夹内容进行部署
根据架构选择aarch64x86版本将对应文件夹内容进行部署
- `liboffice_plugin` 放入 tsl 安装根目录下 Plugin 文件夹
@@ -28,14 +28,7 @@ TSOffice 项目:纯 TSL 代码实现 excel、word 文件读写
- [Word 帮助文档](./DocxFile使用帮助.docx)
## 扩展
扩展模块的内容需要通过`git submodule update --init --recursive`获取,相关帮助文档在`./funcext/TSOffice/Extensions/模块名`目录下可获取
支持的扩展:
- **OfficeVba** 可通过类 Vba 方式对 office文件 进行读写操作。
## Demo
Demo 文件夹提供了一些参考的 demo
Demo文件夹提供了一些参考的demo
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
+271 -299
View File
@@ -1,299 +1,271 @@
Type NodeInfo = class
public
Function Create(parentObj, name);
Begin
if ifObj(parentObj) then
NodeUri := parentObj.NodeUri = '' ? name : (parentObj.NodeUri + '/' + name);
else
NodeUri := '';
NodeName := name;
ExtAttr := array();
ExtNodes := array();
ReplaceArr := array();
End
Function Root(); virtual;
Begin
return RootObj;
End
Function Update(position);overload;
Begin
if ifObj(RootObj) then Begin
arr := Marshal();
if length(arr['attributes']) or length(arr['children']) then Begin
curNode := class(TSXml).GetNode(RootObj, NodeUri, position);
if ifObj(curNode) then
class(TSXml).UpdateNode(curNode, arr['attributes'], arr['children']);
End;
End;
End;
Function Update();overload;
Begin
self.Update("end");
End;
Function Delete();overload;
Begin
if ifObj(RootObj) then
begin
curNode := class(TSXml).GetNode(RootObj, NodeUri);
if ifObj(curNode) then curNode.DeleteChildren();
end
End;
Function Delete(name);overload;
Begin
if not ifObj(RootObj) then return false;
node := NodeUri <> '' ? class(TSXml).GetNode(RootObj, NodeUri) : RootObj;
attrs := GetAttrsEx();
lName := lowerCase(name);
r := sselect * from attrs where lName = lowerCase([0]) end;
if istable(r) then
begin
node.DeleteAttribute(r[1]);
return true;
end
children := GetChildren();
r := select * from children where lName = lowerCase(['field']) end;
if istable(r) then Begin
r := r[0];
delete_node := node.FirstChildElement(r['name']);
if ifObj(delete_node) then
begin
node.DeleteChild(delete_node);
return true;
end
End;
return false;
End;
Function GetAttrs(); virtual;
Begin
return ExtAttr;
End
Function GetChildren(); virtual;
Begin
return ExtNodes;
End
Function GetNode(index); // 返回的index的对象
Begin
return ExtNodes[index]['obj'];
End
Function GetCount(); // 返回当前节点的个数
Begin
return length(ExtAttr);
End
// arr := array(('Size', 'sz', 15), ('Style', 'style', 'line'));
Function AddAttr(arr); // 添加属性
Begin
if not istable(arr) then return;
ExtAttr union= arr;
return 1;
End
Function Marshal();
Begin
children_ := nil;
GetChildrenEx(); //优化为变量,减少数据COPY2022-12-10
child_arr := array();
name_map := array();
len := 0;
for i:=0 to length(children_)-1 do
begin
node_type := children_[i]['nodeType'];
obj := children_[i]['obj'];
if ifnil(obj) then continue;
if not ifObj(obj) and not ifnil(name_map[children_[i]['name']]) and children_[i]['attrEx'] <> '' then
begin
child_arr[name_map[children_[i]['name']]]['attributes'] union=array(children_[i]['attrEx'] : obj);
continue;
end
arr := array('type': 'element', 'name': children_[i]['name'], 'attributes': array());
if node_type = 'empty' then // <b/>
begin
empty_name := children_[i]['attrEx'] ? children_[i]['attrEx'] : 'val';
if obj = 0 then arr['attributes'] := array(empty_name: 0);
else if obj = 1 then arr['attributes'] := array(empty_name: 1);
end
else if node_type = 'pcdata' then
begin
arr['children'] := array(('type': 'pcdata', 'value': obj));
end
else if ifObj(obj) then
begin
marshal := obj.Marshal();
if length(marshal['children'])=0 and length(marshal['attributes'])=0 then continue;
arr['children'] := marshal['children'];
arr['attributes'] union= marshal['attributes'];
end
else
begin
key := children_[i]['attrName'] ? children_[i]['attrName'] : children_[i]['attrEx'] ? children_[i]['attrEx'] : 'val';
arr['attributes'] := array(key : obj);
end
name_map[children_[i]['name']] := len;
child_arr[len++] := arr;
end
name_arr := str2array(NodeName, '/');
tmp_arr := array('type': 'element', 'name': name_arr[length(name_arr)-1], 'children': child_arr);
for i:=length(name_arr)-2 downto 0 do
begin
tmp_arr := array('type': 'element', 'name': name_arr[i], 'children': array(tmp_arr));
end
attrs_arr := GetAttrs();
tmp_arr['attributes'] := array();
for i:=0 to length(attrs_arr)-1 do
begin
if not ifnil(attrs_arr[i, 2]) then Begin
tmp_arr['attributes'][ attrs_arr[i, 1] ] := attrs_arr[i, 2];
End;
end
return tmp_arr;
End
Function NewChildNode(nodeArr);
Begin
if not ifarray(nodeArr) then return 0;
if not ifstring(nodeArr['name']) then return 0;
arr := array((
'name': nodeArr['name'],
'obj': nodeArr['obj'],
'attrEx': nodeArr['attrEx'],
'nodeType': nodeArr['nodeType'],
));
ExtNodes union= arr;
return 1;
End
Function Value(name, nottransferable);
Begin
if not ifObj(RootObj) then return nil;
if NodeUri <> '' then
node := class(TSXml).GetNode(RootObj, NodeUri);
else
node := RootObj;
if not ifObj(node) then return nil;
attrs := GetAttrsEx();
lName := lowerCase(name);
r := sselect * from attrs where lName = lowerCase([0]) end;
if istable(r) then
begin
value := node.GetAttribute(r[1]);
return nottransferable ? value : trystrtofloat(value, r) ? r : value;
end
children := GetChildren();
r := select * from children where lName = lowerCase(['field']) end;
if istable(r) then Begin
r := r[0];
node := node.FirstChildElement(r['name']);
if not ifObj(node) and r['nodeType'] = 'empty' and not nottransferable then return false;
if not ifObj(node) then return nil;
if r['nodeType'] = 'pcdata' then //返回文本串
return node.GetText();
if ifObj(r['obj']) then //对象,返回XML串
return node.Data();
key := r['attrName'] ? r['attrName'] : r['attrEx'] ? r['attrEx'] : 'val';
value := node.GetAttribute(key);
if r['nodeType'] = 'empty' and value = '' then return true;
return nottransferable ? value : trystrtofloat(value, r) ? r : value;
End;
return nil;
End;
Function Replace(hash, bChild);
Begin
if istable(hash) then Begin
ReplaceArr := hash;
replaceNodeName();
if bChild then Begin
r := GetChildren();
for i:=0 to length(r)-1 do Begin
if ifObj(r[i]['obj']) then
r[i]['obj'].Replace(hash, bChild);
End;
End;
End;
End
Function GetChildrenEx();
Begin
if not ifarray(children_) then Begin
children_ := getChildren();
if not istable(ReplaceArr) then Begin
for i:=0 to length(children_)-1 do Begin
if ifarray(children_[i]['obj']) then Begin
if istable(children_[i]['obj']) then
hasArr := 1;
else
children_[i]['obj'] := nil;
End;
End;
End;
if hasArr or istable(ReplaceArr) then Begin
a := array();
for i:=0 to length(children_)-1 do Begin
if istable(ReplaceArr) then
children_[i]['name'] := ReplaceName(children_[i]['name']);
if istable(children_[i]['obj']) then Begin
a union= children_[i]['obj'];
End
else
a[ length(a) ] := children_[i];
end;
children_ := a;
End;
End;
End;
Function GetAttrsEx();
Begin
return GetAttrs();//暂时没有发现需要更新属性前缀的情况
if not istable(ReplaceArr) then return GetAttrs();
attr := GetAttrs();
for i:=0 to length(attr)-1 do
attr[i][1] := ReplaceName(attr[i][1]);
return attr;
End
private
Function replaceNodeName();
Begin
for k, v in ReplaceArr do
begin
NodeName := ReplaceStr(NodeName, k, v);
end
End
Function ReplaceName(name);
Begin
for k,v in ReplaceArr do Begin
if k='' then
name := v + name;
else
name := ReplaceStr(name, k, v);
End;
return name;
End;
public
NodeName;
ExtAttr;
ExtNodes;
ReplaceArr;
RootObj; // xml node
NodeUri:string;
children_;
End
Type NodeInfo = class
public
Function Create(parentObj, name);
Begin
if ifObj(parentObj) then
NodeUri := parentObj.NodeUri = '' ? name : (parentObj.NodeUri + '/' + name);
else
NodeUri := '';
NodeName := name;
ExtAttr := array();
ExtNodes := array();
ReplaceArr := array();
End
Function Root(); virtual;
Begin
return RootObj;
End
Function Update(position);overload;
Begin
if ifObj(RootObj) then Begin
arr := Marshal();
if length(arr['attributes']) or length(arr['children']) then Begin
curNode := class(TSXml).GetNode(RootObj, NodeUri, position);
if ifObj(curNode) then
class(TSXml).UpdateNode(curNode, arr['attributes'], arr['children']);
End;
End;
End;
Function Update();overload;
Begin
self.Update("end");
End;
Function Delete();
Begin
if ifObj(RootObj) then
begin
curNode := class(TSXml).GetNode(RootObj, NodeUri);
if ifObj(curNode) then curNode.DeleteChildren();
end
End;
Function GetAttrs(); virtual;
Begin
return ExtAttr;
End
Function GetChildren(); virtual;
Begin
return ExtNodes;
End
Function GetNode(index); // 返回的index的对象
Begin
return ExtNodes[index]['obj'];
End
Function GetCount(); // 返回当前节点的个数
Begin
return length(ExtAttr);
End
// arr := array(('Size', 'sz', 15), ('Style', 'style', 'line'));
Function AddAttr(arr); // 添加属性
Begin
if not istable(arr) then return;
ExtAttr union= arr;
return 1;
End
Function Marshal();
Begin
children_ := nil;
GetChildrenEx(); //优化为变量,减少数据COPY2022-12-10
child_arr := array();
name_map := array();
len := 0;
for i:=0 to length(children_)-1 do
begin
node_type := children_[i]['nodeType'];
obj := children_[i]['obj'];
if ifnil(obj) then continue;
if not ifObj(obj) and not ifnil(name_map[children_[i]['name']]) and children_[i]['attrEx'] <> '' then
begin
child_arr[name_map[children_[i]['name']]]['attributes'] union=array(children_[i]['attrEx'] : obj);
continue;
end
arr := array('type': 'element', 'name': children_[i]['name'], 'attributes': array());
if node_type = 'empty' then // <b/>
begin
empty_name := children_[i]['attrEx'] ? children_[i]['attrEx'] : 'val';
if obj = 0 then arr['attributes'] := array(empty_name: 0);
else if obj = 1 then arr['attributes'] := array(empty_name: 1)
end
else if node_type = 'pcdata' then
begin
arr['children'] := array(('type': 'pcdata', 'value': obj));
end
else if ifObj(obj) then
begin
marshal := obj.Marshal();
if length(marshal['children'])=0 and length(marshal['attributes'])=0 then continue;
arr['children'] := marshal['children'];
arr['attributes'] union= marshal['attributes'];
end
else
begin
key := children_[i]['attrName'] ? children_[i]['attrName'] : children_[i]['attrEx'] ? children_[i]['attrEx'] : 'val';
arr['attributes'] := array(key : obj);
end
name_map[children_[i]['name']] := len;
child_arr[len++] := arr;
end
name_arr := str2array(NodeName, '/');
tmp_arr := array('type': 'element', 'name': name_arr[length(name_arr)-1], 'children': child_arr);
for i:=length(name_arr)-2 downto 0 do
begin
tmp_arr := array('type': 'element', 'name': name_arr[i], 'children': array(tmp_arr));
end
attrs_arr := GetAttrs();
tmp_arr['attributes'] := array();
for i:=0 to length(attrs_arr)-1 do
begin
if not ifnil(attrs_arr[i, 2]) then Begin
tmp_arr['attributes'][ attrs_arr[i, 1] ] := attrs_arr[i, 2];
End;
end
return tmp_arr;
End
Function NewChildNode(nodeArr);
Begin
if not ifarray(nodeArr) then return 0;
if not ifstring(nodeArr['name']) then return 0;
arr := array((
'name': nodeArr['name'],
'obj': nodeArr['obj'],
'attrEx': nodeArr['attrEx'],
'nodeType': nodeArr['nodeType'],
));
ExtNodes union= arr;
return 1;
End
Function Value(name, nottransferable);
Begin
if not ifObj(RootObj) then return nil;
if NodeUri <> '' then
node := class(TSXml).GetNode(RootObj, NodeUri);
else
node := RootObj;
if not ifObj(node) then return nil;
attrs := GetAttrsEx();
lName := lowerCase(name);
r := sselect * from attrs where lName = lowerCase([0]) end;
if istable(r) then
begin
value := node.GetAttribute(r[1]);
return nottransferable ? value : trystrtofloat(value, r) ? r : value;
end
children := GetChildren();
r := select * from children where lName = lowerCase(['field']) end;
if istable(r) then Begin
r := r[0];
node := node.FirstChildElement(r['name']);
if not ifObj(node) and r['nodeType'] = 'empty' then return false;
if not ifObj(node) then return nil;
if r['nodeType'] = 'pcdata' then //返回文本串
return node.GetText();
if ifObj(r['obj']) then //对象,返回XML串
return node.Data();
key := r['attrName'] ? r['attrName'] : r['attrEx'] ? r['attrEx'] : 'val';
value := node.GetAttribute(key);
if r['nodeType'] = 'empty' and value = '' then return true;
return nottransferable ? value : trystrtofloat(value, r) ? r : value;
End;
return nil;
End;
Function Replace(hash, bChild);
Begin
if istable(hash) then Begin
ReplaceArr := hash;
replaceNodeName();
if bChild then Begin
r := GetChildren();
for i:=0 to length(r)-1 do Begin
if ifObj(r[i]['obj']) then
r[i]['obj'].Replace(hash, bChild);
End;
End;
End;
End
Function GetChildrenEx();
Begin
if not ifarray(children_) then Begin
children_ := getChildren();
if not istable(ReplaceArr) then Begin
for i:=0 to length(children_)-1 do Begin
if ifarray(children_[i]['obj']) then Begin
if istable(children_[i]['obj']) then
hasArr := 1;
else
children_[i]['obj'] := nil;
End;
End;
End;
if hasArr or istable(ReplaceArr) then Begin
a := array();
for i:=0 to length(children_)-1 do Begin
if istable(ReplaceArr) then
children_[i]['name'] := ReplaceName(children_[i]['name']);
if istable(children_[i]['obj']) then Begin
a union= children_[i]['obj'];
End
else
a[ length(a) ] := children_[i];
end;
children_ := a;
End;
End;
End;
Function GetAttrsEx();
Begin
return GetAttrs();//暂时没有发现需要更新属性前缀的情况
if not istable(ReplaceArr) then return GetAttrs();
attr := GetAttrs();
for i:=0 to length(attr)-1 do
attr[i][1] := ReplaceName(attr[i][1]);
return attr;
End
private
Function replaceNodeName();
Begin
for k, v in ReplaceArr do
begin
NodeName := ReplaceStr(NodeName, k, v);
end
End
Function ReplaceName(name);
Begin
for k,v in ReplaceArr do Begin
if k='' then
name := v + name;
else
name := ReplaceStr(name, k, v);
End;
return name;
End;
public
NodeName;
ExtAttr;
ExtNodes;
ReplaceArr;
RootObj; // xml node
NodeUri:string;
children_;
End
-27
View File
@@ -178,33 +178,6 @@ Type TOffice = Class
Begin
return getTextBoxImpl(p, true);
End;
///获取docx的类型
///普通段落:0
///图片段落:1
///表格:2
///其他:-1
Function GetDocxPartType(part);
Begin
if not ifObj(part) then return -1;
case part.name_ of
'w:p':
begin
if class(TSXml).GetNode(part.node_, 'w:r/w:drawing') then return 1;
return 0;
end
'w:tbl':
return 2;
else
return -1;
end;
End;
///单元格内的标题是否添加到目录
Function SetCellTableContent(flag);
Begin
hash_["CELLFLAG"] := flag;
End;
Function Set(k, v);
Begin
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -52,8 +52,8 @@ Type TDocxChart = Class(TSChart)
return true;
End;
[weakref]docx_;
docx_;
chartId_;
rId_;
xmlObj_;
End;
End;
File diff suppressed because it is too large Load Diff
+206 -207
View File
@@ -1,207 +1,206 @@
Type TDocxStyles = Class
//段落格式
Function Create(docx);
Begin
docx_ := docx;
stylesXml_ := docx.Zip().Get('word/styles.xml');
idMap_ := array();
nameMap_ := array();
maxStyleId_ := 1;
if ifObj(stylesXml_) then Begin
stylesNode := stylesXml_.FirstChildElement('w:styles');
if ifObj(stylesNode) then Begin
node := stylesNode.FirstChildElement('w:style');
while ifObj(node) do Begin
o := TOfficeObj('TDocxStyle');
o.Init(node);
id := StrToIntDef(o.StyleId, 0);
if id > 0 and maxStyleId_ <= id then
maxStyleId_ := id + 1;
_addStyle(o);
node := node.NextElement('w:style');
End;
pNode := class(TSXml).GetNode(stylesNode, 'w:docDefaults/w:pPrDefault');
if ifObj(pNode) then
defaultPpr_ := Class(TSXml).ReadPprFormat(pNode);
rNode := class(TSXml).GetNode(stylesNode, 'w:docDefaults/w:rPrDefault');
if ifObj(rNode) then
defaultRpr_ := Class(TSXml).ReadRprFormat(rNode);
End;
End;
End;
///缺省样式
///styleType:样式类型(paragraph、character、table
///返回:TDocxStyle对象
Function Default(styleType);
Begin
for k, obj in idMap_ do Begin
if obj.wType = styleType then Begin
v := obj.node_.GetAttribute('w:default');
if v <> '1' then
return obj;
End;
End
return nil;
End;
///返回指定名称的TDocxStyle
///name:样式名称
///返回:TDocxStyle对象
Function GetStyle(name);
Begin
return nameMap_[ class(TSXml).CurCodePageToUtf8(lowercase(name)) ];
End;
///返回指定StyleId名称的TDocxStyle
///idStyleID号(数字ID号或名称字符串)
///返回:TDocxStyle对象
Function GetStyleById(id);
Begin
newid := ifnumber(id) ? tostring(id) : id;
return idMap_[ newid ];
End;
//返回全部LatentStyles对象列表
//返回:TDocxStyle对象列表
Function LatentStyles();
Begin
r := array();
node := stylesXml_.FirstChildElement('w:styles');
if ifObj(node) then
node := node.FirstChildElement('w:latentStyles');
if ifObj(node) then Begin
node := node.FirstChildElement('w:lsdException');
while ifObj(node) do Begin
o := TOfficeObj('TDocxStyle');
o.Init(node);
r[ length(r) ] := o;
node := node.NextElement('w:lsdException');
End;
End;
return r;
End;
///返回全部Styles对象列表
///返回:TDocxStyle对象列表
Function Styles();
Begin
return idMap_;
End;
///插入新的段落样式
///styleId:样式ID,可以是自定义名称
///xmlStrInnerXml串
///返回:TDocxStyle对象
Function AddStyleByInnerXml(styleId, xmlStr);
Begin
if ifstring(styleId) and styleId <> '' then Begin
style := GetStyle(styleId);
if ifObj(style) then
return style;
node := stylesXml_.FirstChildElement('w:styles').InsertEndChild(xmlStr);
node.SetAttribute('w:styleId', styleId);
End
else Begin
node := stylesXml_.FirstChildElement('w:styles').InsertEndChild(xmlStr);
node.SetAttribute('w:styleId', maxStyleId_++);
End;
o := TOfficeObj('TDocxStyle');
o.Init(node);
_addStyle(o);
return o;
End;
///插入新的段落样式
///oTDocxStyle对象
///StyleId:样式ID,可以是自定义名称
///返回:TDocxStyle对象
Function AddStyle(o, StyleId);
Begin
if ifObj(o) then Begin
if ifString(StyleId) and StyleId <> '' and not ifObj(idMap_[ StyleId ]) then
o.StyleId := StyleId;
else
o.StyleId := maxStyleId_++;
node := stylesXml_.FirstChildElement('w:styles').InsertEndChild(o.Marshal());
obj := TOfficeObj('TDocxStyle');
obj.Init(node);
_addStyle(obj);
return obj;
End;
return nil;
End;
///复制样式
///newstyle:样式节点TDocxStyle对象
///返回:TDocxStyle对象
Function CopyStyle(newstyle);
Begin
if ifObj(newstyle) and ifObj(newstyle.node_) then Begin
node := stylesXml_.FirstChildElement('w:styles').InsertEndChild(newstyle.node_.Marshal()[0]);
node.DeleteAttribute('w:default');
obj := TOfficeObj('TDocxStyle');
obj.StyleId := node.GetAttribute('w:styleId');
obj.Name := node.FirstChildElement('w:name').GetAttribute('w:val');
obj.Init(node);
_addStyle(obj);
return obj;
End;
return nil;
End;
///插入新的LatentStyle段落样式
///oTDocxStyle对象
///返回:TDocxStyle对象
Function AddLatentStyle(o);
Begin
if ifObj(o) then Begin
latentStyles := stylesXml_.FirstChildElement('w:styles').InsertEndChild('w:latentStyles');
node := latentStyles.InsertEndChild('lsdException');
node.UnMarshal(o);
latentStyles.SetAttribute('w:count', length(LatentStyles()));
return node;
End;
return nil;
End;
///插入缺省的段落样式(Title、Heading1-9
///返回:TDocxStyle对象
Function AddDefaultStyle(Name);
Begin
styleId := AnsiReplaceText(Name, ' ', '');
charId := StyleId + 'Char';
styleFname := 'wStyle/' + styleId + '.xml';
cStyleFname := 'wStyle/' + charId + '.xml';
xmlData := TOfficeTemplate(styleFname);
if ifnil(xmlData) then
return xmlData;
o := AddStyleByInnerXml(styleId, xmlData);
charStyle := idMap_[charId];
if not ifObj(charStyle) then Begin
xmlData := TOfficeTemplate(cStyleFname);
if ifstring(xmlData) then Begin
AddStyleByInnerXml(charId, xmlData);
End;
End;
return o;
End;
Function _addStyle(o);
Begin
idMap_[ o.StyleId ] := o;
nameMap_[ lowercase(o.Name) ] := o;
End;
defaultPpr_;
defaultRpr_;
private
[weakref]docx_;
stylesXml_;
idMap_;
nameMap_;
maxStyleId_:integer;
End;
Type TDocxStyles = Class
//段落格式
Function Create(docx);
Begin
docx_ := docx;
stylesXml_ := docx.Zip().Get('word/styles.xml');
idMap_ := array();
nameMap_ := array();
maxStyleId_ := 1;
if ifObj(stylesXml_) then Begin
stylesNode := stylesXml_.FirstChildElement('w:styles');
if ifObj(stylesNode) then Begin
node := stylesNode.FirstChildElement('w:style');
while ifObj(node) do Begin
o := TOfficeObj('TDocxStyle');
o.Init(node);
id := StrToIntDef(o.StyleId, 0);
if id > 0 and maxStyleId_ <= id then
maxStyleId_ := id + 1;
_addStyle(o);
node := node.NextElement('w:style');
End;
pNode := class(TSXml).GetNode(stylesNode, 'w:docDefaults/w:pPrDefault');
if ifObj(pNode) then
defaultPpr_ := Class(TSXml).ReadPprFormat(pNode);
rNode := class(TSXml).GetNode(stylesNode, 'w:docDefaults/w:rPrDefault');
if ifObj(rNode) then
defaultRpr_ := Class(TSXml).ReadRprFormat(rNode);
End;
End;
End;
///缺省样式
///styleType:样式类型(paragraph、character、table
///返回:TDocxStyle对象
Function Default(styleType);
Begin
for k, obj in idMap_ do Begin
if obj.wType = styleType then Begin
v := obj.node_.GetAttribute('w:default');
if v <> '1' then
return obj;
End;
End
return nil;
End;
///返回指定名称的TDocxStyle
///name:样式名称
///返回:TDocxStyle对象
Function GetStyle(name);
Begin
return nameMap_[ class(TSXml).CurCodePageToUtf8(lowercase(name)) ];
End;
///返回指定StyleId名称的TDocxStyle
///idStyleID号(数字ID号或名称字符串)
///返回:TDocxStyle对象
Function GetStyleById(id);
Begin
newid := ifnumber(id) ? tostring(id) : id;
return idMap_[ newid ];
End;
//返回全部LatentStyles对象列表
//返回:TDocxStyle对象列表
Function LatentStyles();
Begin
r := array();
node := stylesXml_.FirstChildElement('w:styles');
if ifObj(node) then
node := node.FirstChildElement('w:latentStyles');
if ifObj(node) then Begin
node := node.FirstChildElement('w:lsdException');
while ifObj(node) do Begin
o := TOfficeObj('TDocxStyle');
o.Init(node);
r[ length(r) ] := o;
node := node.NextElement('w:lsdException');
End;
End;
return r;
End;
///返回全部Styles对象列表
///返回:TDocxStyle对象列表
Function Styles();
Begin
return idMap_;
End;
///插入新的段落样式
///styleId:样式ID,可以是自定义名称
///xmlStrInnerXml串
///返回:TDocxStyle对象
Function AddStyleByInnerXml(styleId, xmlStr);
Begin
if ifstring(styleId) and styleId <> '' then Begin
style := GetStyle(styleId);
if ifObj(style) then
return style;
node := stylesXml_.FirstChildElement('w:styles').InsertEndChild(xmlStr);
node.SetAttribute('w:styleId', styleId);
End
else Begin
node := stylesXml_.FirstChildElement('w:styles').InsertEndChild(xmlStr);
node.SetAttribute('w:styleId', maxStyleId_++);
End;
o := TOfficeObj('TDocxStyle');
o.Init(node);
_addStyle(o);
return o;
End;
///插入新的段落样式
///oTDocxStyle对象
///StyleId:样式ID,可以是自定义名称
///返回:TDocxStyle对象
Function AddStyle(o, StyleId);
Begin
if ifObj(o) then Begin
if ifString(StyleId) and StyleId <> '' and not ifObj(idMap_[ StyleId ]) then
o.StyleId := StyleId;
else
o.StyleId := maxStyleId_++;
node := stylesXml_.FirstChildElement('w:styles').InsertEndChild(o.Marshal());
obj := TOfficeObj('TDocxStyle');
obj.Init(node);
_addStyle(obj);
return obj;
End;
return nil;
End;
///复制样式
///newstyle:样式节点TDocxStyle对象
///返回:TDocxStyle对象
Function CopyStyle(newstyle);
Begin
if ifObj(newstyle) and ifObj(newstyle.node_) then Begin
node := stylesXml_.FirstChildElement('w:styles').InsertEndChild(newstyle.node_.Marshal()[0]);
obj := TOfficeObj('TDocxStyle');
obj.StyleId := node.GetAttribute('w:styleId');
obj.Name := node.FirstChildElement('w:name').GetAttribute('w:val');
obj.Init(node);
_addStyle(obj);
return obj;
End;
return nil;
End;
///插入新的LatentStyle段落样式
///oTDocxStyle对象
///返回TDocxStyle对象
Function AddLatentStyle(o);
Begin
if ifObj(o) then Begin
latentStyles := stylesXml_.FirstChildElement('w:styles').InsertEndChild('w:latentStyles');
node := latentStyles.InsertEndChild('lsdException');
node.UnMarshal(o);
latentStyles.SetAttribute('w:count', length(LatentStyles()));
return node;
End;
return nil;
End;
///插入缺省的段落样式(Title、Heading1-9
///返回:TDocxStyle对象
Function AddDefaultStyle(Name);
Begin
styleId := AnsiReplaceText(Name, ' ', '');
charId := StyleId + 'Char';
styleFname := 'wStyle/' + styleId + '.xml';
cStyleFname := 'wStyle/' + charId + '.xml';
xmlData := TOfficeTemplate(styleFname);
if ifnil(xmlData) then
return xmlData;
o := AddStyleByInnerXml(styleId, xmlData);
charStyle := idMap_[charId];
if not ifObj(charStyle) then Begin
xmlData := TOfficeTemplate(cStyleFname);
if ifstring(xmlData) then Begin
AddStyleByInnerXml(charId, xmlData);
End;
End;
return o;
End;
Function _addStyle(o);
Begin
idMap_[ o.StyleId ] := o;
nameMap_[ lowercase(o.Name) ] := o;
End;
defaultPpr_;
defaultRpr_;
private
docx_;
stylesXml_;
idMap_;
nameMap_;
maxStyleId_:integer;
End;
+1 -1
View File
@@ -228,7 +228,7 @@ Type TNumbering = Class
class(TSXml).UpdateNode(o.node_, arr['attributes'], arr['children']);
End;
private
[weakref]docx_;
docx_;
numberingXml_;
maxAbstractNumId_:integer;
maxNumId_:integer;
+4 -14
View File
@@ -94,16 +94,6 @@ Type TTableContent = class
_AddTableContent(posOpt, UpperHeadingLevel, LowerHeadingLevel);
End;
///添加目录条目
///UpperHeadingLevel:标题最高级别
///LowerHeadingLevel:标题最低级别
/// 使用 UpperHeadingLevel 属性可设置起始标题级别(最高)。例如,若要设置 TOC 域语法 {TOC \o "1-3"},可将 LowerHeadingLevel 属性设为 3,并将 UpperHeadingLevel 属性设为 1。
Function CustomAdd(posOpt, UpperHeadingLevel, LowerHeadingLevel);
Begin
//自定义页码计算与word、wps有差异,不推荐使用
_AddTableContent(posOpt, UpperHeadingLevel, LowerHeadingLevel);
End;
Function _AddDefaultTableContent(UpperHeadingLevel, LowerHeadingLevel);
Begin
p := _AddItem(UpperHeadingLevel, LowerHeadingLevel, UpperHeadingLevel - 1, true);
@@ -128,13 +118,13 @@ Type TTableContent = class
tab := TOfficeObj('TTabStop');
tab.Val := 'right';
tab.leader := 'dot';
tab.Position := TOfficeApi().Get("TTableContent-tab-position") ?: 8306;
tab.Position := 8640;
p.Format.Tabs.Add(0, tab);
if first then Begin
//fldCharType
r1 := p.AddRun();
r1.fldCharType := 'begin';
r1.Dirty := 0;
r1.Dirty := 'true';
//instrText
r2 := p.AddRun();
@@ -294,7 +284,7 @@ Type TTableContent = class
if ifObj(node) then
return node.GetAttribute('w:name');
bookMarkID := docx_.Document().GetBookMarkID();
return p.AddBookMark(bookMarkID);
return p.AddBookMark();
End;
Function _CheckNodes(name);
@@ -324,7 +314,7 @@ Type TTableContent = class
End;
End;
[weakref]docx_;
docx_;
node_;
impl_;
End;
@@ -20,11 +20,6 @@ Type docxDocument = Class
Begin
return body_;
End;
Function AddNameSpace(prefix, uri);
Begin
root_.SetAttribute('xmlns:' + prefix, uri);
End;
Function GetBookMarkID();
Begin
+1 -1
View File
@@ -65,7 +65,7 @@ Type xlsxChart = Class(TSChart)
node := drawingXmlObj_.FirstChild('xdr:wsDr').InsertEndChild(o.Marshal());
End;
private
[weakref]excel_;
excel_;
drawingRID_:integer;
chartId_;
chartRid_;
+2 -6
View File
@@ -148,11 +148,7 @@ xmlns:v="urn:schemas-microsoft-com:vml">
comment += r_node.FirstChildElement('t').GetText();
r_node := r_node.NextElement();
end
if length(comment) > 1 then
begin
if comment[1] = "\n" then comment := comment[2:];
else if comment[1:2] = "\r\n" then comment := comment[3:];
end
if length(comment) > 1 then comment := comment[2:];
return array(author, comment);
End;
@@ -184,7 +180,6 @@ private
End;
End;
[weakref]excel_;//TSXlsxFile对象
authors_;
commentId_;
commentFileName_:string;//'../comments/comment1.xml'
@@ -192,6 +187,7 @@ private
rId_:string;
sheetName_:string;//sheet名称
sheetFileName_;//xl/worksheets/sheetN.xml
excel_;//TSExcelFile对象
commentXmlFile_;//XmlFile对象
commentObjs_;
End;
+1 -1
View File
@@ -45,5 +45,5 @@ Type xlsxDocProps = Class
End;
private
[weakref]excel_; //TSExcelFile对象
excel_; //TSExcelFile对象
End;
@@ -33,6 +33,6 @@ Type xlsxHeaderFooter = Class
private
sheet_; //XmlSheet对象
[weakref]file_; //TSExcelFile对象
file_; //TSExcelFile对象
xmlFile_; //sheet对应的xml对象
End;
+1 -1
View File
@@ -92,7 +92,7 @@ Type xlsxHyperLink = Class
End;
private
[weakref]file_; //TSExcelFile对象
file_; //TSExcelFile对象
xmlFile_;//XmlFile对象
sheetName_;
+1 -1
View File
@@ -169,6 +169,6 @@ private
private
sheetName_:string; //sheet名称
[weakref]excel_;//TSExcelFile对象
excel_;//TSExcelFile对象
zipfile_;
End;
@@ -38,7 +38,7 @@ Type xlsxPageLayout = Class
End;
private
[weakref]file_; //TSExcelFile对象
file_; //TSExcelFile对象
sheet_;//XmlSheet对象
xmlFile_; //sheet对应的xml对象
End;
+1 -1
View File
@@ -113,7 +113,7 @@ private
private
sheetName_:string; //sheet名称
[weakref]excel_;//TSExcelFile对象
excel_;//TSExcelFile对象
zipfile_;
drawingXmlObj_;
End;
+1 -1
View File
@@ -95,7 +95,7 @@ Type xlsxSheetView = Class
End;
private
[weakref]file_; //TSExcelFile对象
file_; //TSExcelFile对象
sheet_;//XmlSheet对象
xmlFile_; //sheet对应的xml对象
End;
+22 -126
View File
@@ -1,29 +1,15 @@
Type xlsxStyles = Class
Function Create(file);
Function Create(sheetobj, file);
Begin
sheet_ := sheetobj;
file_ := file;
style_xml_file_ := file_.WorkBook().GetXmlFileObj('xl/styles.xml');
style_ := array();
Init();
styleXmlFile_ := file_.WorkBook().GetXmlFileObj('xl/styles.xml');
End
Function Init();
Begin
style_node := style_xml_file_.FirstChildElement("styleSheet");
xfs_node := style_node.FirstChildElement("cellXfs");
xf_node := xfs_node.FirstChildElement("xf");
count := 0;
while ifObj(xf_node) do
begin
style_[inttostr(count++)] := xf_node;
xf_node := xf_node.NextElement("xf");
end
End
Function GetStyleId(style);overload;
Begin
node := style_xml_file_.FirstChildElement('styleSheet');
node := styleXmlFile_.FirstChildElement('styleSheet');
font_id := insertNode(node, 'fonts', style.Font);
border_id := insertNode(node, 'borders', style.Border);
fill_id := insertNode(node, 'fills', style.Fill);
@@ -54,16 +40,15 @@ Type xlsxStyles = Class
node := node.FirstChildElement('cellXfs');
count := node.GetAttribute('count');
node.InsertEndChild(xf.Marshal());
node.SetAttribute('count', strtoint(count) + 1);
xf_node := node.InsertEndChild(xf.Marshal());
style_[count] := xf_node;
return count;
End;
Function GetStyleId(newStyle, oldStyleId);overload;
Begin
if oldStyleId = '' then return nil;
style_node := style_xml_file_.FirstChildElement('styleSheet');
style_node := styleXmlFile_.FirstChildElement('styleSheet');
cellXfs_node := style_node.FirstChildElement('cellXfs');
count := strtoint(cellXfs_node.GetAttribute('count'));
Id := strtoint(oldStyleId);
@@ -104,14 +89,19 @@ Type xlsxStyles = Class
xf_node.SetAttribute('applyProtection', 1);
end
style_[inttostr(count)] := xf_node;
return inttostr(count);
End;
Function GetStyle(styleId);
Begin
xf := style_[styleId];
if not ifObj(xf) then return nil;
if styleId = '' then return nil;
style_node := styleXmlFile_.FirstChildElement('styleSheet');
cellXfs_node := style_node.FirstChildElement('cellXfs');
count := strtoint(cellXfs_node.GetAttribute('count'));
Id := strtoint(styleId);
if Id > count-1 then return nil;
xf := getNode(cellXfs_node, 'xf', Id);
attrs := xf.Attributes();
numfmt_id := trystrtoint(attrs['numFmtId'], r) ? r : 0;
font_id := trystrtoint(attrs['fontId'], r) ? r : 0;
@@ -120,7 +110,6 @@ Type xlsxStyles = Class
alignment := xf.FirstChildElement('alignment');
protection := xf.FirstChildElement('protection');
style_node := style_xml_file_.FirstChildElement('styleSheet');
style := TOfficeObj('TStyle');
numFmts_node := style_node.FirstChildElement('numFmts');
fonts_node := style_node.FirstChildElement('fonts');
@@ -138,51 +127,12 @@ Type xlsxStyles = Class
return style;
End;
Function GetNumFmtId(styleId);
class Function NewObject(sheetname, file);
Begin
node := style_[styleId];
if ifnil(node) then return '0';
numfmt := node.GetAttribute("numFmtId");
return numfmt = '' ? '' : numfmt;
End;
Function GetType(styleId, value);
Begin
numfmt_id := GetNumFmtId(styleId);
if numfmt_id = '' then return array(nil, value);
numfmt_id := strtoint(numfmt_id);
if numfmt_id = 0 then return array(nil, value);
if numfmt_id >= 1 and numfmt_id <= 10 then return array('n', value);
if numfmt_id >= 11 and numfmt_id <= 26 then
begin
if ifstring(value) and (trystrtodate(value, r) or trystrtodatetime(value, r) or trystrtotime(value, r)) then return array('n', r);
t := ifstring(value) ? 's' : 'n';
return array(t, value);
end
if numfmt_id >= 27 and numfmt_id <= 34 then return array('e', value);
if numfmt_id >= 35 and numfmt_id <= 48 then return array('n', value);
if numfmt_id = 49 then return array('s', value);
if numfmt_id >= 50 and numfmt_id <= 55 then
begin
if ifstring(value) and trystrtodate(value, r) then return array('n', r);
t := ifstring(value) ? 's' : 'n';
return array(t, value);
end
if numfmt_id >= 56 and numfmt_id <= 58 then
begin
if ifstring(value) and trystrtodatetime(value, r) then return array('n', r);
t := ifstring(value) ? 's' : 'n';
return array(t, value);
end
else begin
if ifstring(value) then
begin
if (pos("-", value) or pos("/", value)) and (trystrtodatetime(value, r) or trystrtodate(value, r)) then return array('n', r);
if trystrtofloat(value, r) then return array("n", value);
return array("s", value);
end
return array("n", value);
end
o := file.WorkBook().GetSheetObj(sheetname);
if not ifObj(o) then return 0;
styles := new xlsxStyles(o, file);
return styles;
End;
private
@@ -336,61 +286,7 @@ private
End
private
[weakref]file_; //TSXlsxFile对象
style_xml_file_;
style_; // 缓存style
sheet_; //XmlSheet对象
file_; //TSExcelFile对象
styleXmlFile_; //xmlFile对象
End;
// 参考:https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.numberingformat?view=openxml-3.0.1
// 0 General
// 1 0
// 2 0.00
// 3 #,##0
// 4 #,##0.00
// 5 整数,货币符号旁边($#,##0_);($#,##0)
// 6 带两位小数的货币符号($#,##0.00_);($#,##0.00)
// 7 整数,带括号表示负数($#,##0_);Red
// 8 带两位小数的货币符号和红色负数($#,##0.00_);Red
// 9 0%
// 10 0.00%
// 11 0.00E+00
// 12 # ?/?
// 13 # ??/??
// 14 mm-dd-yy
// 15 d-mmm-yy
// 16 d-mmm
// 17 mmm-yy
// 18 h:mm AM/PM
// 19 h:mm:ss AM/PM
// 20 h:mm
// 21 h:mm:ss
// 22 m/d/yy h:mm
// 27 [$-404]e/m/d yyyy"年"m"月"
// 28 [$-404]e"年"m"月"d"日" m"月"d"日"
// 29 [$-404]e"年"m"月"d"日" m"月"d"日"
// 30 m/d/yy m-d-yy
// 31 yyyy"年"m"月"d"日" yyyy"年"m"月"d"日"
// 32 hh"時"mm"分" h"时"mm"分"
// 33 hh"時"mm"分"ss"秒" h"时"mm"分"ss"秒"
// 34 上午/下午hh"時"mm"分" 上午/下午h"时"mm"分"
// 35 上午/下午hh"時"mm"分"ss"秒" 上午/下午h"时"mm"分"ss"秒"
// 36 [$-404]e/m/d yyyy"年"m"月"
// 37 #,##0 ;(#,##0)
// 38 #,##0 ;[Red](#,##0)
// 39 #,##0.00;(#,##0.00)
// 40 #,##0.00;[Red](#,##0.00)
// 45 mm:ss
// 46 [h]:mm:ss
// 47 mmss.0
// 48 ##0.0E+0
// 49 @(文本)
// 50 [$-404]e/m/d yyyy"年"m"月"
// 51 [$-404]e"年"m"月"d"日" m"月"d"日"
// 52 上午/下午hh"時"mm"分" yyyy"年"m"月"
// 53 上午/下午hh"時"mm"分"ss"秒" m"月"d"日"
// 54 [$-404]e"年"m"月"d"日" m"月"d"日"
// 55 上午/下午hh"時"mm"分" 上午/下午h"时"mm"分"
// 56 上午/下午hh"時"mm"分"ss"秒" 上午/下午h"时"mm"分"ss"秒"
// 57 [$-404]e/m/d yyyy"年"m"月"
// 58 [$-404]e"年"m"月"d"日" m"月"d"日"
+1 -1
View File
@@ -126,7 +126,7 @@ private
private
sheetName_:string; //sheet名称
[weakref]excel_;//TSExcelFile对象
excel_;//TSExcelFile对象
zipfile_;
tableXmlFile_;
End;
File diff suppressed because it is too large Load Diff
+488 -839
View File
File diff suppressed because it is too large Load Diff