为有中文需求的客户提供多渠道中文技术支持.

Mon Sep 19, 2022 2:50 am

Code: Select all
 
//设置列宽
        for (int i = 0; i < table.getRows().getCount(); i++) {

            table.get(i,0).setCellWidth(80f,CellWidthType.Point);
            table.get(i,1).setCellWidth(160f,CellWidthType.Point);
        }

        //固定列宽
        table.autoFit(AutoFitBehaviorType.Fixed_Column_Widths);


80f 代表什么意思,当字段很多的时候,如何分配大小来填充word界面,现在我的表格有13个字段,想要设置固定列宽,发现很难调整

whgao116
 
Posts: 14
Joined: Mon Sep 19, 2022 2:42 am

Mon Sep 19, 2022 6:36 am

您好,

感谢您的留言。
80f是是以point为单位的值,您可以使用下面“代码1”将厘米单位转换为point单位后,再对列宽设置大小,这样您对列宽的值有一个更清楚的认知。
另外,当表格的列较多时,我推荐您设置列宽的时候使用百分比的方式。如“代码2”.
如果您还有任何问题,欢迎随时联系。

代码1:
Code: Select all
 //单位换算
        PdfUnitConvertor unitConvertor = new PdfUnitConvertor();
        //将厘米单位转为point单位(将6厘米换算为单位为point的值)
        float valuePoint = unitConvertor.convertUnits(6, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);

        //设置列宽
        for (int i = 0; i < table.getRows().getCount(); i++) {

            table.get(i,0).setCellWidth(valuePoint,CellWidthType.Point);
        }


代码2:
Code: Select all
 //设置每列的宽度占表格总宽度的百分比
        for (int i = 0; i < table.getRows().getCount(); i++) {
            //设置第一列占表格总宽度的25%
            table.get(i,0).setCellWidth(25,CellWidthType.Percentage);
            table.get(i,1).setCellWidth(25,CellWidthType.Percentage);
            table.get(i,2).setCellWidth(30,CellWidthType.Percentage);
            table.get(i,3).setCellWidth(20,CellWidthType.Percentage);
        }


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Mon Sep 19, 2022 6:37 am

我发现固定列宽不能生效的原因是,我设置了word页大小
Code: Select all
//设置页边距
        section.getPageSetup().getMargins().setTop(90);
        section.getPageSetup().getMargins().setBottom(90);
        section.getPageSetup().getMargins().setLeft(70);
        section.getPageSetup().getMargins().setRight(70);
        //设置页面方向为横向
        section.getPageSetup().setOrientation(PageOrientation.Landscape);
        //设置纸张大小
        section.getPageSetup().setPageSize(PageSize.A4);

但是我不知道怎么去固定word中表格的大小(即,表格属性,指定宽度),导致表格不能填充页面。
(我尝试手动指定表格的宽度后,对表格中列的设置就达到了预期的效果)
Last edited by whgao116 on Mon Sep 19, 2022 7:04 am, edited 2 times in total.

whgao116
 
Posts: 14
Joined: Mon Sep 19, 2022 2:42 am

Mon Sep 19, 2022 6:47 am

Abel.He wrote:您好,

感谢您的留言。
80f是是以point为单位的值,您可以使用下面“代码1”将厘米单位转换为point单位后,再对列宽设置大小,这样您对列宽的值有一个更清楚的认知。
另外,当表格的列较多时,我推荐您设置列宽的时候使用百分比的方式。如“代码2”.
如果您还有任何问题,欢迎随时联系。

代码1:
Code: Select all
 //单位换算
        PdfUnitConvertor unitConvertor = new PdfUnitConvertor();
        //将厘米单位转为point单位(将6厘米换算为单位为point的值)
        float valuePoint = unitConvertor.convertUnits(6, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);

        //设置列宽
        for (int i = 0; i < table.getRows().getCount(); i++) {

            table.get(i,0).setCellWidth(valuePoint,CellWidthType.Point);
        }


代码2:
Code: Select all
 //设置每列的宽度占表格总宽度的百分比
        for (int i = 0; i < table.getRows().getCount(); i++) {
            //设置第一列占表格总宽度的25%
            table.get(i,0).setCellWidth(25,CellWidthType.Percentage);
            table.get(i,1).setCellWidth(25,CellWidthType.Percentage);
            table.get(i,2).setCellWidth(30,CellWidthType.Percentage);
            table.get(i,3).setCellWidth(20,CellWidthType.Percentage);
        }


Sincerely
Abel
E-iceblue support team



