網上看到的比較全的關於C#操作EXCEL常見操作集合,比較全,寫的不錯

來源:互聯網
上載者:User

原文地址:http://hi.baidu.com/kjkj911/blog/item/0ecc3ec7855dd6d4d100600f.html

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}

 

  1. range.NumberFormatLocal = "@";     //設定儲存格格式為文本
  2. range = (Range)worksheet.get_Range("A1", "E1");     //擷取Excel多個儲存格範圍:本例做為Excel表頭
  3. range.Merge(0);     //儲存格合并動作
  4. worksheet.Cells[1, 1] = "Excel儲存格賦值";     //Excel儲存格賦值
  5. range.Font.Size = 15;     //設定字型大小
  6. range.Font.Underline=true;     //設定字型是否有底線
  7. range.Font.Name="黑體";       設定字型的種類   
  8. range.HorizontalAlignment=XlHAlign.xlHAlignCenter;     //設定字型在儲存格內的對其方式
  9. range.ColumnWidth=15;     //設定儲存格的寬度
  10. range.Cells.Interior.Color=System.Drawing.Color.FromArgb(255,204,153).ToArgb();     //設定儲存格的背景色
  11. range.Borders.LineStyle=1;     //設定儲存格邊框的粗細
  12. range.BorderAround(XlLineStyle.xlContinuous,XlBorderWeight.xlThick,XlColorIndex.xlColorIndexAutomatic,System.Drawing.Color.Black.ToArgb());     //給儲存格加邊框
  13. range.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone; //設定儲存格上邊框為無邊框
  14.     range.EntireColumn.AutoFit();     //自動調整列寬
  15. Range.HorizontalAlignment= xlCenter;     // 文本水平置中方式
  16. Range.VerticalAlignment= xlCenter     //文本垂直置中方式
  17. Range.WrapText=true;     //文本自動換行
  18. Range.Interior.ColorIndex=39;     //填充顏色為淡紫色
  19. Range.Font.Color=clBlue;     //字型顏色
  20. xlsApp.DisplayAlerts=false;     //儲存Excel的時候,不彈出是否儲存的視窗直接進行儲存
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.