You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Exceed DocX生成Word项目符号列表无输出,寻求技术解决方案

问题分析与解决

你的代码无法显示项目符号列表,核心原因是使用DocX库时,仅通过AddList和AddListItem的方式没有正确将列表项段落与文档内容流关联起来。

修正后的代码(推荐写法)

直接为每个答案段落设置项目符号属性,逻辑更直观可靠:

var idxQuestion = 1;
foreach (var question in category.Questions)
{
    // 插入问题段落
    var questionPara = document.InsertParagraph($"{idxSection}.{idxQuestion} " + question.Body);
    questionPara.Bold();
    questionPara.FontSize(15);
    questionPara.KeepLinesTogether();
    questionPara.KeepWithNextParagraph();

    // 为每个答案创建带项目符号的段落
    foreach (var answer in question.Answers)
    {
        var answerPara = document.InsertParagraph(answer.Body);
        answerPara.ListItemType = ListItemType.Bulleted;
        answerPara.ListLevel = 0;
    }

    // 插入空行分隔
    document.InsertParagraph("");

    idxQuestion++;
}

基于AddList的修正写法

如果坚持使用列表对象的方式,需修正列表初始化的参数,确保列表正常工作:

var idxQuestion = 1;
foreach (var question in category.Questions)
{
    // 插入问题段落
    var questionPara = document.InsertParagraph($"{idxSection}.{idxQuestion} " + question.Body);
    questionPara.Bold();
    questionPara.FontSize(15);
    questionPara.KeepLinesTogether();
    questionPara.KeepWithNextParagraph();

    // 创建项目符号列表(移除多余的空字符串参数)
    var list = document.AddList(ListItemType.Bulleted, 0);

    // 填充列表项
    foreach (var answer in question.Answers)
    {
        document.AddListItem(list, answer.Body);
    }

    // 插入空行分隔
    document.InsertParagraph("");

    idxQuestion++;
}

内容的提问来源于stack exchange,提问作者Dio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 06:13:09