C# 操作Excel属性

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.   
  3. range = (Range)worksheet.get_Range("A1", "E1");     //获取Excel多个单元格区域:本例做为Excel表头  
  4.   
  5. range.Merge(0);     //单元格合并动作  
  6.   
  7. worksheet.Cells[1, 1] = "Excel单元格赋值";     //Excel单元格赋值  
  8.   
  9. range.Font.Size = 15;     //设置字体大小  
  10.   
  11. range.Font.Underline=true;     //设置字体是否有下划线  
  12.   
  13. range.Font.Name="黑体";       设置字体的种类   
  14.   
  15. range.HorizontalAlignment=XlHAlign.xlHAlignCenter;     //设置字体在单元格内的对其方式  
  16.   
  17. range.ColumnWidth=15;     //设置单元格的宽度  
  18.   
  19. range.Cells.Interior.Color=System.Drawing.Color.FromArgb(255,204,153).ToArgb();     //设置单元格的背景色  
  20.   
  21. range.Borders.LineStyle=1;     //设置单元格边框的粗细  
  22.   
  23. range.BorderAround(XlLineStyle.xlContinuous,XlBorderWeight.xlThick,XlColorIndex.xlColorIndexAutomatic,System.Drawing.Color.Black.ToArgb());     //给单元格加边框  
  24.   
  25. range.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone; //设置单元格上边框为无边框  
  26.   
  27.     range.EntireColumn.AutoFit();     //自动调整列宽  
  28.   
  29. Range.HorizontalAlignment= xlCenter;     // 文本水平居中方式  
  30.   
  31. Range.VerticalAlignment= xlCenter     //文本垂直居中方式  
  32.   
  33. Range.WrapText=true;     //文本自动换行  
  34.   
  35. Range.Interior.ColorIndex=39;     //填充颜色为淡紫色  
  36.   
  37. Range.Font.Color=clBlue;     //字体颜色  
  38.   
  39. xlsApp.DisplayAlerts=false;     //保存Excel的时候,不弹出是否保存的窗口直接进行保存

 

range.NumberFormatLocal = "@"; //设置单元格格式为文本 range = (Range)worksheet.get_Range("A1", "E1"); //获取Excel多个单元格区域:本例做为Excel表头 range.Merge(0); //单元格合并动作 worksheet.Cells[1, 1] = "Excel单元格赋值"; //Excel单元格赋值 range.Font.Size = 15; //设置字体大小 range.Font.Underline=true; //设置字体是否有下划线 range.Font.Name="黑体"; 设置字体的种类 range.HorizontalAlignment=XlHAlign.xlHAlignCenter; //设置字体在单元格内的对其方式 range.ColumnWidth=15; //设置单元格的宽度 range.Cells.Interior.Color=System.Drawing.Color.FromArgb(255,204,153).ToArgb(); //设置单元格的背景色 range.Borders.LineStyle=1; //设置单元格边框的粗细 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=39; //填充颜色为淡紫色 Range.Font.Color=clBlue; //字体颜色 xlsApp.DisplayAlerts=false; //保存Excel的时候,不弹出是否保存的窗口直接进行保存 ==================================================================== using System; using System.Collections.Generic; using System.Text; using System.Reflection; using System.Runtime.InteropServices; using Microsoft.Office.Interop.Excel; using ExcelApplication = Microsoft.Office.Interop.Excel.ApplicationClass; using System.IO; namespace ExcalDemo { public class ExcelFiles { public void CreateExcelFiles() { ExcelApplication excel = new ExcelApplication(); try { excel.Visible = false;// 不显示 Excel 文件,如果为 true 则显示 Excel 文件 excel.Workbooks.Add(Missing.Value);// 添加工作簿 Worksheet sheet = (Worksheet)excel.ActiveSheet;// 获取当前工作表 Range range = null;// 创建一个空的单元格对象 range = sheet.get_Range("A1", Missing.Value);// 获取单个单元格 range.RowHeight = 20; // 设置行高 range.ColumnWidth = 20; // 设置列宽 range.Borders.LineStyle = 1; // 设置单元格边框 range.Font.Bold = true; // 加粗字体 range.Font.Size = 20; // 设置字体大小 range.Font.ColorIndex = 5; // 设置字体颜色 range.Interior.ColorIndex = 6; // 设置单元格背景色 range.HorizontalAlignment = XlHAlign.xlHAlignCenter;// 设置单元格水平居中 range.VerticalAlignment = XlVAlign.xlVAlignCenter;// 设置单元格垂直居中 range.Value2 = "设置行高和列宽";// 设置单元格的值 range = sheet.get_Range("B2", "D4");// 获取多个单元格 range.Merge(Missing.Value); // 合并单元格 range.Columns.AutoFit(); // 设置列宽为自动适应 range.NumberFormatLocal = "#,##0.00";// 设置单元格格式为货币格式 // 设置单元格左边框加粗 range.Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick; // 设置单元格右边框加粗 range.Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick; range.Value2 = "合并单元格"; // 页面设置 sheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4; // 设置页面大小为A4 sheet.PageSetup.Orientation = XlPageOrientation.xlPortrait; // 设置垂直版面 sheet.PageSetup.HeaderMargin = 0.0; // 设置页眉边距 sheet.PageSetup.FooterMargin = 0.0; // 设置页脚边距 sheet.PageSetup.LeftMargin = excel.InchesToPoints(0.354330708661417); // 设置左边距 sheet.PageSetup.RightMargin = excel.InchesToPoints(0.354330708661417);// 设置右边距 sheet.PageSetup.TopMargin = excel.InchesToPoints(0.393700787401575); // 设置上边距 sheet.PageSetup.BottomMargin = excel.InchesToPoints(0.393700787401575);// 设置下边距 sheet.PageSetup.CenterHorizontally = true; // 设置水平居中 // 打印文件 sheet.PrintOut(Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); // 保存文件到程序运行目录下 sheet.SaveAs(Path.Combine(System.Windows.Forms.Application.StartupPath,"demo.xls"), Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); excel.ActiveWorkbook.Close(false, null, null); // 关闭 Excel 文件且不保存 } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { excel.Quit(); // 退出 Excel excel = null; // 将 Excel 实例设置为空 } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值