using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using NPOI.HSSF.UserModel;
using Microsoft.EntityFrameworkCore;
using NPOI.SS.UserModel;
using Jiumu.Entity;
namespace Project1.Controllers
{
public class OutputExcel:ControllerBase
{
private readonly SalesbudgetquotingplatformContext Context;
public OutputExcel(SalesbudgetquotingplatformContext context)
{
Context = context;
}
#region NPOI导出到Excel
//NPOI导出到Excel(故障码)
public ActionResult ExportToExcel()
{
//查询数据
var entity = Context.MatPrice.Include(x=>x.MatSupplier).Include(x => x.MatWarehouse).Include(x => x.MatWarehouse.MatRaw).Where(x=> string.IsNullOrEmpty(x.MatWarehouse.SyncId)).OrderBy(x=>x.MatWarehouse.Name).ToList();
//二:代码创建一个Excel表格(这里称为工作簿)
//创建Excel文件的对象 工作簿(调用NPOI文件)
HSSFWorkbook excelBook = new HSSFWorkbook();
ICellStyle style1 = excelBook.CreateCellStyle();//声明style1对象,设置Excel表格的样式
ICellStyle style2 = excelBook.CreateCellStyle();
ICellStyle style3 = excelBook.CreateCellStyle();
IFont font = excelBook.CreateFont();
font.Color = IndexedColors.Red.Index;
//style3.SetFont(font);
style1.Alignment = HorizontalAlignment.Justify;//两端自动对齐(自动换行)
style1.VerticalAlignment = VerticalAlignment.Center;
style2.Alignment = HorizontalAlignment.Center;
style2.VerticalAlignment = VerticalAlignment.Center;
style3.Alignment = HorizontalAlignment.Center;
style3.VerticalAlignment = VerticalAlignment.Center;
//创建Excel工作表 Sheet=故障码信息
NPOI HSSFWorkbook自建格式导出
最新推荐文章于 2024-03-12 16:45:20 发布