С# NPOI 单词列表计数

С# NPOI Word list numeration

我需要一个如下结构的word文档列表:

我已经想到了这个

这是代码。但是我不明白 ids 是如何工作的,那是什么,这个库根本没有文档。如果您找到了一些解释或可以解释,我将不胜感激

            XWPFDocument document = new XWPFDocument();
            XWPFNumbering numbering = document.CreateNumbering();
            string abstractNumId = "";
            string numId = numbering.AddNum(abstractNumId);
            
            numbering.AddNum(abstractNumId, numId);

            //Create paragragh and set its list level
            XWPFParagraph para1 = document.CreateParagraph();
            XWPFRun run1 = para1.CreateRun();
            run1.SetText("The first paragraph");
            para1.SetNumID(numId, "0");

            para1 = document.CreateParagraph();
            run1 = para1.CreateRun();
            run1.SetText("The first sub-item");
            para1.SetNumID(numId, "1");

            //Create paragragh and set the list level
            XWPFParagraph para2 = document.CreateParagraph();
            XWPFRun run2 = para2.CreateRun();
            run2.SetText("The second paragraph");
            para2.SetNumID(numId, "0");

            para2 = document.CreateParagraph();
            run2 = para2.CreateRun();
            run2.SetText("The second sub-item");
            para2.SetNumID(numId, "1");

            //Create paragragh and apply multi level list
            XWPFParagraph para3 = document.CreateParagraph();
            XWPFRun run3 = para3.CreateRun();
            run3.SetText("The third paragraph");
            para3.SetNumID(numId, "0");
            para3 = document.CreateParagraph();
            run3 = para3.CreateRun();
            run3.SetText("The first sub-item");
            para3.SetNumID(numId, "1");
            para3 = document.CreateParagraph();
            run3 = para3.CreateRun();
            run3.SetText("The second sub-item");
            para3.SetNumID(numId, "1");
            para3 = document.CreateParagraph();
            run3 = para3.CreateRun();
            run3.SetText("The sub-sub-item");
            para3.SetNumID(numId, "2");

您可以创建自己的编号并为每个级别指定格式。

使用 NPOI 2.5.4 测试

XWPFDocument document = new XWPFDocument();
XWPFNumbering numbering = document.CreateNumbering();
        
