worksheet.Cells[1, 1, 3, 3].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells[1, 1, 3, 3].Style.Fill.BackgroundColor.SetColor(Color.LightBlue);如何添加value
时间: 2023-05-18 14:04:02 浏览: 130
您可以使用以下代码将值添加到单元格中:
worksheet.Cells[1, 1].Value = "Value1";
worksheet.Cells[2, 1].Value = "Value2";
worksheet.Cells[3, 1].Value = "Value3";
相关问题
public ActionResult ExportToExcel() { var package = new ExcelPackage(); var worksheet = package.Workbook.Worksheets.Add("Sheet1"); //表格边框 worksheet.Cells[1,1,3,3].Style.Border.Left.Style = ExcelBorderStyle.Thin; worksheet.Cells[1, 1, 3, 3].Style.Border.Right.Style = ExcelBorderStyle.Thin; worksheet.Cells[1, 1, 3, 3].Style.Border.Top.Style = ExcelBorderStyle.Thin; worksheet.Cells[1, 1, 3, 3].Style.Border.Bottom.Style = ExcelBorderStyle.Thin; //表格居中 worksheet.Cells[1, 1, 3, 3].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; //表格字体颜色 worksheet.Cells[1, 1].Style.Font.Color.SetColor(Color.Red); //设置单元格背景色 worksheet.Cells[1, 1].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells[1, 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(128, 128, 128)); // 添加表头 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 = 25; worksheet.Cells[2, 3].Value = "男"; worksheet.Cells[3, 1].Value = "李四"; worksheet.Cells[3, 2].Value = 30; worksheet.Cells[3, 3].Value = "女"; // 自适应列宽 //worksheet.Cells.AutoFitColumns(); // 设置文件名 string fileName = "Export.xlsx"; // 将 Excel 文件转换为字节数组 byte[] fileContents = package.GetAsByteArray(); // 返回文件 return File(fileContents, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName); }在里面添加个图表
可以在以下代码中添加图表:
// 添加图表
var chart = worksheet.Drawings.AddChart("Chart1", eChartType.ColumnClustered);
chart.SetPosition(4, 0, 6, 0);
chart.SetSize(600, 400);
var series = chart.Series.Add(worksheet.Cells["B2:B3"], worksheet.Cells["A2:A3"]);
chart.Title.Text = "年龄统计";
chart.Style = eChartStyle.Style2;
这段代码会在表格下方添加一个柱状图,用于展示年龄统计信息。你可以根据需要修改图表的类型、位置、大小、数据源等属性。
public ActionResult ExportToExcel() { var package = new ExcelPackage(); var worksheet = package.Workbook.Worksheets.Add("Sheet1"); // 添加表头 worksheet.Cells[1, 1].Value = "姓名"; worksheet.Cells[1, 1, 3, 3].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells[1, 1, 3, 3].Style.Fill.BackgroundColor.SetColor(Color.LightBlue); worksheet.Cells[1, 2].Value = "年龄"; worksheet.Cells[1, 3].Value = "性别"; // 添加数据 worksheet.Cells[2, 1].Value = "张三"; worksheet.Cells[2, 2].Value = 25; worksheet.Cells[2, 3].Value = "男"; worksheet.Cells[3, 1].Value = "李四"; worksheet.Cells[3, 2].Value = 30; worksheet.Cells[3, 3].Value = "女"; // 自适应列宽 worksheet.Cells.AutoFitColumns(); // 设置文件名 string fileName = "Export.xlsx"; // 将 Excel 文件转换为字节数组 byte[] fileContents = package.GetAsByteArray(); // 返回文件 return File(fileContents, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName); }为什么当前上下文不存在"Color"
这是因为在代码中没有引用 System.Drawing 命名空间,而 Color 类是在该命名空间中定义的。要解决这个问题,需要在代码文件的顶部添加以下引用语句:
using System.Drawing;
阅读全文