使用NPOI导出Excel操作的改进

1 篇文章 0 订阅

NPOI导出Excel是比较方便的,可以设置标题,列宽等,原先我导出Excel的方法如下:

/// <summary>
        /// 导出到Excel文件
        /// </summary>
        /// <param name="dt">表</param>
        /// <param name="sheetname">文件名</param>
        /// <param name="dictList">标题头与列宽</param>
        /// <param name="context">上下文</param>
        public void WriteExcel(DataTable dt, string sheetname, Dictionary<string, int> dictList, HttpContextBase context)
        { 
            NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
             
            if (dt != null && dt.Rows.Count > 0)
            {
                NPOI.SS.UserModel.ISheet sheet = book.CreateSheet(sheetname);

                NPOI.SS.UserModel.IRow row = sheet.CreateRow(0);

                //标题头与列宽
                var dtList = dictList.ToList();
                for (int i = 0; i < dtList.Count; i++)
                {
                    sheet.SetColumnWidth(i, dtList[i].Value);
                    row.CreateCell(i).SetCellValue(dtList[i].Key);
                }

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    NPOI.SS.UserModel.IRow row2 = sheet.CreateRow(i + 1);
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        row2.CreateCell(j).SetCellValue(Convert.ToString(dt.Rows[i][j]));
                    }
                } 
            }
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                book.Write(ms);
                context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", sheetname));
                context.Response.BinaryWrite(ms.ToArray());
                book = null;
                ms.Close();
                ms.Dispose();
            }
        }

需要传入DataTable,然后将表的标题和列宽以字典形式传入。这样的做法比较麻烦,标题和数据是分别传入,而且会将dt上所有的列都导出。所有需要将dt上的列和字典中的数据一一对应,不然导出的Excel会错位。当然也可以自己定义个类改传入标题,字段名,列宽,这样会比较方便的,dt导出数据时可以根据传入字典中的字段来判断需要导入哪些字段的数据。

但是现在使用DataTable越来越少了,直接改为传入List<T>集合的形式,我们可以定义标题特性的,在dto中进行设置然后导入时通过反射的方式来回去标题内容等信息,这样会更方便。

标题特性定义

