返回顶部

收藏

常用的 C# 设置 Excel 单元格的方法

更多

例如设置单元格颜色、添加或者删除sheet、重命名、行的添加删除等

[C#]代码

using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
using System.IO;
using System.Reflection;

Application app = new Application();
Workbooks wbks = app.Workbooks;
_Workbook _wbk = wbks.Add(xxx);

//Open an exist Excel file, please replace “xxx” with the excel file path. 

//Create a new Excel file, please replace “xxx” with “true”. 
//Note: there is only one worksheet in the excel file.

Sheets shs = _wbk.Sheets;

//Left side boder line
_wsh.get_Range(
_wsh.Cells[2, 1], _wsh.Cells[2, 2])
.Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//

//Right side border line
_wsh.get_Range(
_wsh.Cells[2, 1], _wsh.Cells[2, 2])
.Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//

//Upside border line
_wsh.get_Range(
_wsh.Cells[2, 1], _wsh.Cells[2, 2])
.Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//下

//Downside border line
_wsh.get_Range(
_wsh.Cells[2, 1], _wsh.Cells[2, 2])
.Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;

标签:Excel,c#

收藏

1人收藏

支持

0

反对

0

发表评论