c#导出Excel NPOI方式

/// <summary>
/// 导出商品列表
/// </summary>
public FileResult ExportProduct()
{
    //创建一个新的xls文件
    HSSFWorkbook workbook = new HSSFWorkbook();
 
    //创建一个Sheet
    ISheet sheet = workbook.CreateSheet("Sheet1");
    sheet.DefaultRowHeight = 300;
    //创建标题
    IRow rowTitle = sheet.CreateRow(0);
    rowTitle.Height = 500;
    ICellStyle styleTitle = workbook.CreateCellStyle();
    styleTitle.Alignment = HorizontalAlignment.Center;
    styleTitle.VerticalAlignment = VerticalAlignment.Center;
    IFont fontTitle = workbook.CreateFont();
    fontTitle.FontName = "宋体";
    fontTitle.FontHeightInPoints = 18;
    styleTitle.SetFont(fontTitle);
    ICell cellTitle = rowTitle.CreateCell(0);
    cellTitle.SetCellValue("商品列表");
    cellTitle.CellStyle = styleTitle;
    sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 5));  //合并单元格
    //创建表格样式
    IFont font = workbook.CreateFont();
    font.FontName = "宋体";
    font.FontHeightInPoints = 10;
    ICellStyle style = workbook.CreateCellStyle(); ;
    style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
    style.BottomBorderColor = HSSFColor.Black.Index;
    style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
    style.LeftBorderColor = HSSFColor.Black.Index;
    style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
    style.RightBorderColor = HSSFColor.Black.Index;
    style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
    style.TopBorderColor = HSSFColor.Black.Index;
    style.Alignment = HorizontalAlignment.Center;
    style.SetFont(font);
    //创建表头
    IRow rowHead = sheet.CreateRow(1);
    rowHead.CreateCell(0).SetCellValue("序号");
    rowHead.GetCell(0).CellStyle = style;
    sheet.SetColumnWidth(0, 256 * 5);
 
    rowHead.CreateCell(1).SetCellValue("商品名称");
    rowHead.GetCell(1).CellStyle = style;
    sheet.SetColumnWidth(1, 256 * 25);
 
    rowHead.CreateCell(2).SetCellValue("商品品牌");
    rowHead.GetCell(2).CellStyle = style;
    sheet.SetColumnWidth(2, 256 * 20);
 
    rowHead.CreateCell(3).SetCellValue("商品价格");
    rowHead.GetCell(3).CellStyle = style;
    sheet.SetColumnWidth(3, 256 * 15);
 
    rowHead.CreateCell(4).SetCellValue("数量");
    rowHead.GetCell(4).CellStyle = style;
    sheet.SetColumnWidth(3, 256 * 10);
 
    rowHead.CreateCell(5).SetCellValue("总金额");
    rowHead.GetCell(5).CellStyle = style;
    sheet.SetColumnWidth(3, 256 * 15);
 
    //获取商品列表数据
    List<ProductModel> dataList = GetProductList();
    //绑定表内容
    int rowindex = 2;
    int xh = 1;
    foreach (var item in dataList)
    {
        IRow rowContent = sheet.CreateRow(rowindex);
        rowContent.CreateCell(0).SetCellValue(xh);
        rowContent.GetCell(0).CellStyle = style;
 
        rowContent.CreateCell(1).SetCellValue(item.ProductName);
        rowContent.GetCell(1).CellStyle = style;
 
        rowContent.CreateCell(2).SetCellValue(item.ProductBrand);
        rowContent.GetCell(2).CellStyle = style;
 
        rowContent.CreateCell(3).SetCellValue(item.ProductPrice.ToString());
        rowContent.GetCell(3).CellStyle = style;
 
        rowContent.CreateCell(4).SetCellValue(item.Quantity.ToString());
        rowContent.GetCell(4).CellStyle = style;
        //设置函数,计算总金额
        rowContent.CreateCell(5).SetCellFormula(String.Format("$D{0}*$E{0}", rowindex+1));
        rowContent.GetCell(5).CellStyle = style;
 
        rowindex++;
        xh++;
    }
 
    //输出 
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    workbook.Write(ms);
    ms.Seek(0, SeekOrigin.Begin);
    return File(ms, "application/vnd.ms-excel", "商品列表.xls");
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用C#和NPOI库可以方便地导出Excel表格。下面是一个简单的示例: 1. 首先,你需要安装NPOI库,可以使用NuGet包管理器进行安装。 2. 在代码中,首先创建一个工作簿和一个工作表: ``` using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System.IO; ... // 创建一个工作簿 var workbook = new XSSFWorkbook(); // 创建一个工作表 var sheet = workbook.CreateSheet("Sheet1"); ``` 3. 接下来,你可以向表格中添加数据。以下是将数据添加到第一行的示例: ``` // 创建第一行并添加数据 var headerRow = sheet.CreateRow(0); headerRow.CreateCell(0).SetCellValue("ID"); headerRow.CreateCell(1).SetCellValue("Name"); headerRow.CreateCell(2).SetCellValue("Age"); ``` 4. 然后,你可以循环遍历数据并将其添加到表格中。以下是将数据添加到第二行和第三行的示例: ``` // 模拟数据 var data = new List<Person> { new Person { ID = 1, Name = "Alice", Age = 18 }, new Person { ID = 2, Name = "Bob", Age = 20 } }; // 循环遍历数据 for (int i = 0; i < data.Count; i++) { var row = sheet.CreateRow(i + 1); row.CreateCell(0).SetCellValue(data[i].ID); row.CreateCell(1).SetCellValue(data[i].Name); row.CreateCell(2).SetCellValue(data[i].Age); } ``` 5. 最后,将工作簿保存到文件中: ``` // 保存工作簿到文件 using (var fileStream = new FileStream("output.xlsx", FileMode.Create)) { workbook.Write(fileStream); } ``` 完整的代码示例: ``` using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System.Collections.Generic; using System.IO; class Person { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } } ... // 创建一个工作簿 var workbook = new XSSFWorkbook(); // 创建一个工作表 var sheet = workbook.CreateSheet("Sheet1"); // 创建第一行并添加数据 var headerRow = sheet.CreateRow(0); headerRow.CreateCell(0).SetCellValue("ID"); headerRow.CreateCell(1).SetCellValue("Name"); headerRow.CreateCell(2).SetCellValue("Age"); // 模拟数据 var data = new List<Person> { new Person { ID = 1, Name = "Alice", Age = 18 }, new Person { ID = 2, Name = "Bob", Age = 20 } }; // 循环遍历数据 for (int i = 0; i < data.Count; i++) { var row = sheet.CreateRow(i + 1); row.CreateCell(0).SetCellValue(data[i].ID); row.CreateCell(1).SetCellValue(data[i].Name); row.CreateCell(2).SetCellValue(data[i].Age); } // 保存工作簿到文件 using (var fileStream = new FileStream("output.xlsx", FileMode.Create)) { workbook.Write(fileStream); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值