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 = ; //ColumnWidth "A:B"表示第一列和第二列, "A:A"表示第一列
((Excel.Range)_workSheet.Columns["A:B", System.Type.Missing]).ColumnWidth = ; // EXCEL操作(需要冻结的字段 按住ALT+W 再按F)
Excel.Range excelRange = _workSheet .get_Range(_workSheet .Cells[, ], _workSheet .Cells[, ]);
excelRange.Select();
excelApplication.ActiveWindow.FreezePanes = true; //Borders.LineStyle 单元格边框线
Excel.Range excelRange = _workSheet.get_Range(_workSheet.Cells[, ], _workSheet.Cells[, ]);
//单元格边框线类型(线型,虚线型)
excelRange.Borders.LineStyle = ;
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 =; //设置字体大小
excelRange.Font.Size = ;
//设置字体是否有下划线
excelRange.Font.Underline = true; //设置字体在单元格内的对其方式
excelRange.HorizontalAlignment = XlHAlign.xlHAlignCenter;
//设置单元格的宽度
excelRange.ColumnWidth = ;
//设置单元格的背景色
excelRange.Cells.Interior.Color = System.Drawing.Color.FromArgb(, , ).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 = ; //合并单元格
excelRange.Merge(excelRange.MergeCells);
_workSheet.get_Range("A15", "B15").Merge(_workSheet.get_Range("A15", "B15").MergeCells);

附颜色枚举表:

 /// <summary>
/// 常用颜色定义,对就Excel中颜色名
/// </summary>
public enum ColorIndex
{
无色 = -, 自动 = -, 黑色 = , 褐色 = , 橄榄 = , 深绿 = , 深青 = ,
深蓝 = , 靛蓝 = , 灰色80 = , 深红 = , 橙色 = , 深黄 = , 绿色 = ,
青色 = , 蓝色 = , 蓝灰 = , 灰色50 = , 红色 = , 浅橙色 = , 酸橙色 = ,
海绿 = , 水绿色 = , 浅蓝 = , 紫罗兰 = , 灰色40 = , 粉红 = ,
金色 = , 黄色 = , 鲜绿 = , 青绿 = , 天蓝 = , 梅红 = , 灰色25 = ,
玫瑰红 = , 茶色 = , 浅黄 = , 浅绿 = , 浅青绿 = , 淡蓝 = , 淡紫 = ,
白色 =
}

说明:

 range.NumberFormatLocal = "@";     //设置单元格格式为文本
range = (Range)worksheet.get_Range("A1", "E1"); //获取Excel多个单元格区域:本例做为Excel表头
range.Merge(); //单元格合并动作
worksheet.Cells[, ] = "Excel单元格赋值"; //Excel单元格赋值
range.Font.Size = ; //设置字体大小
range.Font.Underline=true; //设置字体是否有下划线
range.Font.Name="黑体"; 设置字体的种类
range.HorizontalAlignment=XlHAlign.xlHAlignCenter; //设置字体在单元格内的对其方式
range.ColumnWidth=; //设置单元格的宽度
range.Cells.Interior.Color=System.Drawing.Color.FromArgb(,,).ToArgb(); //设置单元格的背景色
range.Borders.LineStyle=; //设置单元格边框的粗细
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=; //填充颜色为淡紫色
Range.Font.Color=clBlue; //字体颜色
xlsApp.DisplayAlerts=false; //保存Excel的时候,不弹出是否保存的窗口直接进行保存

C#操作EXCEL常见操作集合(行高,列宽,合并单元格,单元格边框线)的更多相关文章

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

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

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

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

  3. TableLayoutPanel 行高列宽设置

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

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

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

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

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

  6. VBS操作Excel常见方法

    VBS操作Excel常见方法 作者: 字体:[增加 减小] 类型:转载 时间:2009-11-13我要评论 VBS控制Excel常见方法,需要的朋友可以参考下. dim oExcel,oWb,oShe ...

  7. vbscript 中对excel常见操作

    vbs 对excel的操作 删除.修改单元格,设置字体.背景色dim oExcel,oWb,oSheet Set oExcel= CreateObject("Excel.Applicatio ...

  8. C# 操作Excel常见错误

    1.未在本地注册 string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FilePath + &qu ...

  9. C#中操作刚导出的Excel,设置其为自动调整列宽

    [问题] 用C#导出数据为excel,但是导出的数据中,不是自动调整列宽的: 希望变成这样的: [解决过程] 1.参考: 在C#里对excel文件的列宽进行操作 去试试: //auto adjust ...

随机推荐

  1. mycat 离散分片 -&gt; 程序指定分区的分片

    1.程序指定分区的分片 此规则是在运行阶段有应用自主决定路由到那个分片. 此方法为直接依据字符子串(必须是数字)计算分区号(由应用传递參数.显式指定分区号). 2,加入配置文件 在function.x ...

  2. 苹果浏览器Safari对html标签submit按钮的默认渲染

    -webkit-appearance: none; 上面的设置就告诉Safari不要使用默认渲染,使用我们写好的 有这么一个webkit的私有属性: -webkit-appearance:none;  ...

  3. 【b802】火柴棒等式

    Time Limit: 1 second Memory Limit: 50 MB [问题描述] 给你n根火柴棍,你可以拼出多少个形如"A+B=C"的等式?等式中的A.B.C是用火柴 ...

  4. [Angular] Auxiliary named router outlets

    Define a auxilliary router: export const ROUTES: Routes = [ { path: 'folder/:name', component: MailF ...

  5. https://github.com/zhangxianyi/Source-Insight-Plugin

    https://github.com/zhangxianyi/Source-Insight-Plugin

  6. 【47.95%】【codeforces 554C】Kyoya and Colored Balls

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. OK6410开发板系统安装---嵌入式回归第二篇

    1. 嵌入式系统须要被安装到开发板的nandflash上 或者 SD卡上 2. 须要安装的嵌入式Linux系统能够分三个部分 watermark/2/text/aHR0cDovL2Jsb2cuY3Nk ...

  8. 如何将服务器传来的字符串转成HTML显示在前端页面

    从后台返回的字符串是一段HTML源代码,如果不做处理,直接插入前端页面中 ,会显示为字符: 现需求为:将后台返回的字符串str以HTML显示在div中: <div id='container'& ...

  9. android生成分享长图而且加入全图水印

    尊重他人的劳动成果.转载请标明出处:http://blog.csdn.net/gengqiquan/article/details/65938021. 本文出自:[gengqiquan的博客] 领导近 ...

  10. Expression Blend 的点滴(4)--创建类似iPhone屏幕锁控件(下)

    原文:Expression Blend 的点滴(4)--创建类似iPhone屏幕锁控件(下) 接着上篇... 接下去,将一步步演示如果创建当点击checkBox后,其中的按钮由左边滑动到右边,表示处于 ...