In the XML file corresponding the the worksheet, a cell is an entity like this:
<c r="A2" t="n">
<v>18.999</v>
</v>
- Attribut
r
is the address of the cell in the worksheet. - Attribut
t
type of the value stored by the cell (n=numeric, s=string, b=boolean), 'n' is the default value.. -
Entity
<v>
contains the unformatted value in case of numeric or boolean. But, in case of a string,<v>
contains the index value of the string stored in the file "xl/sharedStrings.xml" .
So your problem is to replace a combination such as :
file "xl/worksheets/sheet1.xml" :
<c r="A2" t="s">
<v>29</v>
</c>
file "xl/sharedStrings.xml" :
<sst>
...
<si><t>placeholder_1</t></si> // 29th entity
...
</sst>
with :
file "xl/worksheets/sheet1.xml" :
<c r="A2" t="n">
<v>100001</v>
</c>
So in order to change the placeholder and the cell type, you have to :
- Search into
"sharedStrings.xml"
for the index of entity<si>
where is placed your placeholder (the value of the index is not explicit in"sharedStrings.xml"
). - Search into
"sheet1.xml"
for the<v>
entity that contains this index. - Replace the value of this
<v>
entity with the unformatted numerical value. - Replace the attribute of its parent
<c>
entity with"n"
(or delete attributet="s"
).
The good news are :
- You can leave unused shared string in
"xl/sharedStrings.xml"
, this will not make an error. - You can apply a number format to your cell while it contains the string value
placeholder_1
. This number format will be kept and applied to the number when you'll change the cell type.
If you have the pleasure to work with PHP, there is a nice library call OpenTBS that provides the feature of replacing textual placeholders in Excel worksheets and change the cell type if needed. OpenTBS is a plug-in for the TinyButStrong template engine for working with Ms Office and LibreOffice.