2018年7月6日 星期五

epplus 用法

自動設定欄寬
sheet.Cells.AutoFitColumns(3, 20); // 必須設定 min 跟 max 才會正常作用

凍結欄位
sheet.View.FreezePanes(4, 4);

標題列
ws.PrinterSettings.RepeatRows = sheet.Cells["1:1"];

縮小成一頁寬
ws.PrinterSettings.FitToPage = true;
ws.PrinterSettings.FitToWidth = 1;
ws.PrinterSettings.FitToHeight = 0;

邊界
ws.PrinterSettings.LeftMargin = ws.PrinterSettings.TopMargin = ws.PrinterSettings.BottomMargin = ws.PrinterSettings.RightMargin = (decimal)0.3;

底色
sheet.Cells[row, 1, row, col - 1].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
sheet.Cells[row, 1, row, col - 1].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Yellow);

框線
range.Style.Border.Top.Style = range.Style.Border.Bottom.Style = range.Style.Border.Left.Style = range.Style.Border.Right.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;

小數格式
ws.Cells[row, 6].Style.Numberformat.Format = "0.00"; // 固定小數兩位
ws.Cells[row, 6].Style.Numberformat.Format = 整數不含小數點格式字串((decimal?)ws.Cells[row, 6].Value, "0.##"); // 若有小數則最多取兩位,若為整數則不含結尾的"."
string 整數不含小數點格式字串(decimal? value, string 格式字串)
        {
            if (格式字串 == null) return "";
            if (value == null) return 格式字串;
            if (value % 1 == 0 && 格式字串.IndexOf("0.#") > -1) return 格式字串.Substring(0, 格式字串.IndexOf("0.#") + 1);
            return 格式字串;
        }

插入列並保留格式
ws.InsertRow(rowindex, 1);
ws.Cells[rowindex-1, 1, rowindex-1, 最後欄index].Copy(ws.Cells[rowindex , 1, rowindex , 最後欄index]);

取得公式值
sheet.Cells[rowindex, colindex].FormulaR1C1="xxx";
sheet.Cells[rowindex, colindex].Calculate();
var 公式值 = sheet.Cells[rowindex, colindex].Value;

沒有留言:

Entity Framework 建立新物件並儲存後馬上取得關聯資料

使用 CreateProxy 建立物件,不要直接 new var newmodel = _contextXXX.CreateProxy<yyy>(); ... _contextXXX.yyy.Add(newmodel); await _contextXXX.SaveC...