您的位置:首页 > 编程语言 > C#

c# 操作excle

2015-11-12 14:57 513 查看
添加引用 Microsoft.Office.Interop.Excel;

添加命名空间 using Excel = Microsoft.Office.Interop.Excel;

//创建接口变量------------------------------------------

_Workbook _xlWorkBook = null;
Worksheet _xlWorkSheet = null;
Excel.Application _xlApp = null;


//创建excle Application----------------------------------

_xlApp= new Excel.Application();
//_xlApp.DisplayAlerts = false; //设置报警窗口
_xlApp.Visible = true;             //设置显示
// _xlApp.ScreenUpdating = false; //设置屏幕刷新


//创建workbook ----------------------------------------

//打开已存在的workbook  path是文件路径
_xlWorkBook = _xlApp.Workbooks.Open(path, System.Type.Missing, System.Type.Missing, System.Type.Missing,System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);


//创建worksheet--------------------------------------------

//得到指定的sheet

//Excel.Worksheet xlsWorkSheet = (Worksheet)xlsWorkBook.Worksheets["2013年"];
_xlWorkSheet = (Worksheet)_xlWorkBook.Sheets["2013年"];//得到指定的sheet
//_xlWorkSheet =(Worksheet) _xlWorkBook.ActiveSheet;//得到当前活跃sheet


//获取所有sheet

Sheets xlsWorkSheets = _xlWorkBook.Worksheets;
foreach (var s in xlsWorkSheets)
{
Worksheet xlsWorkSheet = s as Worksheet;
string ss= xlsWorkSheet.Name;
}


//增加sheet页

// Worksheet workSheet = (Worksheet)_xlWorkBook.Worksheets.Add(System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);


//创建Excel.Range----------------------------------------------------- 

//获取单元格内容
var cell= (Range)_xlWorkSheet.Cells[1, 3];
var cellvalue = cell.Value.ToString();
string pathd = @"c:\wellname.txt";
_xlWorkSheet.Hyperlinks.Add(cell, pathd); //添加超链接


//RowHeight   "1:1"表示第一行, "1:2"表示,第一行和第二行
((Excel.Range)_xlWorkSheet.Rows["1:1", System.Type.Missing]).RowHeight = 100;
//ColumnWidth "A:B"表示第一列和第二列, "A:A"表示第一列
((Excel.Range)_xlWorkSheet.Columns["A:B", System.Type.Missing]).ColumnWidth = 10;
Excel.Range excelRange = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[10, 5], _xlWorkSheet.Cells[10, 5]);
excelRange.Select(); //选中区域
_xlApp.ActiveWindow.FreezePanes = true;//冻结字段
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.Merge(excelRange.MergeCells);
_xlWorkSheet.get_Range("A15", "B15").Merge(_xlWorkSheet.get_Range("A15", "B15").MergeCells);


  

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: