当前位置:网站首页>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;
}
}
边栏推荐
- How to choose the appropriate automated testing tools?
- 23. Merge K ascending linked lists -c language
- SAR image quality evaluation
- 【Azure微服务 Service Fabric 】因证书过期导致Service Fabric集群挂掉(升级无法完成,节点不可用)
- 【colmap】稀疏重建转为MVSNet格式输入
- Have you ever been confused? Once a test / development programmer, ignorant gadget C bird upgrade
- [azure microservice service fabric] the service fabric cluster hangs up because the certificate expires (the upgrade cannot be completed, and the node is unavailable)
- [开源] .Net ORM 访问 Firebird 数据库
- 建立自己的网站(18)
- Use partial derivatives to display normals in unity
猜你喜欢
VTOL in Px4_ att_ Control source code analysis [supplement]
Talk about relational database and serverless
EasyCVR配置中心录像计划页面调整分辨率时的显示优化
How does win11 time display the day of the week? How does win11 display the day of the week today?
谈谈制造企业如何制定敏捷的数字化转型策略
Two methods of calling WCF service by C #
Dayu200 experience officer MPPT photovoltaic power generation project dayu200, hi3861, Huawei cloud iotda
Win11U盘不显示怎么办?Win11插U盘没反应的解决方法
嵌入式开发:如何为项目选择合适的RTOS?
Kirin Xin'an operating system derivative solution | storage multipath management system, effectively improving the reliability of data transmission
随机推荐
OpenGL configuration vs2019
强化学习-学习笔记9 | Multi-Step-TD-Target
How to realize the movement control of characters in horizontal game
Jerry's initiation of ear pairing, reconnection, and opening of discoverable and connectable cyclic functions [chapter]
How to make agile digital transformation strategy for manufacturing enterprises
vite Unrestricted file system access to
使用 CustomPaint 绘制基本图形
23. Merge K ascending linked lists -c language
Song list 11111
Pre sale 179000, hengchi 5 can fire? Product power online depends on how it is sold
Jerry's about TWS pairing mode configuration [chapter]
operator
OpenGL jobs - shaders
Can I open a stock account directly online now? Is it safe?
Jerry's about TWS channel configuration [chapter]
Revit secondary development - project file to family file
【Azure微服务 Service Fabric 】因证书过期导致Service Fabric集群挂掉(升级无法完成,节点不可用)
NVR硬盤錄像機通過國標GB28181協議接入EasyCVR,設備通道信息不顯示是什麼原因?
Programming mode - table driven programming
Win11游戏模式怎么开启?Win11开启游戏模式的方法