c# 操作excle
生活随笔
收集整理的這篇文章主要介紹了
c# 操作excle
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
添加引用 ?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);
轉載于:https://www.cnblogs.com/crhdyl/p/4959029.html
總結
以上是生活随笔為你收集整理的c# 操作excle的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 构造不可变类及其优点
- 下一篇: Educational Codeforc