您好
目前我有一個表格內容如下圖
有兩個問題想請教
1.如何找到字串{{AA}}是在第幾列(row index)?
2.如果想要在表格換-品-數...... 底下新增多筆detail該如何做?
在麻煩您了,謝謝
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);
}
}
}
}
}
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);
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);