Excel 2003 XML格式-AutoFitWidth不工作
我有一个以Excel2003XML格式输出Excel工作簿的程序。它可以很好地工作,但有一个问题,我无法自动设置列宽Excel 2003 XML格式-AutoFitWidth不工作,xml,excel,openxml,Xml,Excel,Openxml,我有一个以Excel2003XML格式输出Excel工作簿的程序。它可以很好地工作,但有一个问题,我无法自动设置列宽 <xsl:value-of select="5.25 * Min((string-length(.)+2),200)"/> 我制作的一个片段: <Table > <Column ss:AutoFitWidth="1" ss:Width="2"/> <Row ss:AutoFitHeight="
<xsl:value-of select="5.25 * Min((string-length(.)+2),200)"/>
我制作的一个片段:
<Table >
<Column ss:AutoFitWidth="1" ss:Width="2"/>
<Row ss:AutoFitHeight="0" ss:Height="14.55">
<Cell ss:StyleID="s62"><Data ss:Type="String">Database</Data></Cell>
<xsl:value-of select="5.25 * Min((string-length(.)+2),200)"/>
数据库
这不会将列设置为自动调整。我试过不设置宽度,我试过很多方法,我被卡住了
<xsl:value-of select="5.25 * Min((string-length(.)+2),200)"/>
谢谢。仅自动安装日期和数字值:-(
<xsl:value-of select="5.25 * Min((string-length(.)+2),200)"/>
quote:“…我们不自动调整文本值”
<xsl:value-of select="5.25 * Min((string-length(.)+2),200)"/>
在传递到XML之前,先获取字符串长度,然后构造ss:Width=“length”。自动拟合不适用于带有字符串的单元格。
<xsl:value-of select="5.25 * Min((string-length(.)+2),200)"/>
尝试用以下代码替换示例中的列行:
<xsl:for-each select="/*/*[1]/*">
<Column>
<xsl:variable name="columnNum" select="position()"/>
<xsl:for-each select="/*/*/*[position()=$columnNum]">
<xsl:sort select="concat(string-length(string-length(.)),string-length(.))" order="descending"/>
<xsl:if test="position()=1">
<xsl:if test="string-length(.) < 201">
<xsl:attribute name="ss:Width">
<xsl:value-of select="5.25 * (string-length(.)+2)"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="string-length(.) > 200">
<xsl:attribute name="ss:Width">
<xsl:value-of select="1000"/>
</xsl:attribute>
</xsl:if>
</xsl:if>
<xsl:if test = "local-name() = 'Sorteer'">
<xsl:attribute name="ss:Width">
<xsl:value-of select="0"/>
</xsl:attribute>
</xsl:if>
</xsl:for-each>
</Column>
</xsl:for-each>
<xsl:value-of select="5.25 * Min((string-length(.)+2),200)"/>
说明:它根据字符串长度进行排序(首先是最长的字符串),取排序字符串的第一行,取该字符串的长度*5.25,您将有一个合理的自动拟合
<xsl:value-of select="5.25 * Min((string-length(.)+2),200)"/>
分拣线:
<xsl:sort select="concat(string-length(string-length(.)),string-length(.))" order="descending"/>
<xsl:value-of select="5.25 * Min((string-length(.)+2),200)"/>
说明:如果你只是按长度排序,比如
<xsl:sort select="string-length(.)" order="descending"/>
<xsl:value-of select="5.25 * Min((string-length(.)+2),200)"/>
因为长度是作为字符串处理的,所以2在10之后,这是您不希望看到的。所以您应该左键填充长度,以便正确排序(因为002在010之前)。但是,由于找不到填充函数,我通过将长度的长度与长度合并来解决它。长度为100的字符串将转换为3100(第一位数字是长度的长度),您将看到解决方案将始终正确排序字符串。例如:2将是“12”,10将是“210”,因此该字符串将被正确排序。只有当长度大于9时才会导致问题,但长度100000000的字符串不能由Excel处理
<xsl:value-of select="5.25 * Min((string-length(.)+2),200)"/>
解释
<xsl:if test="string-length(.) < 201">
<xsl:attribute name="ss:Width">
<xsl:value-of select="5.25 * (string-length(.)+2)"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="string-length(.) > 200">
<xsl:attribute name="ss:Width">
<xsl:value-of select="1000"/>
</xsl:attribute>
</xsl:if>
<xsl:value-of select="5.25 * Min((string-length(.)+2),200)"/>
我想将字符串的长度最大化到200左右,但我无法让Min函数工作,比如
<xsl:value-of select="5.25 * Min((string-length(.)+2),200)"/>
所以我不得不用肮脏的方式
<xsl:value-of select="5.25 * Min((string-length(.)+2),200)"/>
我希望你现在可以自动调整!我知道这篇文章很旧,但是如果有人仍然使用openXml,我会用我编写的解决方案来更新它。它适用于大文件和小文件
<xsl:value-of select="5.25 * Min((string-length(.)+2),200)"/>
该算法是在vb中实现的,它需要一个字符串的数组列表(可以根据需要更改)来实现excel数组
<xsl:value-of select="5.25 * Min((string-length(.)+2),200)"/>
我使用Windows窗体查找渲染文本的宽度,并使用链接仅选择最大的单元格(以提高大文件的效率)
<xsl:value-of select="5.25 * Min((string-length(.)+2),200)"/>
在那里:
<xsl:value-of select="5.25 * Min((string-length(.)+2),200)"/>
Dim colsTmp as ArrayList '(of Arraylist(of String))
Dim cols as Arraylist '(of Integer) Max size of cols
'Whe populate the Arraylist
Dim width As Integer
'For each column
For i As Integer = 0 To colsTmp.Count - 1
'Whe sort cells by the length of their String
colsTmp(i) = (From f In CType(colsTmp(i), String()) Order By f.Length).ToArray
Dim deb As Integer = 0
'If they are more than a 100 cells whe only take the biggest 10%
If colsTmp(i).length > 100 Then
deb = colsTmp(i).length * 0.9
End If
'For each cell taken
For j As Integer = deb To colsTmp(i).length - 1
'Whe messure the lenght with the good font and size
width = Windows.Forms.TextRenderer.MeasureText(colsTmp(i)(j), font).Width
'Whe convert it to "excel lenght"
width = (width / 1.42) + 10
'Whe update the max Width
If width > cols(i) Then cols(i) = width
Next
Next
有人想知道为什么Excel可以很好地自动调整文本列。知道为什么ss:Width与Excel本身使用的单位没有任何关系吗?例如,我指定ss:Width:200,它在Excel中显示为37.43的列宽。它也不是像素,那么ss:Width单位是什么?