1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > C# Aspose Word表格合并 拆分 增删行 单元格操作

C# Aspose Word表格合并 拆分 增删行 单元格操作

时间:2022-06-09 05:07:03

相关推荐

C# Aspose Word表格合并 拆分 增删行 单元格操作

合并单元格

//合并比较简单//横向cell.CellFormat.HorizontalMerge = CellMerge.First;//开始合并单元格cell.CellFormat.HorizontalMerge = CellMerge.Previous; //被合并cell.CellFormat.HorizontalMerge = CellMerge.None; //无//纵向cell.CellFormat.VerticalMerge = CellMerge.First;cell.CellFormat.VerticalMerge = CellMerge.Previous;cell.CellFormat.VerticalMerge = CellMerge.None;

拆分单元格

对于已经被合并的单元格,再次读取时已经读不到原来的总列数了,这时需要用table.ConvertToHorizontallyMergedCells();转换为以列宽显示的合并单元格,这时就能读取到所有的单元格信息了,把被合并的单元格都取消掉,或者仅处理需要处理的之后需要doc.UpdatePageLayout();刷新表格。不然再次读取修改表格合并时会把表格打乱。

//表格预先处理foreach (Section section in doc.Sections){foreach (Table table in section.Body.Tables){if (true){//合并属性补全(关键操作)table.ConvertToHorizontallyMergedCells();//清空合并属性foreach (Row itemrow in table.Rows){foreach (Cell cell in itemrow.Cells){cell.CellFormat.HorizontalMerge = CellMerge.None;cell.CellFormat.VerticalMerge = CellMerge.None;}}}}}doc.UpdatePageLayout();//表格预先处理结束

删行

public static void DealDelRowList_Aspose(Aspose.Words.Tables.Table TableOne, int count){for (int i = 0; i < count; i++){spireTableOne.Rows.Remove(TableOne.Rows[spireTableOne.Rows.Count - 1]);}}

删列

/// <summary>/// 删除列/// </summary>/// <param name="table">表格</param>/// <param name="index">索引</param>public static void DeleteTableColumn_Aspose(Aspose.Words.Tables.Table table, int index){for (int i = 0; i < table.Rows.Count; i++){try{Aspose.Words.Tables.Cell asposeCell = table.Rows[i].Cells[table.Rows[i].Cells.Count - 1];//如果是合并的。拆开if (asposeCell.CellFormat.HorizontalMerge != Aspose.Words.Tables.CellMerge.None){asposeCell.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None;}table.Rows[i].Cells.RemoveAt(table.Rows[i].Cells.Count - 1);}catch{不规则表格}}}

插行

public static void DealAddRowList_Aspose(Aspose.Words.Tables.Table TableOne, int count){for (int i = 0; i < count; i++){Aspose.Words.Node row = TableOne.Rows[TableOne.Rows.Count - 1].Clone(true);TableOne.Rows.Insert(TableOne.Rows.Count - 1, row);}}

插列

