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;

//添加‘\n’进行文字换行
sheet.Range["F9"].Text = "这是\n手动\n换行";

//自动换行
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);

原文地址:https://www.cnblogs.com/hEnius/p/10494506.html

时间: 2024-08-06 21:12:39

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

DatagridView列宽设置与对齐方式

一.设置对齐方式 1.列标题居中对齐 dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; 但实际上看上去仍然偏左,这是因为存在排序三角形,需要取消排序功能 2.内容居中对齐 dataGridView1.DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignme

在 Excel 中设置图片

package com.smbea.demo.excel; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStrea

uibutton 使用settitle后如何修改其中文字对齐方式

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];            btn.frame = CGRectMake(5, self.alertView.lableView.frame.size.height + self.checkBox.frame.size.height + 80.0f, self.localView.frame.size.width - self.localView.frame.size.width

Eclipse中设置编码的方式

Eclipse中设置编码的方式 如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文输出,则最好使 Java文件使用UTF-8编码.然而,Eclipse工 作空间(workspace)的缺省字符编码是操作系统缺省的编码,简体中文操作系统 (Windows XP.Windows 2000简体中文)的缺省编码是GB18030,在此工作空间中建立的工程编码是GB18030,工程中建立的java文件也是GB18030.如果要使 新建立工程.java文件直接使UTF-8则需要做以下工作: 1.

[WPF打印]WPF 文档元素(Run TextBlock Paragraph)的文字对齐方式

最近开发WPF程序,需要打印,用到了FlowDocument(这相当于有了打印模版,而且可以随时修改,真的是挺方便的).可是在输出表格形数据(这种情况恐怕是大多数~)时遇到了点儿麻烦. 由于TableCell里面不能直接使用Run或TextBlock,要添加Paragraph,这时候问题就来了,在Paragraph上面设置的TextAlignment属性,只有在文字内容直接写在Paragraph中时有效, 对于包含的TextBlock或者Run,即使你直接设置了他们的对齐方式也没有效果,内容一律

ObjectARX学习笔记(三十二)----如何设置AcDbMText对齐方式

//_T("\\pxql;") 居左 //_T("\\pxqr;") 居右 //_T("\\pxqc;") 居中 //_T("\\pxqd;") 分散对齐 //MtextInfoId 文字ID AcDbObjectPointer<AcDbMText> pEnt_1(MtextInfoId, AcDb::kForWrite); if (pEnt_1.openStatus() == Acad::eOk) { ACHAR

JQuery EasyUI之DataGrid列名和数据列分别设置不同对齐方式(转)

需求如下 现有数据列三列 Name,Age,CreateDate 数据 张三,18,2000-12-09 :12:34:56 李四,28,2000-12-09 :12:34:56 王麻子,38,2000-12-09 :12:34:56 Jquery Easyui DataGrid中列设置 { field: 'Name', title: '名称', width: 120 ,align:left},{field: 'Age', title: '年龄', width: 120 ,align:right

html&amp;css中的文字对齐问题

html&css的使用过程中,我们经常会遇到很多文字对齐问题.下面我要介绍一个有丁点难的文字对齐问题. 实现效果如下图所示. 两行长度不定的文字,要让它们总体水平居中,然后,这两行字要左对齐,如下图所示. 听起来好像挺简单,然而实现起来却...... 错误做法一: 用一个大块div包住两个div,在大div中设置"text-align:center; ",在小div中设置"text-align:left; " 结果如下图所示. 因为div会占据一整行,而不能

设置UIButton 对齐方式

设置UIButton上字体的对齐方式,不是用: [payWayButton.titleLabelsetTextAlignment:UITextAlignmentCenter]; 而是用: [payWayButtonsetContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];