一、对行高,列宽、单元格边框等的设置

这篇简短的文字对单元格的操作总结的比较全面,特此转载过来。

private _Workbook _workBook = null;
private Worksheet _workSheet = null;
private Excel.Application _excelApplicatin = null; _excelApplicatin = new Excel.Application();
_excelApplicatin.Visible = true;
_excelApplicatin.DisplayAlerts = true; _workBook = _excelApplicatin.Workbooks.Add(XlSheetType.xlWorksheet);
_workSheet = (Worksheet)_workBook.ActiveSheet;
_workSheet.Name = "workSheetName"; //打开已存在的Excel
string strExcelPathName = AppDomain.CurrentDomain.BaseDirectory + "excelSheetName.xls";
Excel.Workbook workBook = application.Workbooks.Open(strExcelPathName, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

//读取已打开的Excel
Excel.Worksheet workSheet1 = (Excel.Worksheet)workBook.Sheets["SheetName1"];
Excel.Worksheet workSheet2 = (Excel.Worksheet)workBook.Sheets["SheetName2"]; //添加一个workSheet
Worksheet workSheet = (Worksheet)workBook.Worksheets.Add(System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing); //RowHeight "1:1"表示第一行, "1:2"表示,第一行和第二行
((Excel.Range)_workSheet.Rows["1:1", System.Type.Missing]).RowHeight = 100; //ColumnWidth "A:B"表示第一列和第二列, "A:A"表示第一列
((Excel.Range)_workSheet.Columns["A:B", System.Type.Missing]).ColumnWidth = 10; // EXCEL操作(需要冻结的字段 按住ALT+W 再按F)
Excel.Range excelRange = _workSheet .get_Range(_workSheet .Cells[10, 5], _workSheet .Cells[10, 5]);
excelRange.Select();
excelApplication.ActiveWindow.FreezePanes = true; //Borders.LineStyle 单元格边框线
Excel.Range excelRange = _workSheet.get_Range(_workSheet.Cells[2, 2], _workSheet.Cells[4, 6]);
//单元格边框线类型(线型,虚线型)
excelRange.Borders.LineStyle = 1;
excelRange.Borders.get_Item(XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlLineStyle.xlContinuous;
//指定单元格下边框线粗细,和色彩
excelRange.Borders.get_Item(XlBordersIndex.xlEdgeBottom).Weight = Excel.XlBorderWeight.xlMedium; excelRange.Borders.get_Item(XlBordersIndex.xlEdgeBottom).ColorIndex =3; //设置字体大小
excelRange.Font.Size = 15;
//设置字体是否有下划线
excelRange.Font.Underline = true; //设置字体在单元格内的对其方式
excelRange.HorizontalAlignment = XlHAlign.xlHAlignCenter;
//设置单元格的宽度
excelRange.ColumnWidth = 15;
//设置单元格的背景色
excelRange.Cells.Interior.Color = System.Drawing.Color.FromArgb(255, 204, 153).ToArgb();
// 给单元格加边框
excelRange.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThick,
XlColorIndex.xlColorIndexAutomatic, System.Drawing.Color.Black.ToArgb());
//自动调整列宽
excelRange.EntireColumn.AutoFit();
// 文本水平居中方式
excelRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
//文本自动换行
excelRange.WrapText = true;
//填充颜色为淡紫色
excelRange.Interior.ColorIndex = 39; //合并单元格
excelRange.Merge(excelRange.MergeCells);
_workSheet.get_Range("A15", "B15").Merge(_workSheet.get_Range("A15", "B15").MergeCells); /// <summary>
/// 常用颜色定义,对就Excel中颜色名
/// </summary>
public enum ColorIndex
{
无色 = -4142, 自动 = -4105, 黑色 = 1, 褐色 = 53, 橄榄 = 52, 深绿 = 51, 深青 = 49,
深蓝 = 11, 靛蓝 = 55, 灰色80 = 56, 深红 = 9, 橙色 = 46, 深黄 = 12, 绿色 = 10,
青色 = 14, 蓝色 = 5, 蓝灰 = 47, 灰色50 = 16, 红色 = 3, 浅橙色 = 45, 酸橙色 = 43,
海绿 = 50, 水绿色 = 42, 浅蓝 = 41, 紫罗兰 = 13, 灰色40 = 48, 粉红 = 7,
金色 = 44, 黄色 = 6, 鲜绿 = 4, 青绿 = 8, 天蓝 = 33, 梅红 = 54, 灰色25 = 15,
玫瑰红 = 38, 茶色 = 40, 浅黄 = 36, 浅绿 = 35, 浅青绿 = 34, 淡蓝 = 37, 淡紫 = 39,
白色 = 2
}

Code segment2

    range.NumberFormatLocal = "@";     //设置单元格格式为文本
range = (Range)worksheet.get_Range("A1", "E1"); //获取Excel多个单元格区域:本例做为Excel表头
range.Merge(0); //单元格合并动作
worksheet.Cells[1, 1] = "Excel单元格赋值"; //Excel单元格赋值
range.Font.Size = 15; //设置字体大小
range.Font.Underline=true; //设置字体是否有下划线
range.Font.Name="黑体"; 设置字体的种类
range.HorizontalAlignment=XlHAlign.xlHAlignCenter; //设置字体在单元格内的对其方式
range.ColumnWidth=15; //设置单元格的宽度
range.Cells.Interior.Color=System.Drawing.Color.FromArgb(255,204,153).ToArgb(); //设置单元格的背景色
range.Borders.LineStyle=1; //设置单元格边框的粗细
range.BorderAround(XlLineStyle.xlContinuous,XlBorderWeight.xlThick,XlColorIndex.xlColorIndexAutomatic,System.Drawing.Color.Black.ToArgb()); //给单元格加边框
range.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone; //设置单元格上边框为无边框
range.EntireColumn.AutoFit(); //自动调整列宽
Range.HorizontalAlignment= xlCenter; // 文本水平居中方式
Range.VerticalAlignment= xlCenter //文本垂直居中方式
Range.WrapText=true; //文本自动换行
Range.Interior.ColorIndex=39; //填充颜色为淡紫色
Range.Font.Color=clBlue; //字体颜色
xlsApp.DisplayAlerts=false; //保存Excel的时候,不弹出是否保存的窗口直接进行保存

Excel Interior.ColorIndex色彩列表

对用的Interior.ColorIndex色彩列表。

在C# 对Excel编程中的使用:

 for (int i = 5; i < countRow + 1; i++)
{
string strKB = null;
Microsoft.Office.Interop.Excel.Range kbrng = (Microsoft.Office.Interop.Excel.Range)sheSource.Cells[i, 1];
if (kbrng.Value2 != null)
strKB = kbrng.Value2.ToString();
if (strTotal.Contains(strKB))
kbrng.Interior.ColorIndex = 3;
}

二、常见的Excel单元格格式设置内容

2.1 数字(Range.NumberFormatlocal 属性)

常规:

Range.NumberFormatlocal = "G/通用格式"

数值:

Range.NumberFormatlocal = "0.000_" --保留小 数位数为3  (此处“_”表示:留下一个与下一个字符同等宽度的空格)

Range.NumberFormatlocal = "0" --不要小数

Range.NumberFormatlo cal = "#,##0.000" --保留小数位数为3,并使用千位分隔符
货币:

Range.NumberFormatlocal
= "$#,##0.000"
百分比:

Range.NumberFormatlocal
= "0.000%"
分数:

Range.NumberFormatlocal
= "# ?/?"
科学计数:

Range.NumberFormatlocal
= "0.00E+00"

文本

Range.NumberFormatlocal
= "@"

特殊:

Range.NumberFormatlocal = "000000"---邮政编码
Range.NumberFormatlocal = "[DBNum1]G/通用格式"---中文小写数字
Range.NumberFormatlocal = "[DBNum2]G/通用格式"---中文大写数字
Range.NumberFormatlocal = "[DBNum2][$RMB]G/通用格式"---人民币大写

2.2 对齐

水平对齐:Range.HorizontalAlignment =
etHAlignCenter  ---居中
垂 直对齐:Range.VerticalAlignment = etVAlignCenter---居中
是否自动换行:Range.WrapText = True
是否缩小字体填充:Range.ShrinkToFit = True
是否合并单元格:Range.MergeCells = False
文字竖排:Range.Orientation = etVertical
文字倾斜度数:Range.Orientation = 45 -----倾斜45度
字体(Font对象)
字体名称:Font.Name = "华文行楷"
字形: Font.FontStyle = "常规"
字号:Font.Size = "10"
下划线:Font.Strikethrough = True; Font.Underline =
etUnderlineStyleDouble ---双下划线
上标:Font.Superscript = True
下 标:Font.SubScript = True
删除线:Font.OutlineFont = True

2.3 边框(Borders对象)

Borders.Item(etEdgeTop):上边框
Borders.Item(etEdgeLeft):左边框
Borders.Item (etEdgeRight):右边框
Borders.Item(etEdgeBottom):下边框
Borders.Item(etDiagonalDown) :左上--右下边框
Borders.Item(etDiagonalUp):左下--右上边框
Border.LineStyle = etContinuous 线条样式

参考博文

1. Herbert, C# Excel 行高,列宽,合并单元格,单元格边框线,冻结

2. zhpingyun008, C#设置EXCEL单元格格式.

C# 对Excel 单元格格式, 及行高、 列宽、 单元格边框线、 冻结设置的更多相关文章

  1. C#操作EXCEL常见操作集合(行高,列宽,合并单元格,单元格边框线)

    private _Workbook _workBook = null; private Worksheet _workSheet = null; private Excel.Application _ ...

  2. 27.openpyxl 向指定单元格添加图片并修改图片大小 以及修改单元格行高列宽

    openpyxl 向指定单元格添加图片并修改图片大小 以及修改单元格行高列宽 from openpyxl import Workbook,load_workbook from openpyxl.dra ...

  3. Datagridview 在基于文本的单元格中启用换行,自动调整行高列宽

    将 DataGridViewCellStyle的 WrapMode 属性设置为 DataGridViewTriState 枚举值之一.下面的代码示例使用 System.Windows.Forms.Da ...

  4. datagridview 行高列宽的自动设置

    1) 设定行高和列宽自动调整 [C#]// 设定包括Header和所有单元格的列宽自动调整 DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSi ...

  5. TableLayoutPanel 行高列宽设置

    /// <summary> /// 获取TableLayoutPanel指定行的高度 /// </summary> /// <param name="layou ...

  6. 20170707xlVBA多区域拆分多表保持行高列宽

    Public Sub 多个区域拆分到多表() AppSettings On Error GoTo ErrHandler Dim StartTime, UsedTime As Variant Start ...

  7. GRIDVIEW多行多列合并单元格(合并列)

    GitHub项目地址:https://github.com/mingceng/merge-gridviewcell 去年的时候,我写了两篇文章:  GridView多行多列合并单元格(完整代码和例子) ...

  8. ABAP ALV 颜色设置(行,列,单元格)

    BCALV_EDIT_03 http://blog.sina.com.cn/s/blog_a87b19300102who3.html 关于ALV表格颜色,这种需求在项目中会经常用到. 列颜色 列的颜色 ...

  9. DataGridView合并单元格(多行多列合并)

    一.点击在拖入的显示控件(TreeList)右上方的箭头,在Treelist任务中选择数据源,添加项目数据源,依次选择数据库.数据集,新建连接,浏览选择数据库(*.mdb),依次点击 下一步,选择“表 ...

随机推荐

  1. mp3 音频 音乐 tag ID3 ID3V1 ID3V2 标签 读取信息 获得图片 jpeg bmp 图片转换等

    mp3 音频 音乐 tag ID3 ID3V1 ID3V2 标签 读取信息 获得图片 jpeg bmp 图片转换(上) MP3文件格式(二)---ID3v2 图:ID3V1标签结构 图:ID3V2标签 ...

  2. Linux Shell学习

    https://yunpan.cn/cMxw3i8TkcsWI (提取码:d4e1)

  3. JavaWeb项目开发案例精粹-第6章报价管理系统-03Dao层

    1. package com.sanqing.dao; import java.io.Serializable; import java.util.LinkedHashMap; import com. ...

  4. SpringMVC 中的Interceptor 拦截器

    1.配置拦截器 在springMVC.xml配置文件增加: <mvc:interceptors>  <!-- 日志拦截器 -->  <mvc:interceptor> ...

  5. windows 7 ssh server for scp

    Software: BvSshServe. (个人用免费,商业收费) scp localfile.txt user_tst@11.111.12.170:'E:\downloads\SSH\auto.p ...

  6. psycopg2.pool – Connections pooling / psycopg2.pool – 连接池 / postgresql 连接池

    创建新的PostgreSQL连接可以是一个昂贵的操作.这个模块提供了一些纯Python类直接在客户端应用程序实现简单的连接池.      class psycopg2.pool.AbstractCon ...

  7. MapReduce读取hdfs上文件,建立词频的倒排索引到Hbase

    Hdfs上的数据文件为T0,T1,T2(无后缀): T0: What has come into being in him was life, and the life was the light o ...

  8. Linux系统信息查看命令

    一.系统 # uname -a #查看内核/操作系统/CPU信息 # head -n 1 /etc/issue #查看操作系统版本 # cat /proc/cpuinfo #查看CPU信息 # hos ...

  9. hadoop优点和缺点

    l扩容能力(Scalable):能可靠地(reliably)存储和处理千兆字节(PB)数据. l成本低(Economical):可以通过普通机器组成的服务器群来分发以及处理数据.这些服务器群总计可达数 ...

  10. 安装SQL2008的时候 出现System.Configuration.ConfigurationErrorsException: 创建 userSettings/Microsoft.SqlServe

    System.Configuration.ConfigurationErrorsException: 创建 userSettings/Microsoft.SqlServer.Configuration ...