public static void InsertTableColumn_Aspose(Aspose.Words.Tables.Table table, int count, Dictionary<int, float> columnsWidthCM){for (int x = 0; x < count; x++){for (int i = 0; i < table.Rows.Count; i++){var preCell = table.Rows[i].Cells.Last() as Aspose.Words.Tables.Cell;Aspose.Words.Tables.Cell cell = new Aspose.Words.Tables.Cell(table.Document);Aspose.Words.Paragraph paragraph = null;try{paragraph = preCell.FirstParagraph.Clone(true) as Aspose.Words.Paragraph;foreach (Aspose.Words.Run run in paragraph.Runs){run.Text = string.Empty;}}catch (Exception ex){//给个空段落paragraph = new Aspose.Words.Paragraph(table.Document);paragraph.AppendChild(new Aspose.Words.Run(table.Document, ""));}cell.AppendChild(paragraph);table.Rows[i].Cells.Insert(table.Rows[i].Cells.Count - 1, cell);try{cell.CellFormat.Width = CentimetersToPoints(columnsWidthCM[table.Rows[i].Cells.Count]);cell.CellFormat.PreferredWidth = Aspose.Words.Tables.PreferredWidth.Auto;}catch (Exception ex){LogHelper.Error(ex);}}}}/// <summary>/// 厘米转磅/// </summary>/// <param name="cm">厘米</param>/// <returns>磅</returns>public static float CentimetersToPoints(float cm){return cm * 28.3464567f;}

设置框线

/// <summary>/// 共享框线/// </summary>/// <param name="table">待修正表格</param>protected void ShareBorders(Table table){for (int i = 0; i < table.Rows.Count; i++){Row row = table.Rows[i];for (int j = 0; j < row.Cells.Count; j++){Cell cell = row.Cells[j];if (cell.CellFormat.Borders.Top.LineStyle != LineStyle.None){Cell precell = table.Rows[i-1].Cells[j];if (precell.CellFormat.Borders.Bottom.LineStyle != LineStyle.None && precell.CellFormat.Borders.Bottom.LineStyle != LineStyle.Double){precell.CellFormat.Borders.Bottom.LineStyle = cell.CellFormat.Borders.Top.LineStyle;}}if (i== table.Rows.Count-1){break;}if (cell.CellFormat.Borders.Bottom.LineStyle != LineStyle.None){Cell nextcell = table.Rows[i + 1].Cells[j];if (nextcell.CellFormat.Borders.Top.LineStyle != LineStyle.None && nextcell.CellFormat.Borders.Top.LineStyle != LineStyle.Double){nextcell.CellFormat.Borders.Top.LineStyle = cell.CellFormat.Borders.Bottom.LineStyle;}}}}}

把第一个表格的行复制到第二个表中

//复制第一个表的标题行for (int i = titleRowCount - 1; i >= 0; i--){Aspose.Words.Node row = firstTable.Rows[i].Clone(true);currentTable.Rows.Insert(0, row);}

单元格段落的中西文间距

/// <summary>/// 禁止自动调整中西文间距/// </summary>/// <param name="cell">单元格</param>public static void DisableCellParagraphsAutoSpace_Aspose(Aspose.Words.Tables.Cell cell){foreach (Aspose.Words.Paragraph paragraph in cell.Paragraphs){// 自动调整中文与西文的间距paragraph.ParagraphFormat.SpaceBeforeAuto = false;// 自动调整中文与数字的间距paragraph.ParagraphFormat.SpaceAfterAuto = false;}}

单元格文字设置下划线

/// <summary>/// 设置段落下划线样式/// </summary>/// <param name="cell">单元格</param>/// <param name="redisCell">Redis数据实体</param>public static void SetCellParagraphsUnderlineStyle_Aspose(Aspose.Words.Tables.Cell cell, bool FontUnderline){foreach (Aspose.Words.Paragraph paragraph in cell.Paragraphs){foreach (var child in paragraph.Runs){if (child is Aspose.Words.Paragraph){(child as Aspose.Words.Paragraph).ParagraphBreakFont.Underline = FontUnderline ? Aspose.Words.Underline.Single : Aspose.Words.Underline.None;}}}}

单元格边框线

/// <summary>/// 单元格边框线设置/// </summary>/// <param name="asposeCell"></param>/// <param name="redisCell"></param>public static void SetCellBorderStyle_Aspose(Aspose.Words.Tables.Cell asposeCell){// 上边框线asposeCell.CellFormat.Borders.Top.LineStyle = Aspose.Words.LineStyle.Single;// 下边框线asposeCell.CellFormat.Borders.Bottom.LineStyle = Aspose.Words.LineStyle.Double;// 左边框asposeCell.CellFormat.Borders.Left.LineStyle = Aspose.Words.LineStyle.Double;// 右边框asposeCell.CellFormat.Borders.Right.LineStyle = Aspose.Words.LineStyle.None;}

文字样式设置

Run tr = para.ChildNodes[j] as Run;tr.Font.Name = fontNameNonFarEast;tr.Font.NameAscii = fontNameNonFarEast;tr.Font.NameBi = fontNameNonFarEast;tr.Font.NameFarEast = fontNameNonFarEast;//避免干扰,最后设置tr.Font.NameFarEast = fontNameFarEastName;if (fontsize != 0){tr.Font.Size = (usefirstFontSize && FirstFontSize != 0) ? FirstFontSize : fontsize;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。