使用aspose.Words更新表格列宽

public static bool UpdateTableStyle(string filePath)

{

try

{

Document doc = new Document();

//样式

Style style = doc.Styles.Add(StyleType.Paragraph, "cellStyle");

style.Font.Name = "simsun";

PageSetup pageSet = doc.Sections[0].PageSetup;

pageSet.PaperSize = PaperSize.A4;

pageSet.LeftMargin = ConvertUtil.MillimeterToPoint(5);

pageSet.RightMargin = ConvertUtil.MillimeterToPoint(5);

//pageSet.PageStartingNumber = 1;

//pageSet.PageNumberStyle = NumberStyle.Arabic;

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);

builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

//builder.InsertField("PAGE", "");

//builder.Write("/");

//builder.InsertField("NUMPAGES", "");

double cellWidth = 18;

double portraitMinWidth = ConvertUtil.MillimeterToPoint(cellWidth);//纵向最小列宽

double pageWidth = 200d;//A4纵向可编辑区域宽度200毫米//A4横向向可编辑区域宽度290毫米

List<Document> docItemList = new List<Document>();

bool IsLandscape = false;//是否为横向

//先计算是使用横向还是纵向的A4

if (File.Exists(filePath))

{

Document docItem = new Document(filePath);

docItemList.Add(docItem);

var tableList = docItem.GetChildNodes(NodeType.Table, true);

bool isExist = false;

foreach (Table table in tableList)

{

if(table.Range != null && table.Range.Bookmarks != null)

{

foreach (Bookmark bkName in table.Range.Bookmarks)

{

if (bkName.Name == "aa")

{

isExist = true;

break;

}

}

}

if (isExist)

{

//先计算是否符合纵向A4纸要求的列宽

double width = ConvertUtil.MillimeterToPoint(200d / table.Rows[2].Cells.Count);

if (width < portraitMinWidth)

{

portraitMinWidth = width;

}

}

}

//不包含指标书签时,退出

if (!isExist)

return false;

}

//如果列宽小于一定宽度那么就将文档变为横向cellWidth

if (portraitMinWidth < ConvertUtil.MillimeterToPoint(cellWidth))

{

IsLandscape = true;

pageWidth = 150d;

}

else

{

IsLandscape = false;

pageWidth = 120d;

}

int i = 1;

foreach (Document docItem in docItemList)

{

builder.MoveToDocumentEnd();

// Document docItem = new Document(path);

PageSetup pageSet1 = docItem.Sections[0].PageSetup;

pageSet1.PaperSize = PaperSize.A4;

pageSet1.LeftMargin = ConvertUtil.MillimeterToPoint(20);

pageSet1.RightMargin = ConvertUtil.MillimeterToPoint(20);

var tableList = docItem.GetChildNodes(NodeType.Table, true);

foreach (Table table in tableList)

{

table.AutoFit(AutoFitBehavior.FixedColumnWidths);

foreach (Row row in table.Rows)

{

if (table.Range != null && table.Range.Bookmarks != null)

{

foreach (Bookmark bkName in table.Range.Bookmarks)

{

if (bkName.Name == "aa")

{

for (int j = 0; j < row.Cells.Count; j++)

{

row.Cells[j].CellFormat.WrapText = true;

double width = ConvertUtil.MillimeterToPoint((pageWidth) / row.Cells.Count);

row.Cells[j].CellFormat.Width = width;

}

break;

}

}

}

}

}

docItem.FirstSection.HeadersFooters.LinkToPrevious(true);

builder.InsertDocument(docItem, ImportFormatMode.UseDestinationStyles);

i++;

}

if (IsLandscape)

{

//文档变为横向

pageSet.Orientation = Aspose.Words.Orientation.Landscape;

}

//重新整理页面布局

doc.UpdatePageLayout();

doc.Save(filePath);

return true;

}

catch (Exception ex)

{

LogHelper.WriteLog("更新表格列宽异常,", ex);

return false;

}

}

相关推荐
czhc114007566310 分钟前
C# 1221
java·servlet·c#
yong99901 小时前
C#实现OPC客户端与S7-1200 PLC的通信
开发语言·网络·算法·c#
先生沉默先2 小时前
c#Socket学习,使用Socket创建一个在线聊天,服务端功能实现,(3)
服务器·学习·c#
superman超哥2 小时前
仓颉热点代码识别深度解析
开发语言·后端·python·c#·仓颉
Lv11770083 小时前
Visual Studio中的接口
ide·笔记·c#·visual studio
阿蒙Amon4 小时前
C#每日面试题-类和结构的区别
开发语言·c#
MyBFuture5 小时前
索引器实战:对象数组访问技巧及命名空间以及项目文件规范
开发语言·前端·c#·visual studio
jghhh016 小时前
基于C#的串口电子秤测试程序
开发语言·c#
向宇it6 小时前
【unity游戏开发——网络】使用Unity+PurrNet+Heathens+Steam,在 Unity 中通过 Steam与你的朋友建立联系
网络·游戏·unity·c#·游戏引擎·steam
WebRuntime6 小时前
问世间,exe是何物?直教AI沉默、Web寡言(1)
javascript·c#·.net·web