当前位置:网站首页>Aspose. Word operation word document (II)
Aspose. Word operation word document (II)
2022-07-07 22:22:00 【Hey, hey, hey, hey, hey】
Catalog
6. Delete a column of the table
1. Create a table
/// <summary>
/// Create a table
/// </summary>
/// <param name="wordDoc"></param>
/// <param name="rowCount"> Row number </param>
/// <param name="cellCount"> Number of columns </param>
/// <param name="ln"> Newline creation </param>
/// <param name="alignment"> Paragraph alignment </param>
public void CreateTable(Document wordDoc, int rowCount, int cellCount, bool ln, ParagraphAlignment alignment = ParagraphAlignment.Left)
{
DocumentBuilder builder = new DocumentBuilder(wordDoc);
builder.MoveToDocumentEnd();
if (ln) builder.Writeln();
ParagraphFormat paragraph = builder.ParagraphFormat;
paragraph.Alignment = alignment;
builder.StartTable();
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < cellCount; j++)
{
builder.InsertCell();
}
builder.EndRow();
}
builder.EndTable();
paragraph.Alignment = ParagraphAlignment.Left;
}
2. Add data to the table
/// <summary>
/// Add data to the table
/// </summary>
/// <param name="dt"></param>
public void WriteTable(DataTable dt)
{
wordDoc.MailMerge.ExecuteWithRegions(dt);
}
Example :
Private void AddValueToTable()
{
DataTable dt = new DataTable(“StudentInfo”);
dt.Columns.Add(“Name”);
dt.Columns.Add(“Age”);
dt.Columns.Add(“Sex”);
dt.Rows.Add(new object[]{ “ Zhang San ”,”18”,” male ” });
WriteTable(dt);
}
3. Get a table
/// <summary>
/// Get form
/// </summary>
/// <param name="tableIndex"></param>
/// <returns></returns>
public Table GetTable(int tableIndex)
{
Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
return table;
}
4. Add a line to the table
/// <summary>
/// Add a row to the table
/// </summary>
/// <param name="tableIndex"> The first few forms </param>
public void AddRowItem(int tableIndex)
{
Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
Row row = (Row)table.LastRow.Clone(true);
table.AppendChild(row);
}
5. Delete table rows
/// <summary>
/// Delete the last row of the table
/// </summary>
/// <param name="tableIndex"></param>
public void RemoveLastRow(int tableIndex)
{
Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
Row row = (Row)table.LastRow;
table.RemoveChild(row);
}
/// <summary>
/// Delete the first row of the table
/// </summary>
/// <param name="tableIndex"></param>
public void RemoveFirstRow(int tableIndex)
{
Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
Row row = (Row)table.LastRow;
table.RemoveChild(row);
}
/// <summary>
/// Delete a row in the table
/// </summary>
/// <param name="tableIndex"></param>
/// <param name="rowIndex"></param>
public void RemoveRowItem(int tableIndex, int rowIndex)
{
Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
Row row = (Row)table.Rows[rowIndex];
if(row != null)
table.RemoveChild(row);
}
6. Delete a column of the table
/// <summary>
/// Delete a column of the table
/// </summary>
/// <param name="tableIndex"></param>
/// <param name="cellIndex"></param>
public void RemoveCellItem(int tableIndex, int cellIndex)
{
Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
for (int i = 0; i < table.Rows.Count; i++)
{
Cell cell = table.Rows[i].Cells[cellIndex];
if (cell != null)
table.Rows[i].RemoveChild(cell);
}
}
7. Assign values to cells
/// <summary>
/// Set a cell value in the table
/// </summary>
/// <param name="tableIndex"> form </param>
/// <param name="row"> Lines </param>
/// <param name="cell"> The first few columns </param>
/// <param name="text"> value </param>
public void SetCellValue(int tableIndex, int row, int cell, string text)
{
Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
Paragraph tempParagraph = table.Rows[row].Cells[cell].GetChild(NodeType.Paragraph, 0, true) as Paragraph;
if (tempParagraph != null)
{
Run run = tempParagraph.GetChild(NodeType.Run, 0, true) as Run;
if (run != null)
{
run.Text = text;
}
else
{
run = new Run(wordDoc);
run.Text = text;
tempParagraph.AppendChild(run);
}
}
else
{
tempParagraph = new Paragraph(wordDoc);
Run run = new Run(wordDoc);
run.Text = text;
tempParagraph.AppendChild(run);
}
}
/// <summary>
/// Set the cell value of a row in the table
/// </summary>
/// <param name="tableIndex"> form </param>
/// <param name="row"> Lines </param>
/// <param name="startCell"> Which column starts assignment </param>
/// <param name="text"> value </param>
public void SetCellValue(int tableIndex, int row,int startCell, string[] text)
{
Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
for (int i = startCell; i < table.Rows[row].Cells.Count; i++)
{
Paragraph tempParagraph = table.Rows[row].Cells[i].GetChild(NodeType.Paragraph, 0, true) as Paragraph;
if (tempParagraph != null)
{
Run run = tempParagraph.GetChild(NodeType.Run, 0, true) as Run;
if (run != null)
{
run.Text = text[i];
}
else
{
run = new Run(wordDoc);
run.Text = text[i];
tempParagraph.AppendChild(run);
}
}
else
{
tempParagraph = new Paragraph(wordDoc);
Run run = new Run(wordDoc);
run.Text = text[i];
tempParagraph.AppendChild(run);
}
}
}
8. merge cell
/// <summary>
/// Merge cells vertically
/// </summary>
/// <param name="tableIndex"> The first few forms </param>
/// <param name="startRow"> The subscript of the starting line to be merged </param>
/// <param name="endRow"> The subscript of the end line to be merged </param>
/// <param name="CellIndex"> Which column subscripts do you want to merge </param>
public void MergeCells(int tableIndex, int startRow, int endRow, int CellIndex)
{
if (endRow - startRow < 1) return;
Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
table.Rows[startRow].Cells[CellIndex].CellFormat.VerticalMerge = CellMerge.First;
for (int i = startRow + 1; i <= endRow; i++)
{
table.Rows[i].Cells[CellIndex].CellFormat.VerticalMerge = CellMerge.Previous;
}
}
/// <summary>
/// Merge cells horizontally
/// </summary>
/// <param name="tableIndex"> The first few forms </param>
/// <param name="startRow"> The subscript of the starting column to be merged </param>
/// <param name="endRow"> The subscript of the end column to be merged </param>
/// <param name="CellIndex"> Which line of subscripts do you want to merge </param>
public void MergeCells(int tableIndex, int startCell, int endCell, int rowIndex)
{
if (endRow - startRow < 1) return;
Table table = (Table)wordDoc.GetChild(NodeType.Table, tableIndex, true);
table.Rows[rowIndex].Cells[startCell].CellFormat.HorizontalMerge = CellMerge.First;
for (int i = startCell + 1; i <= endCell; i++)
{
table.Rows[rowIndex].Cells[i].CellFormat.HorizontalMerge = CellMerge.Previous;
}
}
边栏推荐
- [open source] Net ORM accessing Firebird database
- Song list 11111
- Debugging and handling the problem of jamming for about 30s during SSH login
- 【Azure微服务 Service Fabric 】因证书过期导致Service Fabric集群挂掉(升级无法完成,节点不可用)
- Leetcode SQL first day
- 反爬通杀神器
- SAR影像质量评估
- OpenGL homework - Hello, triangle
- [开源] .Net ORM 访问 Firebird 数据库
- DBSync新增对MongoDB、ES的支持
猜你喜欢
Implementation method of data platform landing
【Azure微服务 Service Fabric 】因证书过期导致Service Fabric集群挂掉(升级无法完成,节点不可用)
[azure microservice service fabric] the service fabric cluster hangs up because the certificate expires (the upgrade cannot be completed, and the node is unavailable)
How does win11 time display the day of the week? How does win11 display the day of the week today?
Ternary expressions, generative expressions, anonymous functions
#DAYU200体验官#MPPT光伏发电项目 DAYU200、Hi3861、华为云IotDA
The latest Android interview collection, Android video extraction audio
NVR硬盤錄像機通過國標GB28181協議接入EasyCVR,設備通道信息不顯示是什麼原因?
如何选择合适的自动化测试工具?
Overseas agent recommendation
随机推荐
Ant destination multiple selection
This experimental syntax requires enabling the parser plugin: ‘optionalChaining‘
ByteDance senior engineer interview, easy to get started, fluent
Dbsync adds support for mongodb and ES
PKPM 2020 software installation package download and installation tutorial
Matplotlib快速入门
VTOL in Px4_ att_ Control source code analysis [supplement]
What if the win11u disk does not display? Solution to failure of win11 plug-in USB flash disk
[JDBC Part 1] overview, get connection, CRUD
应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
Antd date component appears in English
The latest Android interview collection, Android video extraction audio
怎样写一个增广矩阵到txt文件中
The difference between NPM uninstall and RM direct deletion
Blender exchange group, welcome to the water group ~
Remove the default background color of chrome input input box
Pre sale 179000, hengchi 5 can fire? Product power online depends on how it is sold
Jerry's key to initiate pairing [chapter]
Win11游戏模式怎么开启?Win11开启游戏模式的方法
How to close eslint related rules