当前位置:网站首页>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;
}
}边栏推荐
- Overseas agent recommendation
- ByteDance Android interview, summary of knowledge points + analysis of interview questions
- Interview question 01.02 Determine whether it is character rearrangement - auxiliary array algorithm
- Win11时间怎么显示星期几?Win11怎么显示今天周几?
- NVR hard disk video recorder is connected to easycvr through the national standard gb28181 protocol. What is the reason why the device channel information is not displayed?
- Oracle advanced (VI) Oracle expdp/impdp details
- Write in front -- Talking about program development
- What is the difference between the three values of null Nan undefined in JS
- 【colmap】稀疏重建转为MVSNet格式输入
- PDF文档签名指南
猜你喜欢

Debugging and handling the problem of jamming for about 30s during SSH login

It's worth seeing. Interview sites and interview skills

EasyCVR配置中心录像计划页面调整分辨率时的显示优化

Customer case | China law network, through observing the cloud, greatly shortens the time of fault location
![[azure microservice service fabric] how to transfer seed nodes in the service fabric cluster](/img/b6/e5d525d9c7c28f6ef04c3f59b40eb3.png)
[azure microservice service fabric] how to transfer seed nodes in the service fabric cluster

Anti climbing killer
![Jerry's initiation of ear pairing, reconnection, and opening of discoverable and connectable cyclic functions [chapter]](/img/14/1c8a70102c106f4631853ed73c4d82.png)
Jerry's initiation of ear pairing, reconnection, and opening of discoverable and connectable cyclic functions [chapter]

使用 CustomPaint 绘制基本图形

Index summary (assault version)

建立自己的网站(18)
随机推荐
VTOL in Px4_ att_ Control source code analysis [supplement]
Dayu200 experience officer MPPT photovoltaic power generation project dayu200, hi3861, Huawei cloud iotda
Jerry's fast pairing does not support canceling pairing [article]
Talk about relational database and serverless
Relationship between URL and URI
Cv2.resize function reports an error: error: (-215:assertion failed) func= 0 in function ‘cv::hal::resize‘
[open source] Net ORM accessing Firebird database
Win11游戏模式怎么开启?Win11开启游戏模式的方法
[JDBC Part 1] overview, get connection, CRUD
Interview question 01.02 Determine whether it is character rearrangement - auxiliary array algorithm
Implementation method of data platform landing
Jerry's configuration of TWS cross pairing [article]
Vs custom template - take the custom class template as an example
Pre sale 179000, hengchi 5 can fire? Product power online depends on how it is sold
嵌入式开发:如何为项目选择合适的RTOS?
[advanced MySQL] index details (I): index data page structure
Remember aximp once Use of exe tool
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
强化学习-学习笔记9 | Multi-Step-TD-Target
NVR hard disk video recorder is connected to easycvr through the national standard gb28181 protocol. What is the reason why the device channel information is not displayed?