Data
e-iceblue
Name | Capital | Continent | Area | Population | Flag |
Argentina | Buenos Aires | South America | 2777815 | 32300003 | |
Bolivia | La Paz | South America | 1098575 | 7300000 | |
Brazil | Brasilia | South America | 8511196 | 150400000 | |
Canada | Ottawa | North America | 9976147 | 26500000 | |
Chile | Santiago | South America | 756943 | 13200000 | |
Colombia | Bagota | South America | 1138907 | 33000000 | |
Cuba | Havana | North America | 114524 | 10600000 | |
Ecuador | Quito | South America | 455502 | 10600000 | |
El Salvador | San Salvador | North America | 20865 | 5300000 | |
Guyana | Georgetown | South America | 214969 | 800000 |
Option
downloads
- Demo
- Java
- C# source
This demo shows you how to create a table with specified data in a Word document. We also show you how to set the border and background color of the table.
import com.spire.doc.Document; import com.spire.doc.Section; import com.spire.doc.Table; import com.spire.doc.TableRow; import com.spire.doc.documents.*; import com.spire.doc.fields.TextRange; import com.spire.doc.formatting.CellFormat; import java.awt.*; import java.util.Base64; import java.util.List; public class TableDemo { public static String []headerName={"Name","Capital","Continent","Area","Population"}; public void addTable(String docFile, Color headerBackColor, Color rowBackColor, Color alternationBackColor, Color borderColor,List dataList){ Document doc = new Document(); doc.loadFromFile(docFile); Section section = doc.getSections().get(0); Table table = section.addTable(); int rowCount = dataList.size(); table.setDefaultRowHeight(25f); table.setDefaultColumnWidth(0f); table.resetCells(rowCount + 1, headerName.length); //Borders Borders borders = table.getTableFormat().getBorders(); //Left borders.getLeft().setBorderType(BorderStyle.Hairline); borders.getLeft().setColor(borderColor); //Right borders.getRight().setBorderType(BorderStyle.Hairline); borders.getRight().setColor(borderColor); //Bottom borders.getBottom().setBorderType(BorderStyle.Hairline); borders.getBottom().setColor(borderColor); //Top borders.getTop().setBorderType(BorderStyle.Hairline); borders.getTop().setColor(borderColor); //Horizontal borders.getHorizontal().setBorderType(BorderStyle.Hairline); borders.getHorizontal().setColor(borderColor); //Vertical borders.getVertical().setBorderType(BorderStyle.Hairline); borders.getVertical().setColor(borderColor); //Table header TableRow headerRow = table.getRows().get(0); headerRow.isHeader(true); for (int i = 0; i < headerName.length; i++) { Paragraph p = headerRow.getCells().get(i).addParagraph(); p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); TextRange headerText = p.appendText(headerName[i]); headerText.getCharacterFormat().setBold(true); CellFormat cellStyle = headerRow.getCells().get(i).getCellFormat(); cellStyle.setVerticalAlignment(VerticalAlignment.Middle); cellStyle.setBackColor(headerBackColor); } //Data row for (int i = 0; i < rowCount; i++) { CountryModel countryModel = dataList.get(i); //Country name Paragraph p=table.getRows().get(i+1).getCells().get(0).addParagraph(); p.appendText(countryModel.getName()); //Capital p=table.getRows().get(i+1).getCells().get(1).addParagraph(); p.appendText(countryModel.getCapital()); //Continent p=table.getRows().get(i+1).getCells().get(2).addParagraph(); p.appendText(countryModel.getContinent()); //Area p=table.getRows().get(i+1).getCells().get(3).addParagraph(); p.appendText(String.valueOf(countryModel.getArea())); //Population p=table.getRows().get(i+1).getCells().get(4).addParagraph(); p.appendText(String.valueOf(countryModel.getPopulation())); //Flag image p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); byte[] imgData = Base64.getDecoder().decode(String.valueOf(countryModel.getFlag())); p.appendPicture(imgData); } //Set cell styles for (int i = 0; i < rowCount; i++) { for(int j = 0; j < headerName.length; j++){ CellFormat cellFormat = table.getRows().get(i + 1).getCells().get(j).getCellFormat(); cellFormat.setVerticalAlignment(VerticalAlignment.Middle); cellFormat.setBackColor(rowBackColor); if (alternationBackColor != null && i % 2 == 1) { cellFormat.setBackColor(alternationBackColor); } } } } } class CountryModel { private String name; private String capital; private String continent; private int area; private long population; /** * base64 image data */ private String flag; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCapital() { return capital; } public void setCapital(String capital) { this.capital = capital; } public String getContinent() { return continent; } public void setContinent(String continent) { this.continent = continent; } public int getArea() { return area; } public void setArea(int area) { this.area = area; } public long getPopulation() { return population; } public void setPopulation(long population) { this.population = population; } public String getFlag() { return flag; } public void setFlag(String flag) { this.flag = flag; } }
using System; using System.Data; using System.Drawing; using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Formatting; namespace DemoOnlineCode { class Table { public void demoTable(String docFile, DataTable dataTable, string borderColor = null, string headerBackColor = null, string rowBackColor = null, string alternationRowColor = null) { Document document = new Document(docFile, FileFormat.Auto); addTable(document.Sections[0], dataTable, ConvertStrToColor(borderColor), ConvertStrToColor(headerBackColor), ConvertStrToColor(rowBackColor), ConvertStrToColor(alternationRowColor)); document.SaveToFile("demo.doc", FileFormat.Doc); } private void addTable(Section section, DataTable dataTable, Color borderColor, Color headerBackColor, Color rowBackColor, Color alternationRowColor) { Spire.Doc.Table table = section.AddTable(); int rowCount = dataTable.Rows.Count; int columnCount = dataTable.Columns.Count; table.DefaultRowHeight = 25; table.DefaultColumnWidth = 0; table.ResetCells(rowCount + 1, columnCount); table.TableFormat.Borders.Left.BorderType = Spire.Doc.Documents.BorderStyle.Hairline; table.TableFormat.Borders.Left.Color = borderColor; table.TableFormat.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.Hairline; table.TableFormat.Borders.Top.Color = borderColor; table.TableFormat.Borders.Right.BorderType = Spire.Doc.Documents.BorderStyle.Hairline; table.TableFormat.Borders.Right.Color = borderColor; table.TableFormat.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.Hairline; table.TableFormat.Borders.Bottom.Color = borderColor; table.TableFormat.Borders.Horizontal.BorderType = Spire.Doc.Documents.BorderStyle.Hairline; table.TableFormat.Borders.Horizontal.Color = borderColor; table.TableFormat.Borders.Vertical.BorderType = Spire.Doc.Documents.BorderStyle.Hairline; table.TableFormat.Borders.Vertical.Color = borderColor; TableRow headerRow = table.Rows[0]; headerRow.IsHeader = true; for (int c = 0; c < columnCount; c++) { Paragraph p = headerRow.Cells[c].AddParagraph(); p.Format.HorizontalAlignment = HorizontalAlignment.Center; Spire.Doc.Fields.TextRange headerText = p.AppendText(dataTable.Columns[c].ColumnName); headerText.CharacterFormat.Bold = true; CellFormat cellStyle = headerRow.Cells[c].CellFormat; cellStyle.VerticalAlignment = VerticalAlignment.Middle; headerRow.Cells[c].CellFormat.BackColor = headerBackColor; } for (int i = 0; i < rowCount; i++) { object[] rowContent = dataTable.Rows[i].ItemArray; DataRow row = dataTable.Rows[i]; for (int j = 0; j < columnCount; j++) { Paragraph p = table.Rows[i + 1].Cells[j].AddParagraph(); if (rowContent[j] is byte[]) { p.Format.HorizontalAlignment = HorizontalAlignment.Center; p.AppendPicture(rowContent[j] as byte[]); } else { p.AppendText(rowContent[j].ToString()); } CellFormat cellStyle = table.Rows[i + 1].Cells[j].CellFormat; cellStyle.VerticalAlignment = VerticalAlignment.Middle; cellStyle.BackColor = rowBackColor; if (i % 2 == 1 && alternationRowColor != Color.Empty) { cellStyle.BackColor = alternationRowColor; } } } } private Color ConvertStrToColor(string strColor) { if (String.IsNullOrWhiteSpace(strColor)) { return Color.Empty; } else { return ColorTranslator.FromHtml("#" + strColor); } } } }
No Matter How Big or Small Your Project is,
Any technical question related to our product, contact us at support@e-iceblue.com.
Any question related to the purchase of product, contact us at sales@e-iceblue.com.
If you don't find the function you want, please request a free demo from us.