var ct_abn = new CT_AbstractNum();
var mlt = new CT_MultiLevelType();
mlt.val = ST_MultiLevelType.multilevel;
ct_abn.multiLevelType = mlt;
ct_abn.lvl = new System.Collections.Generic.List<CT_Lvl>
        {
            new CT_Lvl{ ilvl = "0", start = new CT_DecimalNumber(){ val = "1"}, numFmt = new CT_NumFmt(){ val= ST_NumberFormat.@decimal },
            lvlText = new CT_LevelText(){ val ="%1." }, lvlJc=new CT_Jc(){ val = ST_Jc.left },
            pPr = new CT_PPr{ ind = new CT_Ind{ left = "360", hanging = 360 } } },
            new CT_Lvl{ ilvl = "1", start = new CT_DecimalNumber(){ val = "1"}, numFmt = new CT_NumFmt(){ val= ST_NumberFormat.@decimal },
            lvlText = new CT_LevelText(){ val ="%1. %2." }, lvlJc=new CT_Jc(){ val = ST_Jc.left },
            pPr = new CT_PPr{ ind = new CT_Ind{ left = "792", hanging = 432 } } },
            new CT_Lvl{ ilvl = "2", start = new CT_DecimalNumber(){ val = "1"}, numFmt = new CT_NumFmt(){ val= ST_NumberFormat.@decimal },
            lvlText = new CT_LevelText(){ val ="%1. %2. %3." }, lvlJc=new CT_Jc(){ val = ST_Jc.left },
            pPr = new CT_PPr{ ind = new CT_Ind{ left = "1224", hanging = 504 } } },
            new CT_Lvl{ ilvl = "3", start = new CT_DecimalNumber(){ val = "1"}, numFmt = new CT_NumFmt(){ val= ST_NumberFormat.@decimal },
            lvlText = new CT_LevelText(){ val ="%1. %2. %3. %4." }, lvlJc=new CT_Jc(){ val = ST_Jc.left },
            pPr = new CT_PPr{ ind = new CT_Ind{ left = "1728", hanging = 648 } } },
            new CT_Lvl{ ilvl = "4", start = new CT_DecimalNumber(){ val = "1"}, numFmt = new CT_NumFmt(){ val= ST_NumberFormat.@decimal },
            lvlText = new CT_LevelText(){ val ="%1. %2. %3. %4. %5." }, lvlJc=new CT_Jc(){ val = ST_Jc.left },
            pPr = new CT_PPr{ ind = new CT_Ind{ left = "2232", hanging = 792 } } },
            new CT_Lvl{ ilvl = "5", start = new CT_DecimalNumber(){ val = "1"}, numFmt = new CT_NumFmt(){ val= ST_NumberFormat.@decimal },
            lvlText = new CT_LevelText(){ val ="%1. %2. %3. %4. %5. %6." }, lvlJc=new CT_Jc(){ val = ST_Jc.left },
            pPr = new CT_PPr{ ind = new CT_Ind{ left = "2736", hanging = 936 } } },
            new CT_Lvl{ ilvl = "6", start = new CT_DecimalNumber(){ val = "1"}, numFmt = new CT_NumFmt(){ val= ST_NumberFormat.@decimal },
            lvlText = new CT_LevelText(){ val ="%1. %2. %3. %4. %5. %6. %7." }, lvlJc=new CT_Jc(){ val = ST_Jc.left },
            pPr = new CT_PPr{ ind = new CT_Ind{ left = "3240", hanging = 1080 } } },
            new CT_Lvl{ ilvl = "7", start = new CT_DecimalNumber(){ val = "1"}, numFmt = new CT_NumFmt(){ val= ST_NumberFormat.@decimal },
            lvlText = new CT_LevelText(){ val ="%1. %2. %3. %4. %5. %6. %7. %8." }, lvlJc=new CT_Jc(){ val = ST_Jc.left },
            pPr = new CT_PPr{ ind = new CT_Ind{ left = "3744", hanging = 1224 } } },
            new CT_Lvl{ ilvl = "8", start = new CT_DecimalNumber(){ val = "1"}, numFmt = new CT_NumFmt(){ val= ST_NumberFormat.@decimal },
            lvlText = new CT_LevelText(){ val ="%1. %2. %3. %4. %5. %6. %7. %8. %9." }, lvlJc=new CT_Jc(){ val = ST_Jc.left },
            pPr = new CT_PPr{ ind = new CT_Ind{ left = "4320", hanging = 1440 } } },
        };
string abstractNumId = numbering.AddAbstractNum(new XWPFAbstractNum(ct_abn));
string numId = numbering.AddNum(abstractNumId);

//Create paragragh and set its list level
XWPFParagraph para1 = document.CreateParagraph();
XWPFRun run1 = para1.CreateRun();
run1.SetText("The first paragraph");
para1.SetNumID(numId, "0");

para1 = document.CreateParagraph();
run1 = para1.CreateRun();
run1.SetText("The first sub-item");
para1.SetNumID(numId, "1");

//Create paragragh and set the list level
XWPFParagraph para2 = document.CreateParagraph();
XWPFRun run2 = para2.CreateRun();
run2.SetText("The second paragraph");
para2.SetNumID(numId, "0");

para2 = document.CreateParagraph();
run2 = para2.CreateRun();
run2.SetText("The second sub-item");
para2.SetNumID(numId, "1");

//Create paragragh and apply multi level list
XWPFParagraph para3 = document.CreateParagraph();
XWPFRun run3 = para3.CreateRun();
run3.SetText("The third paragraph");
para3.SetNumID(numId, "0");
para3 = document.CreateParagraph();
run3 = para3.CreateRun();
run3.SetText("The first sub-item");
para3.SetNumID(numId, "1");
para3 = document.CreateParagraph();
run3 = para3.CreateRun();
run3.SetText("The second sub-item");
para3.SetNumID(numId, "1");
para3 = document.CreateParagraph();
run3 = para3.CreateRun();
run3.SetText("The sub-sub-item");
para3.SetNumID(numId, "2");

//Save the file and Launch
using (FileStream sw = new FileStream("ListNumbering.doc", FileMode.Create))
{
    document.Write(sw);
}