关于excel操作的一些记录
作者:互联网
根据index获取列编号,如传入1,得到A
public string Getzm(int Index)
{
var dividend = Index;
string columnName = string.Empty;
while (dividend > 0)
{
var modulo = (dividend - 1) % 26;
columnName = Convert.ToChar(65 + modulo) + columnName;
dividend = (dividend - modulo) / 26;
}
return columnName;
}
合并单元格
ExcelWorksheet ws
ws.Cells[$"{startName}:{endName}"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
ws.Cells[$"{startName}:{endName}"].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
ws.Cells[$"{startName}:{endName}"].Merge = true;
注意,必须用ws.Cells[$"A1:C1"]这种方式而不是ws.Cells[1,1,1,3]这种,会报错
设置单元格格式
var cell = ws.Cells[$"{startName}:{endName}"]
cell.Style.Numberformat.Format = "yyyy-m-d";
标签:记录,Cells,excel,columnName,ws,endName,操作,dividend,startName 来源: https://www.cnblogs.com/jiaoyi1e/p/15739415.html