使用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;

}

}

相关推荐
小清兔2 小时前
c#基础知识
开发语言·数据库·学习·unity·c#·游戏引擎·.net
★YUI★6 小时前
学习游戏制作记录(保存装备物品技能树和删除存档文件)8.26
学习·游戏·unity·c#
爱炸薯条的小朋友6 小时前
C#由Dictionary不正确释放造成的内存泄漏问题与GC代系
开发语言·opencv·c#
weixin_456904277 小时前
C# .NET Framework 4.0 网络编程完全指南
网络·c#·.net
月巴月巴白勺合鸟月半17 小时前
如果 我退休了
c#
专注VB编程开发20年17 小时前
.NET组件读取压缩包中的内存数据电子表格XLSX文件和图片,不需要解压
linux·服务器·windows·c#·.net·excel·zip
爱上纯净的蓝天1 天前
迁移面试题
java·网络·c++·pdf·c#
单单单单点1 天前
C# 相机内存复用(减少图像采集耗时)以及行数复用
图像处理·计算机视觉·c#·相机
余衫马1 天前
Mysql 5.7 与 SqlSugar 5.X 整合开发实战
mysql·c#·orm·sqlsugar
cplmlm1 天前
WPF+MVVM入门学习
c#·wpf