C# Excel行高、列宽、合并单元格、单元格边框线、冻结
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
}
- range.NumberFormatLocal = "@"; //设置单元格格式为文本
- range = (Range)worksheet.get_Range("A1", "E1"); //获取Excel多个单元格区域:本例做为Excel表头
- range.Merge(0); //单元格合并动作
- worksheet.Cells[1, 1] = "Excel单元格赋值"; //Excel单元格赋值
- range.Font.Size = 15; &
C# Excel行高、列宽、合并单元格、单元格边框线、冻结的更多相关文章
- C#操作EXCEL常见操作集合(行高,列宽,合并单元格,单元格边框线)
private _Workbook _workBook = null; private Worksheet _workSheet = null; private Excel.Application _ ...
- 27.openpyxl 向指定单元格添加图片并修改图片大小 以及修改单元格行高列宽
openpyxl 向指定单元格添加图片并修改图片大小 以及修改单元格行高列宽 from openpyxl import Workbook,load_workbook from openpyxl.dra ...
- Datagridview 在基于文本的单元格中启用换行,自动调整行高列宽
将 DataGridViewCellStyle的 WrapMode 属性设置为 DataGridViewTriState 枚举值之一.下面的代码示例使用 System.Windows.Forms.Da ...
- datagridview 行高列宽的自动设置
1) 设定行高和列宽自动调整 [C#]// 设定包括Header和所有单元格的列宽自动调整 DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSi ...
- 20170707xlVBA多区域拆分多表保持行高列宽
Public Sub 多个区域拆分到多表() AppSettings On Error GoTo ErrHandler Dim StartTime, UsedTime As Variant Start ...
- TableLayoutPanel 行高列宽设置
/// <summary> /// 获取TableLayoutPanel指定行的高度 /// </summary> /// <param name="layou ...
- Excel 如何自动调整列宽?
excel如何自动调整列宽 1.打开Excel表格,选中要调整的表格. 2.点击"格式",选择"自动调整列宽",右键点击"设置单元格格式" ...
- C#设置Excel行高、列宽
设置固定值 worksheet.Columns[1].ColumnWidth = 15; 设置自动换行 worksheet.Columns.WrapText = true; 设置自动行高.列宽 xlA ...
- excel制作田字格,excel行高磅,列宽1/10英寸;
打开一个excel表格,发现列宽是行高的4倍: 开始-格式中查看,发现行高14.25磅,列宽8.38*1/10英寸: 网上百度,了解1英寸=72磅: 那么列宽8.38=60.336磅: 60.336英 ...
随机推荐
- TCP回射服务器修订版(ubuntu 18.04)
一.需求 把https://www.cnblogs.com/soldierback/p/10673345.html中的TCP回射服务器程序重写成使用select来处理任意个客户的单进程 程序,而不是为 ...
- mysql 约束和外键约束实例
1.约束保证数据的完整性和一致性. 2.约束分为表级约束和列级约束.(根据约束所针对的字段的数目的多少来决定) 列级约束:对一个数据列建立的约束 表级约束:对多个数据列建立的约束 列级约束即可以在列定 ...
- transition的属性变化
链接:https://www.cnblogs.com/yehui-mmd/p/5934157.html css3——transition属性和opacity属性 [transition-durat ...
- Kaldi中的Chain模型
Chain模型的训练流程 链式模型的训练过程是MMI的无网格的版本,从音素级解码图生成HMM,对其使用前向后向算法,获得分母状态后验,通过类似的方式计算分子状态后验,但限于对应于转录的序列. 对于神经 ...
- Tomcat多应用启动报错:org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading Illegal access: this web application instance has been stopped already. Could not load [].
Loaded org.apache.tomcat.util.net.NioBlockingSelector$BlockPoller$RunnableRemove from .M22/lib/tomca ...
- Python之List列表的循环和切片
一.循环(for):输出列表中的每一个元素 stus=['杨静','王志华','王银梅','乔美玲'] #一个个输出列表元素 for s in stus: print('s 是 %s'%s) s 是 ...
- 织梦自定义表单ajax提交范例
function add_ajaxmessage(){ var dh = document.getElementById("tel"); //表单验证 if($("#te ...
- 【页面加载】【九九乘法表】【document.write的功能_】【<script>直接显示数组】【声明新变量】
1.页面加载时向body加载文本.弹出框 <body> <script> document.write("<h1>Ja ...
- Mybatis(二)入门程序-通过id查找用户、模糊查找用户、添加用户、删除用户
根据下图myBatis的架构,创建一个使用MyBatis的工程. 一.配置MyBatis 环境(如图) 1.sqlMapConfig.xml 首先,导入jar包(上图右边)并加载路径,然后 ...
- pythonのscrapy抓取网站数据
(1)安装Scrapy环境 步骤请参考:https://blog.csdn.net/c406495762/article/details/60156205 需要注意的是,安装的时候需要根据自己的pyt ...