首页 > 代码库 > Epplus使用技巧
Epplus使用技巧
废话不说,直接开始。
创建Excel工作表
private static ExcelWorksheet CreateSheet(ExcelPackage p, string sheetName)
{
p.Workbook.Worksheets.Add(sheetName);
ExcelWorksheet ws = p.Workbook.Worksheets[1];
ws.Name = sheetName; //Setting Sheet‘s name
ws.Cells.Style.Font.Size = 11; //Default font size for whole sheet
ws.Cells.Style.Font.Name = "Calibri"; //Default Font name for whole sheet
return ws;
}
合并的Excel列
//Merging cells and create a center heading for out table
ws.Cells[1, 1].Value = http://www.mamicode.com/"Sample DataTable Export"; // Heading Name
ws.Cells[1, 1, 1, dt.Columns.Count].Merge = true; //Merge columns start and end range
ws.Cells[1, 1, 1, dt.Columns.Count].Style.Font.Bold = true; //Font should be bold
ws.Cells[1, 1, 1, dt.Columns.Count].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; // Aligmnet is center
Excel单元格背景颜色
//Setting the background color of header cells to Gray
var fill = cell.Style.Fill;
fill.PatternType = ExcelFillStyle.Solid;
fill.BackgroundColor.SetColor(Color.Gray);
Excel单元格边框
//Setting Top/left,right/bottom borders.
var border = cell.Style.Border;
border.Bottom.Style = border.Top.Style = border.Left.Style = border.Right.Style = ExcelBorderStyle.Thin;
Excel公式
//Setting Sum Formula
cell.Formula = "Sum(" + ws.Cells[3, colIndex].Address + ":" + ws.Cells[rowIndex - 1, colIndex].Address + ")";
添加注释到Excel单元格
private static void AddComment(ExcelWorksheet ws, int colIndex, int rowIndex, string comment, string author)
{
//Adding a comment to a Cell
var commentCell = ws.Cells[rowIndex, colIndex];
commentCell.AddComment(comment, author);
}
添加图像在Excel工作表
private static void AddImage(ExcelWorksheet ws, int columnIndex, int rowIndex, string filePath)
{
//How to Add a Image using EP Plus
Bitmap image = new Bitmap(filePath);
ExcelPicture picture = null;
if (image != null)
{
picture = ws.Drawings.AddPicture("pic" + rowIndex.ToString() + columnIndex.ToString(), image);
picture.From.Column = columnIndex;
picture.From.Row = rowIndex;
picture.From.ColumnOff = Pixel2MTU(2); //Two pixel space for better alignment
picture.From.RowOff = Pixel2MTU(2);//Two pixel space for better alignment
picture.SetSize(100, 100);
}
}
添加自定义对象到Excel工作表
private static void AddCustomShape(ExcelWorksheet ws, int colIndex, int rowIndex, eShapeStyle shapeStyle, string text)
{
ExcelShape shape = ws.Drawings.AddShape("cs" + rowIndex.ToString() + colIndex.ToString(), shapeStyle);
shape.From.Column = colIndex;
shape.From.Row = rowIndex;
shape.From.ColumnOff = Pixel2MTU(5);
shape.SetSize(100, 100);
shape.RichText.Add(text);
}
注:摘自http://zeeshanumardotnet.blogspot.jp/2011/06/creating-reports-in-excel-2007-using.html
Epplus使用技巧
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。