Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Sep 01, 2021 8:47 am

您好

目前我有一個表格內容如下圖
table.PNG


有兩個問題想請教
1.如何找到字串{{AA}}是在第幾列(row index)?
2.如果想要在表格換-品-數...... 底下新增多筆detail該如何做?

在麻煩您了,謝謝

d0177954
 
Posts: 6
Joined: Tue Feb 23, 2021 7:38 am

Wed Sep 01, 2021 9:56 am

您好,

感謝您的諮詢。
關於您的第一個問題,請參考以下代碼。
Code: Select all
            Document document = new Document();
            document.LoadFromFile("test.docx");

            Body body = document.Sections[0].Body;

            ITable table = body.Tables[0];

            for(int i=0; i < table.Rows.Count; i++)
            {
                for(int j=0; j < table.Rows[i].Cells.Count; j++)
                {
                    foreach(DocumentObject documentObject in table.Rows[i].Cells[j].ChildObjects)
                    {
                        if (documentObject.DocumentObjectType.Equals(DocumentObjectType.Paragraph))
                        {
                            Paragraph paragraph = documentObject as Paragraph;
                            if (paragraph.Text.Contains("{{AA}}"))
                            {
                                Console.Write(j+1);
                            }
                        }
                    }
                   
                }
            }


至於您的第二個問題,很抱歉我們並不是很明白您的需求。請提供更多信息,包括您的輸入文檔和期望生成的結果文檔給我們做進一步的調查。您可以通過郵件將文檔發送給我們(support@e-iceblue.com)。提前感謝。

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Thu Sep 02, 2021 1:33 am

您好
不好意思關於第二點表達不清楚

下圖是我原先的表格內容
before.PNG


我想要找到字串{{AA}}在第幾列後,將此列刪除,新增多筆明細如下圖呈現結果
after.PNG


在麻煩您們了,感謝

d0177954
 
Posts: 6
Joined: Tue Feb 23, 2021 7:38 am

Thu Sep 02, 2021 4:46 am

您好,

感謝您的響應。
請參考以下修改後的代碼。
Code: Select all
            Document document = new Document();
            document.LoadFromFile("test.docx");

            Body body = document.Sections[0].Body;

            ITable table = body.Tables[0];

            int index = -1;

            for (int i = 0; i < table.Rows.Count; i++)
            {
                for (int j = 0; j < table.Rows[i].Cells.Count; j++)
                {
                    foreach (DocumentObject documentObject in table.Rows[i].Cells[j].ChildObjects)
                    {
                        if (documentObject.DocumentObjectType.Equals(DocumentObjectType.Paragraph))
                        {
                            Paragraph paragraph = documentObject as Paragraph;
                            if (paragraph.Text.Contains("{{AA}}"))
                            {
                                index = i;
                            }
                        }
                    }

                }
            }

            String[][] data =
                {
                new String[]{"MC01123546", "品名A", "1", "100", "100", "是", "TEST"},
                new String[]{"PC01-2562", "品名B", "1", "210", "210", "否", ""},
                new String[]{"AT9854-5651", "品名C", "5", "120", "600", "是", "備註" }
            };
            TableRow detail = null;

            for (int i = 0; i < data.Length; i++)
            {
                detail = new TableRow(document);
                for (int j = 0; j < data[0].Length; j++)
                {
                    TableCell tc = detail.AddCell();
                    Paragraph paragraph = tc.AddParagraph();
                    paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
                    paragraph.AppendText(data[i][j]);
                }
                table.Rows.Insert(index+1+i, detail);
            }

            if (index != -1)
            {
                table.Rows.RemoveAt(index);
            }

            document.SaveToFile("Sample.docx", FileFormat.Docx);

如果還有其他和我們產品相關的問題,請隨時聯繫我們。

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Thu Sep 02, 2021 5:52 am

您好
感謝您的回覆及解答

使用您提供的方法測試後發現
雖然表格欄位已經產生,但是表格的顯示位置不正確,如下圖
error.PNG


想請教一下,可以如何修正呢?
或是有哪些文件可以做參考?

在麻煩您了,謝謝

d0177954
 
Posts: 6
Joined: Tue Feb 23, 2021 7:38 am

Thu Sep 02, 2021 7:10 am

您好,

感謝您的反馈。
我基於您的截圖模擬了一個word文檔測試,沒有重現到您的問題,為了幫助我們更快、更準確地調查您的問題,請提供您的輸入 文件。您可以通過電子郵件將它們發送給我們 (support@e-iceblue.com)。提前致謝。

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Fri Sep 03, 2021 3:56 am

您好
已將Word測試文檔寄給您,在麻煩幫我看看是否有收到

目前有研究出我要得結果了
能否再請您提供是否有更好的方法
謝謝您

d0177954
 
Posts: 6
Joined: Tue Feb 23, 2021 7:38 am

Fri Sep 03, 2021 6:20 am

您好,

感谢通過郵件提供您的测试文档。
請參考以下修改後的代碼。
Code: Select all
            Document document = new Document();
            document.LoadFromFile("forEiceblue.docx");

            Body body = document.Sections[0].Body;

            ITable table = body.Tables[0];

            int index = -1;

            for (int i = 0; i < table.Rows.Count; i++)
            {
                for (int j = 0; j < table.Rows[i].Cells.Count; j++)
                {
                    foreach (DocumentObject documentObject in table.Rows[i].Cells[j].ChildObjects)
                    {
                        if (documentObject.DocumentObjectType.Equals(DocumentObjectType.Paragraph))
                        {
                            Paragraph paragraph = documentObject as Paragraph;
                            if (paragraph.Text.Contains("{{AA}}"))
                            {
                                index = i;
                            }
                        }
                    }

                }
            }

            String[][] data =
                {
                new String[]{"MC01123546", "品名A", "1", "100", "100", "是", "TEST"},
                new String[]{"PC01-2562", "品名B", "1", "210", "210", "否", ""},
                new String[]{"AT9854-5651", "品名C", "5", "120", "600", "是", "備註" }
            };
            TableRow detail = null;

            for (int i = 0; i < data.Length; i++)
            {
                detail = table.Rows[index].Clone();
                for (int j = 0; j < data[0].Length; j++)
                {
                    TableCell tc = detail.Cells[j];
                    tc.ChildObjects.Clear();
                    Paragraph paragraph = tc.AddParagraph();
                    paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
                    paragraph.AppendText(data[i][j]);
                }
                table.Rows.Insert(index + 1 + i, detail);
            }

            if (index != -1)
            {
                table.Rows.RemoveAt(index);
            }

            document.SaveToFile("Sample.docx", FileFormat.Docx);

如果還有其他和我們產品相關的問題,請隨時聯繫我們。

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Thu Sep 16, 2021 10:27 am

您好,

希望您一切安好。
請問我之前給您提供的代碼能解決您的問題嗎?方便在空閒的時候給我們反饋嗎?

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.Doc