using(ExcelPackage package = new ExcelPackage(new FileInfo(@"D:\Sample.xlsx")))
//所有生成xlsx的语句必须写在using领域内部
以下语句必须在using语句中编写
ExcelWorksheet sheet = package.Workbook.Worksheets.Add("sheetName");
sheet.Cells[1, 1].Value = "ID";
sheet.Cells[1, 2].Value = "Product";
sheet.Cells["A4"].Value = "Price";
//将从[a,b]到[A,B]中间的单元格合并。
sheet.Cells[int a,int b,int A,int B].Merge = True;
var cell = sheet.Cells[1,1];
//字体名
sheet.Cells[2, 1, 2, 10].Style.Font.Name = "宋体";
//字体粗体
sheet.Cells[2, 1, 2, 10].Style.Font.Bold = True;
//字体大小
sheet.Cells[1, 1].Style.Font.Size = 1;
//水平对齐
Sheet.Cells[1,1,a,b].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
//垂直对齐
worksheet.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Bottom;
worksheet.Cells.Style.Fill.PatternType= ExcelFillStyle.Solid;
worksheet.Cells.Style.Fill.BackgroundColor.SetColor(Color.LightBlue);
sheet.Cells[a,b].Style.WrapText= true;
sheet.Cells[a,b,A,B].Style.Numberformat.Format= "0.00";
sheet.Cells[1, 1].Style.Font.Color.SetColor(Color.Red);
//设置边框,及其颜色
sheet.Cells[1, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
//设置列宽
sheet.Column(1).Width = 15;
//WPF 以与设备无关的单位(每个单位 1/96 英寸)。
sheet.Column(1).Width=column.ActualWidth;
//设置行高
sheet.Row(b).Height = 12;
//单元格自适应宽高
sheet.Cells.Style.ShrinkToFit = true;
sheet.Cells[1, 1].Hyperlink = new ExcelHyperLink("http:\\www.baidu.com", UriKind.Relative);
sheet.Cells.AutoFitColumns();
sheet.Cells[1, 1].AutoFitColumns();