zoukankan      html  css  js  c++  java
  • C# Excel 中设置文字对齐方式、方向和换行

    在Excel表格中输入文字时,我们常常需要调整文字对齐方式或者对文字进行换行。本文将介绍如何通过编程的方式设置文字对齐方式,改变文字方向以及对文字进行换行。

    //创建Workbook对象
    Workbook wookbook = new Workbook();
    Worksheet sheet = wookbook.Worksheets[0];
    
    //添加说明文字
    sheet.Range["B1"].Text = "文字对齐方式";
    sheet.Range["D1"].Text = "文字方向";
    sheet.Range["F1"].Text = "文字换行";
    sheet.Range["B1:F1"].Style.Font.IsBold = true;
    
    //左对齐
    sheet.Range["B3"].Text = "左对齐";
    sheet.Range["B3"].Style.HorizontalAlignment = HorizontalAlignType.Left;
    
    //水平居中
    sheet.Range["B4"].Text = "水平居中";
    sheet.Range["B4"].Style.HorizontalAlignment = HorizontalAlignType.Center;
    
    //右对齐
    sheet.Range["B5"].Text = "右对齐";
    sheet.Range["B5"].Style.HorizontalAlignment = HorizontalAlignType.Right;
    
    //居上
    sheet.Range["B7"].Text = "居上";
    sheet.Range["B7"].Style.VerticalAlignment = VerticalAlignType.Top;
    
    //居中
    sheet.Range["B8"].Text = "垂直居中";
    sheet.Range["B8"].Style.VerticalAlignment = VerticalAlignType.Center;
    
    //居下
    sheet.Range["B9"].Text = "居下";
    sheet.Range["B9"].Style.VerticalAlignment = VerticalAlignType.Bottom;
    
    //分散对齐并居中
    sheet.Range["B10"].Text = "水平分散对齐+垂直居中";
    sheet.Range["B10"].Style.HorizontalAlignment = HorizontalAlignType.Distributed;
    sheet.Range["B10"].Style.VerticalAlignment = VerticalAlignType.Center;
    
    //逆时针旋转45°
    sheet.Range["D7"].Text = "旋转45°";
    sheet.Range["D7"].Style.Rotation = 45;
    
    //逆时针旋转90°
    sheet.Range["D8"].Text = "旋转90°";
    sheet.Range["D8"].Style.Rotation = 90;
    
    //顺时针旋转45°
    sheet.Range["D9"].Text = "旋转-45°";
    sheet.Range["D9"].Style.Rotation = 135;
    
    //顺时针旋转90°
    sheet.Range["D10"].Text = "旋转-90°";
    sheet.Range["D10"].Style.Rotation = 180;
    
    //添加‘
    ’进行文字换行
    sheet.Range["F9"].Text = "这是
    手动
    换行";
    
    //自动换行
    sheet.Range["F10"].Text = "这是自动换行(额外测试文字)";
    sheet.Range["F10"].Style.WrapText = true;
    
    //设置列宽、行高
    sheet.Columns[1].ColumnWidth = 15;
    sheet.Columns[3].ColumnWidth = 15;
    sheet.Columns[5].ColumnWidth = 15;
    sheet.Range["B3:B5"].RowHeight = 15;
    sheet.Range["B7:B10"].RowHeight = 50;
    
    //保存文档
    wookbook.SaveToFile("TextAlignment.xlsx", FileFormat.Version2013);

    C# Excel 中设置文字对齐方式、方向和换行

  • 相关阅读:
    codevs 4511 信息传递(NOIP2015 day1 T2)
    caption标签,为表格添加标题和摘要
    用css样式,为表格加入边框
    table标签,认识网页上的表格
    认识div在排版中的作用
    使用ol,添加图书销售排行榜
    使用ul添加列表
    使用<pre>标签为你的网页加入大段代码
    想加入一行代码吗?使用<code>标签
    <address>标签,为网页加入地址信息
  • 原文地址:https://www.cnblogs.com/hEnius/p/10494506.html
Copyright © 2011-2022 走看看