当前位置:网站首页>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;
}
}
边栏推荐
- [开源] .Net ORM 访问 Firebird 数据库
- Interview question 01.02 Determine whether it is character rearrangement - auxiliary array algorithm
- Record layoutrebuild Forcerebuildlayoutimmediate does not take effect
- Jerry's about TWS pairing mode configuration [chapter]
- Overseas agent recommendation
- Application practice | the efficiency of the data warehouse system has been comprehensively improved! Data warehouse construction based on Apache Doris in Tongcheng digital Department
- Can I open a stock account directly online now? Is it safe?
- Tsconfig of typescript TS basics JSON configuration options
- Firefox browser installation impression notes clipping
- Attitude estimation (complementary filtering)
猜你喜欢
Jerry's manual matching method [chapter]
嵌入式开发:如何为项目选择合适的RTOS?
Solve the problem of uni in uni app Request sent a post request without response.
[开源] .Net ORM 访问 Firebird 数据库
VTOL in Px4_ att_ Control source code analysis [supplement]
#DAYU200体验官#MPPT光伏发电项目 DAYU200、Hi3861、华为云IotDA
用语雀写文章了,功能真心强大!
三元表达式、各生成式、匿名函数
Jerry's initiation of ear pairing, reconnection, and opening of discoverable and connectable cyclic functions [chapter]
Have you ever been confused? Once a test / development programmer, ignorant gadget C bird upgrade
随机推荐
How does win11 unblock the keyboard? Method of unlocking keyboard in win11
. Net automapper use
How polardb-x does distributed database hotspot analysis
JS number is insufficient, and 0 is added
23. Merge K ascending linked lists -c language
Win11如何解禁键盘?Win11解禁键盘的方法
建立自己的网站(18)
[open source] Net ORM accessing Firebird database
Remember that a development is encountered in the pit of origin string sorting
海外代理推荐
How to make agile digital transformation strategy for manufacturing enterprises
Debugging and handling the problem of jamming for about 30s during SSH login
使用 CustomPaint 绘制基本图形
vite Unrestricted file system access to
648. Word replacement
PKPM 2020软件安装包下载及安装教程
It's worth seeing. Interview sites and interview skills
[azure microservice service fabric] the service fabric cluster hangs up because the certificate expires (the upgrade cannot be completed, and the node is unavailable)
TCP/IP 协议栈
Jerry's initiation of ear pairing, reconnection, and opening of discoverable and connectable cyclic functions [chapter]