table-cell布局

display:table-cell属性简介

display:table-cell;会使元素表现的类似一个表格中的单元格td,利用这个特性可以实现文字的垂直居中效果。同时它也会破坏一些CSS属性,使用table-cell时最好不要与float以及position: absolute一起使用,设置了table-cell的元素对高度和宽度高度敏感,对margin值无反应,可以响应padding的设置,表现几乎类似一个td元素。

小结:
1. 不要与float:left; position:absolute; 一起使用
2. 可以实现大小不固定元素的垂直居中
3. margin设置无效,响应padding设置
4. 对高度和宽度高度敏感
5. 不要对display:table-cell使用百分比设置宽度和高度

下面介绍几个table-cell的应用

1. 高度不固定内容水平垂直居中
  • 文字水平垂直居中
<style>
.table-wrap{
        display: table-cell;
        height: 200px;
        width: 100px;
        padding: 20px;
        vertical-align: middle;
        text-align: center;
        border: 1px solid red;
        }
</style>
<div  class="table-wrap">
    我是一大推文字,我想要垂直居中,这是省略这是省略这是省略这是省略
</div>

avatar
关键在于设置了display:cell;后,vertical-align:middle使文字内容垂直居中

  • 图片水平垂直居中
<style>
.outer{display: table;width: 100%;}
.table-wrap{
    display: table-cell;
    height: 200px;          
    padding: 20px;
    vertical-align: middle;
    text-align: center;
    border: 1px solid red;
    }
</style>
<div class="outer">
    <div  class="table-wrap">
        <img src="http://jsfiddle.net/img/logo@2x.png" alt="logo" />
    </div>  
</div>  

avatar
中间的图片会随着外层容器的大小而自动水平垂直居中,其实原理和文字水平垂直居中一模一样

2. 元素两端垂直对齐
<style>
    *{margin: 0;padding: 0;}
    .outer{display: table;width: 90%;margin: 10px  auto;
        padding: 10px; border: 1px solid green;}
    .table-left,.table-right{
        display: table-cell;            
        width: 20%;
        border: 1px solid red;
    }
    .table-center{
        height: 100px;
        background-color: green;
    }
</style>
<div class="outer">
    <div class="table-left">
        <p>我是左边</p>
    </div>
    <div  class="table-center">
        我是中间
    </div>
    <div class="table-right">
        <p>我是右边</p>
    </div>
</div>

avatar

其中table-center的顶部没有与左右两侧对齐,原因是teble-left中的<p>有一个margin-top, 而表格布局中默认是文字顶部对齐的,所以table-center会向下移动到首行文字基线对齐,解决办法是为table-center添加与左右两侧中margin-top较大者等值的padding-top即可。

3. 等高布局
<style>
        *{margin: 0;padding: 0;}
        .outer{display: table;width: 90%;margin: 10px  auto;
            padding: 10px; border: 1px solid green;}
        .table-left{
            display: table-cell;            
            width: 150px;
            padding-top: 20px;
            border: 1px solid red;
            vertical-align: top;
            text-align: center;
        }

        .table-right{           
            border: 1px solid yellow;
            background-color: green;
            margin-left: 20px;
            padding: 10px;
        }
    </style>
    <div class="outer">
        <div class="table-left">
            <img src="http://jsfiddle.net/img/logo@2x.png" alt="logo" />
        </div>
        <div class="table-right">
            <span>这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字。。。。。</span>
        </div>
    </div>

avatar
拉伸宽度,会发现左右两侧始终保持高度一致。

4. 均分列表布局
<style>
    *{margin: 0;padding: 0;}
    .table-wrap{
        display: table;
        width: 80%;
        margin: 20px auto;
        border: 1px solid red;
        padding: 20px;
    }
    .table-wrap li{
        display: table-cell;
        padding: 20px;
        border: 1px solid green;
        border-right: none;
        text-align: center;
        vertical-align: middle;
    }
    .table-wrap li:last-child{
        border-right: 1px solid green;
    }
</style>
<ul class="table-wrap">
    <li>001</li>
    <li>002</li>
    <li>003</li>
    <li>004</li>
    <li>005</li>
</ul>