1 和 2 的方式我都尝试了。仍然无法填充word,问题的关键是表格的总宽度怎么设置?这是我的代码
Code: Select all

//         设置列宽
        for (int i = 0; i < table.getRows().getCount(); i++) {
            table.get(i, 0).setCellWidth(5, CellWidthType.Percentage);
            table.get(i, 1).setCellWidth(10, CellWidthType.Percentage);
            table.get(i, 2).setCellWidth(15, CellWidthType.Percentage);
            table.get(i, 3).setCellWidth(10, CellWidthType.Percentage);
            table.get(i, 4).setCellWidth(10, CellWidthType.Percentage);
            table.get(i, 5).setCellWidth(5, CellWidthType.Percentage);
            table.get(i, 6).setCellWidth(5, CellWidthType.Percentage);
            table.get(i, 7).setCellWidth(5, CellWidthType.Percentage);
            table.get(i, 8).setCellWidth(5, CellWidthType.Percentage);
            table.get(i, 9).setCellWidth(5, CellWidthType.Percentage);
            table.get(i, 10).setCellWidth(5, CellWidthType.Percentage);
            table.get(i, 11).setCellWidth(5, CellWidthType.Percentage);
            table.get(i, 12).setCellWidth(5, CellWidthType.Percentage);
            table.get(i, 13).setCellWidth(5, CellWidthType.Percentage);
            table.get(i, 14).setCellWidth(5, CellWidthType.Percentage);
        }
        //固定列宽
        table.autoFit(AutoFitBehaviorType.Fixed_Column_Widths);


        //设置表格列宽适应窗体
//        table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Window);

        //列宽自动适应内容
//        table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents);

whgao116
 
Posts: 14
Joined: Mon Sep 19, 2022 2:42 am

Mon Sep 19, 2022 8:59 am

您好,

感谢您的反馈。
根据您提供的代码和描述,您的问题可能是您仅仅是创建了表格,而没有将表格将加入到文档中。另外,设置表格宽度的代码为下面的”代码1“,为了让表格清晰可见,你还需要设置表格的border样式以及行高。我把我测试的完整代码附录在”代码2“中。该代码的结果文档截图如下:
如果这些代码不能帮助到您,请提供您完整的代码,如果有输入文档的话,也请提供来帮助我们做进一步调查。

代码1:
Code: Select all
 //设置表格宽度
        table.setPreferredWidth(new PreferredWidth(WidthType.Percentage,(short) 100));


代码2:完整代码
Code: Select all
import com.spire.doc.*;
import com.spire.doc.documents.BorderStyle;
import com.spire.pdf.graphics.PdfGraphicsUnit;
import com.spire.pdf.graphics.PdfUnitConvertor;

public class demo {
    public static void main(String[] args) {

        String outputFile="output/setColumnWidth.docx";

        //创建文档
        Document document = new Document();
        Section section = document.addSection();
        Table table =  new Table(document);
        //创建一个3行5列的表格
        table.resetCells(3,15);
        //设置表格border样式
        table.getTableFormat().getBorders().setBorderType(BorderStyle.Single);

        for (int i = 0; i < table.getRows().getCount(); i++) {
            //设置行高
            table.getRows().get(i).setHeight(50f);
            table.get(i, 0).setCellWidth(5, CellWidthType.Percentage);
            table.get(i, 1).setCellWidth(10, CellWidthType.Percentage);
            table.get(i, 2).setCellWidth(15, CellWidthType.Percentage);
            table.get(i, 3).setCellWidth(10, CellWidthType.Percentage);
            table.get(i, 4).setCellWidth(10, CellWidthType.Percentage);
            table.get(i, 5).setCellWidth(5, CellWidthType.Percentage);
            table.get(i, 6).setCellWidth(5, CellWidthType.Percentage);
            table.get(i, 7).setCellWidth(5, CellWidthType.Percentage);
            table.get(i, 8).setCellWidth(5, CellWidthType.Percentage);
            table.get(i, 9).setCellWidth(5, CellWidthType.Percentage);
            table.get(i, 10).setCellWidth(5, CellWidthType.Percentage);
            table.get(i, 11).setCellWidth(5, CellWidthType.Percentage);
            table.get(i, 12).setCellWidth(5, CellWidthType.Percentage);
            table.get(i, 13).setCellWidth(5, CellWidthType.Percentage);
            table.get(i, 14).setCellWidth(5, CellWidthType.Percentage);
        }

        //将表格添加到文档中
        section.getTables().add(table);
        table.autoFit(AutoFitBehaviorType.Fixed_Column_Widths);
        //设置表格宽度
        table.setPreferredWidth(new PreferredWidth(WidthType.Percentage,(short) 100));
        //save to file
        document.saveToFile(outputFile, FileFormat.Docx);
    }
}



Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Mon Sep 19, 2022 9:16 am

