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 May 29, 2019 8:14 pm
Hi - I'm trying to create a table from a List<>. I'm struggling with how to get the values showing in each cell.
- Code: Select all
Spire.Doc.Table table = section.AddTable(true);
var testString = new List<string>();
testString.AddRange(new String[] { "John Jones", "Nothing Here" });
testString.AddRange(new String[] { "Pete Smith", "Received 19/11/2018" });
table.ResetCells(testString.Count,2);
int RowCount = 0;
foreach(var item in testString)
{
//Create Rows
RowCount = RowCount + 1;
TableRow dataRow = table.Rows[RowCount];
dataRow.Height = 20;
dataRow.HeightType = TableRowHeightType.Exactly;
dataRow.RowFormat.BackColor = Color.Empty;
//Can't work out how to get the cell data here!
}
-
Wilshaw
-
- Posts: 6
- Joined: Fri May 17, 2019 8:44 pm
Thu May 30, 2019 6:32 am
Dear ,
Thanks for your inquiry.
Note that the List you defined is one-dimensional array. Please try the following code to fill each cells.
- Code: Select all
Document doc = new Document();
Section section = doc.AddSection();
Spire.Doc.Table table = section.AddTable(true);
var testString = new List<string>();
testString.AddRange(new String[] { "John Jones", "Nothing Here" });
testString.AddRange(new String[] { "Pete Smith", "Received 19/11/2018" });
//change the column number 2 to 1
table.ResetCells(testString.Count, 1);
int RowCount = 0;
foreach (var item in testString)
{
//create table row
RowCount = RowCount + 1;
TableRow dataRow = table.Rows[RowCount - 1];
dataRow.Height = 20;
dataRow.HeightType = TableRowHeightType.Exactly;
dataRow.RowFormat.BackColor = Color.Empty;
//fill data to cell
for (int c = 0; c < dataRow.Cells.Count; c++)
{
dataRow.Cells[c].AddParagraph().AppendText(item);
}
}
doc.SaveToFile("result.docx", FileFormat.Docx2013);
Or you could define a List<List<string>> and fill table with data from it. Sample code is given as below.
- Code: Select all
Document doc = new Document();
Section section = doc.AddSection();
Spire.Doc.Table table = section.AddTable(true);
//define a lists
List<List<string>> lists = new List<List<string>>();
List<string> testString1 = new List<string>();
List<string> testString2 = new List<string>();
testString1.AddRange(new String[] { "John Jones", "Nothing Here" });
testString2.AddRange(new String[] { "Pete Smith", "Received 19/11/2018" });
lists.Add(testString1);
lists.Add(testString2);
table.ResetCells(lists.Count, testString1.Count);
for (int r = 0; r < lists.Count; r++)
{
//create table row
TableRow dataRow = table.Rows[r];
dataRow.Height = 20;
dataRow.HeightType = TableRowHeightType.Exactly;
dataRow.RowFormat.BackColor = Color.Empty;
//fill data to cell
for (int c = 0; c < lists[r].Count; c++)
{
Paragraph p = dataRow.Cells[c].AddParagraph();
TextRange tr = p.AppendText(lists[r][c]);
}
}
doc.SaveToFile("result.docx", FileFormat.Docx2013);
If there is any question, please feel free to write back.
Sincerely,
Nina
E-iceblue support team
-
Nina.Tang
-
- Posts: 1182
- Joined: Tue Sep 27, 2016 1:06 am
Thu May 30, 2019 9:18 pm
Thank you for your response - with the help of your answer and a sample in your Demo pages, I was able to get things working fine.
I converted the List<> to a multi-dimensional array with Linq :-
- Code: Select all
String[][] data = lists.Select(a => a.ToArray()).ToArray();
Then I was able to use:
- Code: Select all
for (int r = 0; r < data.Length; r++)
{
TableRow dataRow = table.Rows[r];
dataRow.Height = 20;
dataRow.HeightType = TableRowHeightType.Auto;
dataRow.RowFormat.BackColor = Color.Empty;
for (int c = 0; c < data[r].Length; c++)
{
dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
dataRow.Cells[c].AddParagraph().AppendText(data[r][c]);
}
}
-
Wilshaw
-
- Posts: 6
- Joined: Fri May 17, 2019 8:44 pm
Fri May 31, 2019 1:16 am
Hello,
Thank you so much for sharing your solution. Just feel free to contact us if you need other helps.
Wish you all the best!
Sincerely,
Lisa
E-icbelue support team
-
Lisa.Li
-
- Posts: 1261
- Joined: Wed Apr 25, 2018 3:20 am