avatar
以前进行类似的布局我都会使用display:inline-block;的方式,需要在ul上设置font-size: 0;来清除每一个小块之间的间隙,还需要为每一个项写一个百分比宽度。使用table-cell后,不需要写百分比宽度,也不需要清除小块间的间隙。而且添加额外项时,会自动平均分配宽度。

5. 两栏自适应布局

即左侧宽度固定,右侧自动分配剩余空间

    <style>
        * {
            box-sizing: border-box;
        }
        .content {
            display: table;
            border: 1px solid #06c;
            padding: 15px 5px;
            max-width: 1000px;
            margin: 10px auto;
            min-width: 320px;
            width: 100%;
        }
        .left-box{
            float: left;            
            background-color: green;
            margin-right: 10px;
            padding-top:5px;            
        }
        .right-box{
            display: table-cell;
            border: 1px solid red;
            padding: 10px;
            vertical-align: top;
        }
    </style>
    <div class="content">
        <div class="left-box">
             <img src="http://jsfiddle.net/img/logo@2x.png" alt="logo" />
        </div>         
        <div class="right-box">
            <span>这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字这是很多文字。。。。。</span>
        </div>
    </div>

avatar
左侧宽度固定,右侧宽度自适应。布局原理也可以用BFC来解释,左侧和右侧都是BFC块,BFC块是不会和float块发生重叠的。

6. 诡异的百分比宽高

使用table-cell有一个让人很抓狂的地方,那就是他的宽高是不能使用百分比设置的,具体的原因我一直没有找到,只能理解为这是这个属性的特性。希望有知道的老铁告知一声。
下面说一说关于百分比宽高的结论:
1. 高度设置百分比无效,只与内容的实际高度有关
2. 宽度设置百分比分两种情况,
* 一种是父容器设置display:table;,这种情况下分两种情况
1. 没有其他元素table-cell的容器会获得它前面的元素在水平方向分配完的全部宽度。
2. 在其前面有其他元素时,其宽度可以按照display:inline-block的元素来计算,也就是宽度会小于等于其百分比计算值的宽度的
* 在父容器未设置table时width的百分比值可以理解为内容区宽度加上border-box左右两边到父容器边框的距离,也就是width的百分比值越小,其所占据的宽度越大
如果你对这个问题比较感兴趣,可以看看这个问答
真的是匪夷所思啊。

小结:所有的display:table-cell;布局中,vertical-align属性对于文字对齐都是很关键的。

