EPPlus单元格和样式(含文字竖排)
using (var package = new ExcelPackage())
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet1");
//1,赋值
worksheet.Cells[int row, int col].Value = "xxx";
//或者
worksheet.Cells["A1"].Value = "xxx";
//或者
worksheet.SetValue(row,col,value);
//2,单元格合并
//指定开始行,开始列,结束行,结束列
worksheet.Cells[fromRow, fromCol, toRow, toCol].Merge = true;
//行合并
worksheet.Cells["A1:A5"].Merge = true;//合并A列的1-5行
//列合并
worksheet.Cells["A1:G1"].Merge = true;//合并第1行的A-G列
//3,样式
worksheet.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;//水平居中,全局
worksheet.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Center;//垂直居中,全局
worksheet.Cells.AutoFitColumns();//全局
worksheet.Cells.Style.WrapText = true;//自动换行,全局
worksheet.Cells.Style.Font.Name = "宋体";//全局
worksheet.Cells["A1"].Style.VerticalAlignment = ExcelVerticalAlignment.Center;//垂直居中,只针对特定单元格
worksheet.Cells["A1:A5"].Style.VerticalAlignment = ExcelVerticalAlignment.Center;//垂直居中,只针对某范围单元格
worksheet.Cells["A5"].Style.TextRotation = 180;//内容旋转
worksheet.Cells["P5"].Style.SetTextVertical(); //文字竖排
//调整行高
double rowHeight = 15;
worksheet.Row(1).Height = rowHeight;
//调整列宽
double columnWidth = 50;
worksheet.Column(1).Width = columnWidth;
//自动适应长宽
worksheet.Column(1).BestFit = true;
}