Home > Backend Development > C#.Net Tutorial > C# operation EXCEL detailed code introduction

C# operation EXCEL detailed code introduction

黄舟
Release: 2017-03-02 11:39:21
Original
1945 people have browsed it

EXCEL operation method

private Excel.Application _excelApplicatin = null;
_excelApplicatin = new Excel.Application();
_excelApplicatin.Visible = true;
_excelApplicatin.DisplayAlerts = true;

private _Workbook _workBook = null;
_workBook = _excelApplicatin.Workbooks.Add(XlSheetType.xlWorksheet);//也可以打开现有的EXCEL文件

private Worksheet _workSheet = null;
_workSheet = (Worksheet)_workBook.ActiveSheet;
_workSheet.Name = "workSheetName";

//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]);//也可以A1:F1选择第一行的1-6列
            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
}
Copy after login

The above is the detailed code introduction of C# operating EXCEL. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template