内容过长自动换行, <xsl:template match=“TableReport”> <fo:block > <fo:table text-align="left" border-collapse="collapse" letter-spacing="1px" width="100%" border-width="1pt" font-size="2.6mm"> <fo:table-column border-width="0pt" column-width="0.7cm"/> <fo:table-column border-width="0pt" column-width="0.8cm"/> <fo:table-column border-width="0pt" column-width="2.5cm"/> <fo:table-column border-width="0pt" column-width="1.5cm"/> <fo:table-column border-width="0pt" column-width="1cm"/> <fo:table-column border-width="0pt" column-width="1.6cm"/> <fo:table-column border-width="0pt" column-width="1.6cm"/> <fo:table-column border-width="0pt" column-width="3cm"/> <fo:table-column border-width="0pt" column-width="4.7cm"/> <fo:table-column border-width="0pt" column-width="0.8cm"/> <fo:table-column border-width="0pt" column-width="1.1cm"/> <fo:table-header> <fo:table-row height="5mm"> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block text-align="center">序号</fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block text-align="center">组织</fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block text-align="center">申请单号</fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block text-align="center">类型</fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block text-align="center">状态</fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-style"> <fo:block text-align="center">工号</fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-style"> <fo:block text-align="center">姓名</fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-style"> <fo:block text-align="center">物料编码</fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-style"> <fo:block text-align="center">物料描述</fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-style"> <fo:block text-align="center">销数</fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-style"> <fo:block text-align="center">原数量</fo:block> </fo:table-cell> </fo:table-row> </fo:table-header> <!--表明细--> <fo:table-body> <xsl:apply-templates select="TableRow"/> </fo:table-body> </fo:table> </fo:block> <fo:block id="end"/> </xsl:template> <xsl:template match="TableRow"> <fo:table-row height="5mm"> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block><xsl:value-of select="no"/></fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block><xsl:value-of select="orgCode"/></fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style" font-size="2.2mm"> <fo:block><xsl:value-of select="requestNo"/></fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block><xsl:value-of select="noteType"/></fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block><xsl:value-of select="state"/></fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-style"> <fo:block><xsl:value-of select="empNo"/></fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-style" font-size="2.2mm"> <fo:block><xsl:value-of select="empName"/></fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-style" font-size="2.2mm"> <fo:block><xsl:value-of select="itemCode"/></fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-style" font-size="2.2mm"> <fo:block wrap-option="wrap" language="zh"><xsl:value-of select="itemDesc"/></fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-style" font-size="2.2mm"> <fo:block wrap-option="wrap" language="zh"><xsl:value-of select="qty"/></fo:block> </fo:table-cell> <fo:table-cell xsl:use-attribute-sets="border-cell-style"> <fo:block><xsl:value-of select="holdQty"/></fo:block> </fo:table-cell> </fo:table-row> </xsl:template>
最新发布
08-02
<think>我们有一个XSL-FO样式表片段,用于生成PDF表格。问题描述是“内容过长自动换行”,我们需要确保当单元格中的文本内容过长时能够自动换行,特别是对于物料描述(itemDesc)这样的字段。 在提供的代码中,已经对部分单元格使用了`wrap-option="wrap"`,例如: <fo:table-cell xsl:use-attribute-sets="border-cell-style" font-size="2.2mm"> <fo:block wrap-option="wrap" language="zh"><xsl:value-of select="itemDesc"/></fo:block> </fo:table-cell> 但是,可能仍然存在换行问题。我们需要检查以下几点: 1. 表格列宽是否固定?是的,代码中为每一列指定了固定的宽度(column-width)。 2. 是否设置了允许换行?在需要换行的块(fo:block)上设置了`wrap-option="wrap"`。 3. 是否有足够的空间?由于列宽是固定的,如果内容过长而列宽不够,换行是必然的,但我们要确保换行功能正常。 可能的问题: - 列宽设置过小,而内容太长,导致换行后仍然超出。 - 没有设置块(block)的宽度,导致换行计算不准确。 解决方案: 1. 确保在需要换行的单元格中,`fo:block`元素设置`wrap-option="wrap"`,并且可以添加`overflow-wrap="break-word"`来允许在单词内断行(如果需要)。 2. 对于包含长文本的列,可以设置`display-align="center"`来垂直居中(可选)。 3. 考虑设置块(block)的宽度为100%,这样块会填充整个单元格,换行会基于单元格的宽度。 修改建议: 对于可能包含长文本的列(例如:物料描述、物料编码、申请单号等),我们可以在block上设置: - wrap-option="wrap" - overflow-wrap="break-word" (对于非空格分隔的长字符串,如URL或长单词) - 设置width="100%" 或者使用相对宽度 但是,注意:在表格单元格中,块默认会占据整个单元格的宽度,所以设置width="100%"可能不是必须的。但为了确保,我们可以设置。 另外,我们发现有些列没有设置换行属性,例如申请单号(requestNo)列,它可能也需要换行。因此,我们为所有可能内容过长的列都加上换行属性。 具体修改如下: 1. 在样式表中,为需要自动换行的列(申请单号、物料编码、物料描述、销数等)的block元素添加或修改属性: wrap-option="wrap" 和 overflow-wrap="break-word" 2. 考虑设置块的高度为自动(auto),并允许高度扩展。但是,注意表格行的高度是固定的(height="5mm"),这可能导致换行后的内容被截断。因此,我们需要调整行高为自动(或者至少为最小高度),并允许行高根据内容调整。 由于表格行高固定为5mm,当内容换行后,行高不会自动增加,导致内容重叠或被截断。所以,我们需要改变行高设置,使其可以自动调整。 修改行高为自动: - 移除表格行(fo:table-row)的height属性,或者设置为auto。 - 设置表格行的高度为自动,并设置最小高度(min-height)以保证至少有一定高度。 但是,在XSL-FO中,表格行的高度可以通过以下方式控制: - 使用属性 height="auto" 或直接不设置(默认为auto) - 在单元格中,设置块(block)的属性,如line-height、space-before等来调整行高。 然而,由于我们生成的是PDF,自动调整行高可能会导致表格跨页时出现问题,所以需要谨慎。 另一种做法:保持行高固定,但通过缩小字体或调整内容来适应。但这样无法处理超长内容。 因此,我们选择允许行高自动调整,并设置合适的行高和间距。 修改步骤: 步骤1:修改表格行的高度为自动(auto),并设置最小高度(min-height)为5mm(或其他合适值)。 步骤2:为单元格中的块设置适当的行高(line-height)和上下间距(space-before/after),以保证可读性。 步骤3:对于长文本,确保换行设置生效。 但是,在XSL-FO中,表格行没有min-height属性。我们可以通过设置单元格的高度为auto,然后让单元格内容自动调整高度。 因此,我们移除表格行的高度设置,并设置单元格的块(block)属性,使其可以自动换行并扩展高度。 修改后的模板: 1. 修改表头行(fo:table-header中的行)和表格体行(fo:table-body中的行)的高度为自动。 2. 在块(block)上设置换行属性,并设置适当的行高。 由于原代码中使用了属性集(attribute-sets)来设置单元格样式,我们可能需要修改属性集以支持自动高度。 但是,由于我们无法看到属性集(border-cell-left-style等)的定义,我们假设它们主要设置边框样式。因此,我们直接在单元格或块上设置与高度和换行相关的属性。 修改后的代码示例: 在TableRow模板中,移除行高设置,并在每个单元格的块上设置换行和行高属性。 由于原代码中已经为部分列设置了换行,现在我们为所有需要换行的列都设置,并调整行高。 具体修改如下: 1. 修改表头行和表格体行的行高为自动(移除height属性或设置为auto)。 2. 在表格体行的每个单元格的块中,设置: wrap-option="wrap" overflow-wrap="break-word" (可选,用于长单词) line-height="4mm" (设置行高,保证行间距) space-after.optimum="1mm" (设置段后间距,可选) space-before.optimum="1mm" (设置段前间距,可选) 3. 对于表头行,我们也可以同样设置块属性,但表头行通常不需要换行(因为标题较短),所以可以保持原样。 修改后的模板: 注意:由于列宽固定,当内容过长时,换行后单元格高度会增加,从而整个行的高度也会增加。 修改后的代码: ```xml <xsl:template match="TableReport"> <!-- 省略其他代码 --> <fo:table-header> <fo:table-row> <!-- 移除height属性 --> <!-- 表头单元格保持不变 --> </fo:table-row> </fo:table-header> <fo:table-body> <xsl:apply-templates select="TableRow"/> </fo:table-body> <!-- 省略其他代码 --> </xsl:template> <xsl:template match="TableRow"> <fo:table-row> <!-- 移除height属性,让行高自动调整 --> <!-- 序号列 --> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block line-height="4mm" space-before.optimum="1mm" space-after.optimum="1mm"> <xsl:value-of select="no"/> </fo:block> </fo:table-cell> <!-- 组织列 --> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block line-height="4mm" space-before.optimum="1mm" space-after.optimum="1mm"> <xsl:value-of select="orgCode"/> </fo:block> </fo:table-cell> <!-- 申请单号列:需要换行 --> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style" font-size="2.2mm"> <fo:block wrap-option="wrap" overflow-wrap="break-word" line-height="4mm" space-before.optimum="1mm" space-after.optimum="1mm"> <xsl:value-of select="requestNo"/> </fo:block> </fo:table-cell> <!-- 类型列 --> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block line-height="4mm" space-before.optimum="1mm" space-after.optimum="1mm"> <xsl:value-of select="noteType"/> </fo:block> </fo:table-cell> <!-- 状态列 --> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block line-height="4mm" space-before.optimum="1mm" space-after.optimum="1mm"> <xsl:value-of select="state"/> </fo:block> </fo:table-cell> <!-- 工号列 --> <fo:table-cell xsl:use-attribute-sets="border-cell-style"> <fo:block line-height="4mm" space-before.optimum="1mm" space-after.optimum="1mm"> <xsl:value-of select="empNo"/> </fo:block> </fo:table-cell> <!-- 姓名列:需要换行 --> <fo:table-cell xsl:use-attribute-sets="border-cell-style" font-size="2.2mm"> <fo:block wrap-option="wrap" overflow-wrap="break-word" line-height="4mm" space-before.optimum="1mm" space-after.optimum="1mm"> <xsl:value-of select="empName"/> </fo:block> </fo:table-cell> <!-- 物料编码列:需要换行 --> <fo:table-cell xsl:use-attribute-sets="border-cell-style" font-size="2.2mm"> <fo:block wrap-option="wrap" overflow-wrap="break-word" line-height="4mm" space-before.optimum="1mm" space-after.optimum="1mm"> <xsl:value-of select="itemCode"/> </fo:block> </fo:table-cell> <!-- 物料描述列:需要换行,且内容通常较长 --> <fo:table-cell xsl:use-attribute-sets="border-cell-style" font-size="2.2mm"> <fo:block wrap-option="wrap" overflow-wrap="break-word" line-height="4mm" space-before.optimum="1mm" space-after.optimum="1mm"> <xsl:value-of select="itemDesc"/> </fo:block> </fo:table-cell> <!-- 销数列:需要换行 --> <fo:table-cell xsl:use-attribute-sets="border-cell-style" font-size="2.2mm"> <fo:block wrap-option="wrap" overflow-wrap="break-word" line-height="4mm" space-before.optimum="1mm" space-after.optimum="1mm"> <xsl:value-of select="qty"/> </fo:block> </fo:table-cell> <!-- 原数量列 --> <fo:table-cell xsl:use-attribute-sets="border-cell-style"> <fo:block line-height="4mm" space-before.optimum="1mm" space-after.optimum="1mm"> <xsl:value-of select="holdQty"/> </fo:block> </fo:table-cell> </fo:table-row> </xsl:template> ``` 注意:我们为每个单元格的块(block)都设置了行高(line-height)和段前段后间距(space-before/after),这样可以使行高一致,且有一定间距,提高可读性。对于需要换行的列,我们添加了换行属性。 另外,由于表头行(header)没有修改,如果表头内容过长也需要换行,可以用同样的方式修改表头。 如果表头也需要自动换行,可以修改表头行的块: ```xml <fo:table-header> <fo:table-row> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block wrap-option="wrap" line-height="4mm" space-before.optimum="1mm" space-after.optimum="1mm">序号</fo:block> </fo:table-cell> <!-- 其他表头单元格类似修改 --> </fo:table-row> </fo:table-header> ``` 最后,如果表格跨页,我们可能还需要设置表格的跨页行为,例如: - 设置表格的keep-together.within-page="auto" 或 keep-together.within-column="auto" - 设置表头在每页重复:使用<fo:table-header>和<fo:table-footer>,并设置<fo:table-body>,这样表头会在每页重复。 在原始代码中,已经使用了<fo:table-header>,所以表头会在每页重复。 如果问题仍然存在,请检查属性集(attribute-sets)中是否设置了固定高度,如果有,需要修改属性集。 由于我们看不到属性集的定义,如果属性集中有设置高度相关的属性,请移除或覆盖它们。 总结: - 允许行高自动调整(移除行高固定值) - 为单元格内容设置换行和断词 - 设置适当的行高和间距 这样,当内容过长时,单元格会自动换行并增加行高,从而完整显示内容。 如果问题仍然存在,请检查列宽是否足够,或者考虑调整列宽。 由于列宽是固定的,如果一列中的内容过多,即使换行也可能需要多行显示,此时行高会自动增加,整个表格行的高度也会增加。 如果列宽确实太小,可以考虑调整列宽(column-width)的值。 希望这些修改能解决自动换行问题。</think># XSL-FO 表格内容自动换行解决方案 您的 XSL-FO 表格在内容过长时没有正确自动换行,主要问题是部分列宽度不足,以及缺少必要的换行控制属性。以下是完整的解决方案: ## 问题分析 1. **列宽不足**:部分列(如物料描述)宽度设置过小(4.7cm),无法容纳实际内容 2. **缺少换行控制**:部分单元格没有设置 `wrap-option="wrap"` 属性 3. **行高固定**:所有行固定高度为 5mm,无法适应多行内容 4. **字体大小问题**:部分列字体过小(2.2mm)导致可读性差 ## 完整解决方案 ### 改进的 XSL-FO 表格模板 ```xml <xsl:template match="TableReport"> <!-- 设置表格属性 --> <fo:block> <fo:table text-align="left" border-collapse="collapse" letter-spacing="1px" width="100%" border-width="1pt" font-size="2.6mm" table-layout="fixed"> <!-- 优化列宽设置:增加物料描述列宽度,减少其他非关键列宽度 --> <fo:table-column border-width="0pt" column-width="0.7cm"/> <!-- 序号 --> <fo:table-column border-width="0pt" column-width="0.8cm"/> <!-- 组织 --> <fo:table-column border-width="0pt" column-width="2.0cm"/> <!-- 申请单号 --> <fo:table-column border-width="0pt" column-width="1.2cm"/> <!-- 类型 --> <fo:table-column border-width="0pt" column-width="0.8cm"/> <!-- 状态 --> <fo:table-column border-width="0pt" column-width="1.2cm"/> <!-- 工号 --> <fo:table-column border-width="0pt" column-width="1.4cm"/> <!-- 姓名 --> <fo:table-column border-width="0pt" column-width="2.0cm"/> <!-- 物料编码 --> <fo:table-column border-width="0pt" column-width="6.0cm"/> <!-- 物料描述(增加宽度) --> <fo:table-column border-width="0pt" column-width="0.8cm"/> <!-- 销数 --> <fo:table-column border-width="0pt" column-width="1.1cm"/> <!-- 原数量 --> <!-- 表头 --> <fo:table-header> <fo:table-row height="5mm"> <!-- 表头单元格保持不变 --> </fo:table-row> </fo:table-header> <!-- 表格内容 --> <fo:table-body> <xsl:apply-templates select="TableRow"/> </fo:table-body> </fo:table> </fo:block> <fo:block id="end"/> </xsl:template> <xsl:template match="TableRow"> <!-- 使用自动行高以适应多行内容 --> <fo:table-row> <!-- 序号 --> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block><xsl:value-of select="no"/></fo:block> </fo:table-cell> <!-- 组织 --> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block><xsl:value-of select="orgCode"/></fo:block> </fo:table-cell> <!-- 申请单号:添加自动换行 --> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style" font-size="2.2mm"> <fo:block wrap-option="wrap" hyphenate="true" linefeed-treatment="preserve"> <xsl:value-of select="requestNo"/> </fo:block> </fo:table-cell> <!-- 类型 --> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block><xsl:value-of select="noteType"/></fo:block> </fo:table-cell> <!-- 状态 --> <fo:table-cell xsl:use-attribute-sets="border-cell-left-style"> <fo:block><xsl:value-of select="state"/></fo:block> </fo:table-cell> <!-- 工号 --> <fo:table-cell xsl:use-attribute-sets="border-cell-style"> <fo:block><xsl:value-of select="empNo"/></fo:block> </fo:table-cell> <!-- 姓名:添加自动换行 --> <fo:table-cell xsl:use-attribute-sets="border-cell-style" font-size="2.2mm"> <fo:block wrap-option="wrap" hyphenate="true"> <xsl:value-of select="empName"/> </fo:block> </fo:table-cell> <!-- 物料编码:添加自动换行 --> <fo:table-cell xsl:use-attribute-sets="border-cell-style" font-size="2.2mm"> <fo:block wrap-option="wrap" hyphenate="true"> <xsl:value-of select="itemCode"/> </fo:block> </fo:table-cell> <!-- 物料描述:增强换行控制 --> <fo:table-cell xsl:use-attribute-sets="border-cell-style" font-size="2.2mm"> <fo:block wrap-option="wrap" hyphenate="true" linefeed-treatment="preserve" text-align="left" line-height="3.5mm" space-before.optimum="0.5mm" space-after.optimum="0.5mm"> <xsl:value-of select="itemDesc"/> </fo:block> </fo:table-cell> <!-- 销数:添加自动换行 --> <fo:table-cell xsl:use-attribute-sets="border-cell-style" font-size="2.2mm"> <fo:block wrap-option="wrap" hyphenate="true"> <xsl:value-of select="qty"/> </fo:block> </fo:table-cell> <!-- 原数量 --> <fo:table-cell xsl:use-attribute-sets="border-cell-style"> <fo:block><xsl:value-of select="holdQty"/></fo:block> </fo:table-cell> </fo:table-row> </xsl:template> ``` ### 关键改进说明 1. **列宽优化**: - 将物料描述列宽从 4.7cm 增加到 6.0cm - 减少其他非关键列宽度(申请单号从 2.5cm→2.0cm,类型从 1.5cm→1.2cm) - 使用 `table-layout="fixed"` 确保列宽稳定 2. **自动换行增强**: ```xml <fo:block wrap-option="wrap" hyphenate="true" linefeed-treatment="preserve" line-height="3.5mm"> ``` - `wrap-option="wrap"`:启用自动换行 - `hyphenate="true"`:允许单词内断字 - `linefeed-treatment="preserve"`:保留原始换行符 - `line-height="3.5mm"`:优化行高 3. **行高自适应**: - 移除固定的 `height="5mm"` 属性 - 使用自动行高以适应多行内容 - 添加 `space-before.optimum` 和 `space-after.optimum` 控制垂直间距 4. **字体优化**: - 将关键列字体恢复为默认 2.6mm 提高可读性 - 仅在长文本列使用 2.2mm 字体 ### 添加属性集优化边框样式 在 XSL-FO 文件顶部添加或修改属性集: ```xml <xsl:attribute-set name="border-cell-style"> <xsl:attribute name="border-width">0.5pt</xsl:attribute> <xsl:attribute name="border-style">solid</xsl:attribute> <xsl:attribute name="padding">0.5mm</xsl:attribute> <xsl:attribute name="display-align">center</xsl:attribute> </xsl:attribute-set> <xsl:attribute-set name="border-cell-left-style" use-attribute-sets="border-cell-style"> <xsl:attribute name="border-left-width">1pt</xsl:attribute> </xsl:attribute-set> ``` ### 处理超长内容的备选方案 对于极端超长内容,添加截断功能: ```xml <xsl:template name="truncate-text"> <xsl:param name="text"/> <xsl:param name="max-length" select="50"/> <xsl:choose> <xsl:when test="string-length($text) > $max-length"> <xsl:value-of select="concat(substring($text, 1, $max-length), '...')"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$text"/> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- 在物料描述中使用 --> <fo:block wrap-option="wrap"> <xsl:call-template name="truncate-text"> <xsl:with-param name="text" select="itemDesc"/> <xsl:with-param name="max-length" select="100"/> </xsl:call-template> </fo:block> ``` ### 完整优化后的属性集 ```xml <xsl:attribute-set name="table-cell-base"> <xsl:attribute name="border-width">0.5pt</xsl:attribute> <xsl:attribute name="border-style">solid</xsl:attribute> <xsl:attribute name="border-color">black</xsl:attribute> <xsl:attribute name="padding">0.5mm</xsl:attribute> <xsl:attribute name="display-align">center</xsl:attribute> </xsl:attribute-set> <xsl:attribute-set name="border-cell-style" use-attribute-sets="table-cell-base"> <xsl:attribute name="border-right-width">1pt</xsl:attribute> </xsl:attribute-set> <xsl:attribute-set name="border-cell-left-style" use-attribute-sets="table-cell-base"> <xsl:attribute name="border-left-width">1pt</xsl:attribute> </xsl:attribute-set> <xsl:attribute-set name="wrap-cell"> <xsl:attribute name="wrap-option">wrap</xsl:attribute> <xsl:attribute name="hyphenate">true</xsl:attribute> <xsl:attribute name="linefeed-treatment">preserve</xsl:attribute> <xsl:attribute name="line-height">3.5mm</xsl:attribute> <xsl:attribute name="space-before.optimum">0.5mm</xsl:attribute> <xsl:attribute name="space-after.optimum">0.5mm</xsl:attribute> </xsl:attribute-set> ``` ## 解决方案优势 1. **自适应布局**:列宽优化后能更好地适应不同内容长度 2. **智能换行**:通过组合多个属性实现最佳换行效果 3. **可读性提升**:合理的行高和字体大小设置 4. **灵活扩展**:属性集设计便于全局样式调整 5. **兼容性**:适用于主流 FO 处理器(Apache FOP, RenderX 等) ## 测试建议 1. 使用不同长度的数据测试表格渲染效果 2. 特别测试超长物料描述(>100字符)的表现 3. 验证分页时表格的完整性 4. 检查边框样式和间距是否符合要求 ## 相关问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值