非常感谢您的回答,非常棒,解决了我的问题 :D :D

whgao116
 
Posts: 14
Joined: Mon Sep 19, 2022 2:42 am

Mon Sep 19, 2022 9:38 am

Hello,

感谢您的反馈。
如果您还有任何问题,欢迎随时联系。

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Mon Sep 19, 2022 9:43 am

还有个问题需要请教一下:根据文档,设置的禁止跨页断行,没有生效。代码如下
Code: Select all
//将表格添加到文档中
        section.getTables().add(table);
        //设置表格是否跨页断行
        table.getTableFormat().isBreakAcrossPages(false);
        // 固定列宽
        table.autoFit(AutoFitBehaviorType.Fixed_Column_Widths);
        //设置表格宽度
        table.setPreferredWidth(new PreferredWidth(WidthType.Percentage, (short) 100));

whgao116
 
Posts: 14
Joined: Mon Sep 19, 2022 2:42 am

Tue Sep 20, 2022 9:49 am

您好,

我使用的是最新版的Spire.Doc for Java 10.9.0测试您的场景,没有重现到您的问题。如果您没有使用最新版的Spire.Doc,请更新到最新版并测试。如果此问题还是存在。为了帮助我们重现您的问题以及为您提供解决方案,请提供以下信息:
1) 您测试的完整代码。
2) 如果有输入文档的话,请提供。
3) 您的jdk版本,如oracle jdk1.8.
4) 您的测试环境,如win10-64bit。

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Tue Sep 20, 2022 10:28 am

感谢 :D
通过更换版本为10.9.0 ,问题解决了,是我版本太低导致的。
这是我用的版本
Code: Select all
 <dependency>
            <groupId>e-iceblue</groupId>
            <artifactId>spire.doc</artifactId>
            <version>4.11.3</version>
        </dependency>

:idea: 小建议:功能说明支持的最低版本号

whgao116
 
Posts: 14
Joined: Mon Sep 19, 2022 2:42 am

Wed Sep 21, 2022 3:20 am

现在功能已经满足了,但是现在存在性能问题(版本10.9.0 jdk 1.8.0)
经测试,生成的word 中 表格 数据 1条 大概400ms左右 、生成的word 中 表格 数据 67条 大概6s左右
部分测试代码
Code: Select all
      System.out.println(DateUtil.currentSeconds()+"---2");
        // 添加空白行
        section.addParagraph();
        section.addParagraph();
        // 添加文字
        Paragraph paragraph2 = section.addParagraph();
        paragraph2.appendText("哈哈   --  嗯嗯");
        ParagraphStyle style2 = new ParagraphStyle(document);
        style2.setName("bottomStyle");
        style2.getCharacterFormat().setFontName("宋体");
        style2.getCharacterFormat().setBold(true);
        document.getStyles().add(style2);
        paragraph1.applyStyle("bottomStyle");
        //保存文档
        document.saveToFile(fwFileTemp, FileFormat.Docx_2013);
        System.out.println(DateUtil.currentSeconds()+"---3");


这是测试代码的日志
Code: Select all
1663730211---1
1663730211---2
1663730216---3


可以看到时间都花费在生成文件上了

whgao116
 
Posts: 14
Joined: Mon Sep 19, 2022 2:42 am

Wed Sep 21, 2022 6:11 am

您好,

感谢您的反馈。
对于版本升级带来的新功能以及功能优化,您可以参考我们发布的新闻,具体访问地址如下面链接。
另外,对于您新提出的问题,我们的同事会在您新开的帖子做出回复。
如果您还有任何问题,欢迎随时联系。
https://www.e-iceblue.cn/spire-doc-for-java-news.html

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Thu Sep 22, 2022 7:36 am

您的业务能力很强,回答准确且快速, :D

whgao116
 
Posts: 14
Joined: Mon Sep 19, 2022 2:42 am

Thu Sep 22, 2022 8:02 am

您好,

感谢您的认可,如果您有任何问题。欢迎随时联系。 :P

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Fri Sep 01, 2023 2:10 am

设置固定列宽后,表格中内容显示不全,请问如何根据内容设置行高自适应呢

wangjiashang
 
Posts: 1
Joined: Fri Sep 01, 2023 2:07 am

Return to 中文技术支持