2021-11-01

FileInfo newFile = new FileInfo(@“d:\test.xlsx”);
if (newFile.Exists)
{
newFile.Delete();
newFile = new FileInfo(@“d:\test.xlsx”);
}
using (ExcelPackage package = new ExcelPackage(newFile))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(“test”);

            worksheet.Cells.Style.WrapText = true;
            worksheet.View.ShowGridLines = false;//去掉sheet的网格线

            worksheet.Cells[1, 1].Value = "名称";
            worksheet.Cells[1, 2].Value = "价格";
            worksheet.Cells[1, 3].Value = "销量";

            worksheet.Cells[2, 1].Value = "大米";
            worksheet.Cells[2, 2].Value = 56;
            worksheet.Cells[2, 3].Value = 100;

            worksheet.Cells[3, 1].Value = "玉米";
            worksheet.Cells[3, 2].Value = 45;
            worksheet.Cells[3, 3].Value = 150;

            worksheet.Cells[4, 1].Value = "小米";
            worksheet.Cells[4, 2].Value = 38;
            worksheet.Cells[4, 3].Value = 130;

            worksheet.Cells[5, 1].Value = "糯米";
            worksheet.Cells[5, 2].Value = 22;
            worksheet.Cells[5, 3].Value = 200;

            using (ExcelRange range = worksheet.Cells[1, 1, 5, 3])
            {
                range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                range.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            }

            using (ExcelRange range = worksheet.Cells[1, 1, 1, 3])
            {
                range.Style.Font.Bold = true;
                range.Style.Font.Color.SetColor(Color.White);
                range.Style.Font.Name = "微软雅黑";
                range.Style.Font.Size = 12;
                range.Style.Fill.PatternType = ExcelFillStyle.Solid;
                range.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(128, 128, 128));
            }

            worksheet.Cells[1, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
            worksheet.Cells[1, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
            worksheet.Cells[1, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));

            worksheet.Cells[2, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
            worksheet.Cells[2, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
            worksheet.Cells[2, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));

            worksheet.Cells[3, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
            worksheet.Cells[3, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
            worksheet.Cells[3, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));

            worksheet.Cells[4, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
            worksheet.Cells[4, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
            worksheet.Cells[4, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));

            worksheet.Cells[5, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
            worksheet.Cells[5, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
            worksheet.Cells[5, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));

            ExcelChart chart = worksheet.Drawings.AddChart("chart", eChartType.ColumnClustered);

            ExcelChartSerie serie = chart.Series.Add(worksheet.Cells[2, 3, 5, 3], worksheet.Cells[2, 1, 5, 1]);
            serie.HeaderAddress = worksheet.Cells[1, 3];

            chart.SetPosition(150, 10);
            chart.SetSize(500, 300);
            chart.Title.Text = "销量走势";
            chart.Title.Font.Color = Color.FromArgb(89, 89, 89);
            chart.Title.Font.Size = 15;
            chart.Title.Font.Bold = true;
            chart.Style = eChartStyle.Style15;
            chart.Legend.Border.LineStyle = eLineStyle.Solid;
            chart.Legend.Border.Fill.Color = Color.FromArgb(217, 217, 217);

            package.Save();
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值