/*
 * 导出Excel的相关特性设置
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyUtility.attribute
{
    [AttributeUsage(AttributeTargets.Property,Inherited=true)]
    public class TitleAttribute:Attribute
    {
        public string TitleName { get; set; }
        public int Width { get; set; }

        /// <summary>
        /// 导出Excel的相关特性
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="width">列宽度字符长度设置</param>
        public TitleAttribute(string title,int width)
        {
            TitleName = title;
            Width = width;
        }
    }
}

相关Dto设置 我们只需设置我们需要导出Excel字段的特性就行了,这样方便我们查看的时候和导出时的格式数据的差异性,如下我们只会导出工号,姓名,职位三个字段到Excel中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyUtility.attribute;

namespace MyMis.DTO
{
    public class empDto
    {
        [Title("工号",10)]
        public string account { get; set; }
        [Title("姓名", 10)]
        public string name { get; set; }
        [Title("职位", 10)]
        public string duty { get; set; }
        public string depid { get; set; }
        public string state { get; set; }
        public string py { get; set; }
        public string tel { get; set; }
        public string email { get; set; }
        public status status { get; set; }
        public string password { get; set; }
        public string pstatus { get; set; }
        public Nullable<System.DateTime> pdate { get; set; }
        public Nullable<System.DateTime> preset { get; set; }
        public string preseter { get; set; }
        public string smsstatus { get; set; }
        public string emailstatus { get; set; }
    }
}

导出Excel类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Data;
using NPOI.HPSF;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;
using System.Web;
using MyUtility.attribute;

namespace MyUtility
{
    public static class MyExcel
    {

        /// <summary>
        /// 导出到Excel文件 根据Dto设置的相关Title属性进行导出
        /// </summary>
        /// <param name="dt">表</param>
        /// <param name="sheetname">文件名</param>
        /// <param name="context">上下文</param>
        public static void WriteExcel<T>(List<T> ExpList, string sheetname, HttpContextBase context)
        {
            NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();

            if (ExpList != null && ExpList.Count() > 0)
            {
                NPOI.SS.UserModel.ISheet sheet = book.CreateSheet(sheetname);

                NPOI.SS.UserModel.IRow row = sheet.CreateRow(0);

                //取得导出字段标题宽度相关信息
                List<ExpDto> list = GetExpFields<T>();

                //设置标题与列宽
                for (int i = 0; i < list.Count; i++)
                {
                    sheet.SetColumnWidth(i, list[i].width * 256);//参数的单位是1/256个字符宽度
                    row.CreateCell(i).SetCellValue(list[i].title);
                }

                //设置导出数据
                for (int i = 0; i < ExpList.Count; i++)
                {
                    NPOI.SS.UserModel.IRow row2 = sheet.CreateRow(i + 1);
                    for (int j = 0; j < list.Count; j++)
                    {
                        T obj = ExpList[i];
                        row2.CreateCell(j).SetCellValue(GetValue<T>(ExpList[i], list[j].field));
                    }
                }
            }
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                book.Write(ms);
                context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", sheetname));
                context.Response.BinaryWrite(ms.ToArray());
                book = null;
                ms.Close();
                ms.Dispose();
            }
        }

        /// <summary>
        /// 取得导出Excel相关字段 根据Title特性来判断
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<ExpDto> GetExpFields<T>()
        {
            List<ExpDto> list = new List<ExpDto>();
            var fields = typeof(T).GetProperties();//获取所有属性
            foreach (var item in fields)
            {
                var attribute = item.GetCustomAttributes(typeof(TitleAttribute), true);//取得属性上是否存在指定的特性
                if (attribute.Any())
                {
                    ExpDto exp = new ExpDto();
                    exp.field = item.Name;
                    exp.title = ((TitleAttribute)attribute[0]).TitleName;
                    exp.width = ((TitleAttribute)attribute[0]).Width;
                    list.Add(exp);
                }
            }
            return list;
        }

        /// <summary>
        /// 取得指定字段的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj">obj</param>
        /// <param name="key">属性名</param>
        /// <returns></returns>
        public static string GetValue<T>(T obj, string key)
        {
            var prop = obj.GetType().GetProperty(key);//取得指定属性
            var type = prop.PropertyType.Name;//取得指定属性的字段类型
            var value = prop.GetValue(obj);//从对象上取得值
            return value==null ? null : value.ToString();
        }

        /// <summary>
        /// 导出Excel标题设置数据对象
        /// </summary>
        public class ExpDto
        {
            public string field { get; set; }
            public string title { get; set; }
            public int width { get; set; }
        }
    }
}

直接调用此方法就行了

### 回答1: 使用NPOI可以很方便地在Winform应用程序中导出Excel文件。 首先,我们需要将NPOI引用添加到Winform项目中。可以通过NuGet包管理器或手动引用方式添加。 然后,我们需要创建一个工作簿对象,并添加一个工作表。可以使用HSSFWorkbook或XSSFWorkbook类来创建工作簿对象,分别对应xls和xlsx格式的Excel文件。 接下来,我们可以向工作表中添加数据。可以使用工作表中的创建行对象,然后为每行添加单元格数据。可以设置单元格的值、格式、样式等属性。 最后,我们需要将工作簿保存为Excel文件。可以使用FileStream类创建一个文件流对象,并使用工作簿的Write方法将数据写入到文件流中。 以下是一个简单的示例代码,将一个包含学生信息的列表导出Excel文件: ```csharp using System; using System.Collections.Generic; using System.IO; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; // 创建工作簿和工作表 HSSFWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet("学生信息"); // 添加表头 IRow headerRow = sheet.CreateRow(0); headerRow.CreateCell(0).SetCellValue("学号"); headerRow.CreateCell(1).SetCellValue("姓名"); headerRow.CreateCell(2).SetCellValue("年龄"); // 添加数据 List<Student> students = GetStudents(); for (int i = 0; i < students.Count; i++) { IRow dataRow = sheet.CreateRow(i + 1); dataRow.CreateCell(0).SetCellValue(students[i].Id); dataRow.CreateCell(1).SetCellValue(students[i].Name); dataRow.CreateCell(2).SetCellValue(students[i].Age); } // 保存为Excel文件 using (FileStream fileStream = new FileStream("学生信息.xls", FileMode.Create)) { workbook.Write(fileStream); } ``` 在这个示例中,我们首先创建了一个工作簿和一个工作表,并添加了表头。然后,通过获取学生信息列表来添加数据。最后,我们将工作簿保存为名为“学生信息.xls”的Excel文件。 这样,使用NPOI就可以在Winform应用程序中导出Excel文件。希望可以对你有所帮助! ### 回答2: 使用WinForm搭配NPOI导出Excel非常简单。首先,我们需要在WinForm中添加对NPOI的引用。可以通过NuGet包管理器来导入NPOI库。 导入库后,我们可以创建一个DataGridView控件来展示需要导出的数据,或者直接在代码中定义一个DataTable对象来储存数据。然后,在按钮的Click事件处理程序中编写导出Excel的代码。 以下是一个简单的示例: 1. 添加一个DataGridView控件(或创建DataTable对象)并加载需要导出的数据。 2. 在按钮的Click事件中添加以下代码: ```csharp using NPOI.XSSF.UserModel; // 导入XSSF命名空间 using NPOI.SS.UserModel; // 导入SS命名空间 using NPOI.HSSF.Util; // 导入HSSFUtil命名空间 using NPOI.HSSF.UserModel; // 导入HSSFUserModel命名空间 using NPOI.SS.Util; // 导入SSUtil命名空间 using NPOI.HPSF; // 导入HPSF命名空间 using NPOI.POIFS.FileSystem; // 导入POIFS命名空间 // 创建一个Excel文档对象 XSSFWorkbook workbook = new XSSFWorkbook(); // 创建一个工作表对象 ISheet sheet = workbook.CreateSheet("Sheet1"); // 创建行和单元格 IRow row = sheet.CreateRow(0); for (int i = 0; i < dataGridView1.Columns.Count; i++) { row.CreateCell(i).SetCellValue(dataGridView1.Columns[i].HeaderText); } // 填充数据 for (int i = 0; i < dataGridView1.Rows.Count; i++) { row = sheet.CreateRow(i + 1); for (int j = 0; j < dataGridView1.Columns.Count; j++) { row.CreateCell(j).SetCellValue(dataGridView1.Rows[i].Cells[j].Value.ToString()); } } // 保存文件 SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Excel文件|*.xlsx"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { using (FileStream fs = new FileStream(saveFileDialog.FileName, FileMode.Create)) { workbook.Write(fs); } } // 提示导出成功 MessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); ``` 这是一个基本的WinForm使用NPOI导出Excel的代码示例。你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值