导出Excel方式汇总-- 第三种 利用 Npoi 插件,只需要在项目里引用DLL即可,无需在服务器安装任何插件。

本文介绍了如何使用Npoi插件在ASP.NET C#项目中导出Excel,无需在服务器上安装额外插件。Npoi支持Excel2003和2007,但两者使用对象不同,需要编写两套代码。数据可以保存为文件或直接通过输出流发送到客户端。对于AJAX调用,可以将Excel内容暂存到缓存,然后通过下载按钮触发下载。
摘要由CSDN通过智能技术生成

第三种:利用 Npoi 插件,只需要在项目里引用DLL即可,无需在服务器安装任何插件。

注意:

            1、NOPI4.0  支持excel2003和excel2007

            2、03和07使用的对象不同,需要区分开来。代码需要写两套。

            3、可以保存为文件,也可以直接输出。

 

public string ListToExcel(List<VmExceldgExcelOut> items, string fileName)
        {
            string url="";
            int rows = items.Count;//不包括字段名
            int cols = 0;
            int colIndex = 0;
            if (rows == 0)
            {
                return "没有数据!";
            }

            fileName = "批量导出"+fileName + ".xlsx";// + DateTime.Now.ToString("yyyyMMddhhmmss")
            url = fileUrl + fileName;//返回下载路径
            fileName = filepath + fileName;

            try
            {
                XSSFWorkbook xssfworkbook = new XSSFWorkbook();
                ISheet sheet = xssfworkbook.CreateSheet("tempData");
                ICellStyle cellStyle = xssfworkbook.CreateCellStyle();
                cellStyle.BorderTop = BorderStyle.Thin;
                cellStyle.BorderBottom = BorderStyle.Thin;
                cellStyle.BorderLeft = BorderStyle.Thin;
                cellStyle.BorderRight = BorderStyle.Thin;
                /*IFont font12 = xssfworkbook.CreateFont();
                font12.FontHeightInPoints = 12;
                font12.FontName = "宋体";*/
                //return url;
                #region 生成Excel

                //表头  
                IRow row = sheet.CreateRow(0);
                VmExceldgExcelOut temp = new VmExceldgExcelOut();
                PropertyInfo[] propertys = temp.GetType().GetProperties();
                foreach (PropertyInfo property in propertys)
                {
                    string code = ((DescriptionAttribute)Attribute.GetCustomAttribute(property, typeof(DescriptionAttribute))).Description;// 属性值
                    if (string.IsNullOrEmpty(code))
                        continue;
                    ICell cell = row.CreateCell(colIndex);
                    cell.CellStyle = cellStyle;
                    cell.SetCellValue(code);
                    colIndex++;
                }
                cols = colIndex;

                //数据
                for (int r = 0; r < rows; r++)
                {
                    IRow row1 = sheet.CreateRow(r + 1);
                    temp = items[r];
                    for (int c = 0; c < cols; c++)
                    {
                        propertys = temp.GetType().GetProperties();
                        object o = propertys[c].GetValue(temp, null);
                        ICell cell = row1.CreateCell(c);
                        cell.CellStyle = cellStyle;
                        if (o == null) o = "";
                        cell.SetCellValue(o.ToString());
                    }
                }
                #endregion

                //转为字节数组  
                MemoryStream stream = new MemoryStream();
                xssfworkbook.Write(stream);
                var buf = stream.ToArray();

                /*HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", DateTime.Now.ToString("yyyyMMddHHmmssfff")));
                HttpContext.Current.Response.BinaryWrite(stream.ToArray());
                xssfworkbook = null;
                stream.Close();
                stream.Dispose();*/

                //保存为Excel文件  
                using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
                {
                    fs.Write(buf, 0, buf.Length);
                    fs.Flush();
                }
            }
            catch (Exception e)
            {
                return "导出Excel失败,错误信息:" + e.Message;
            }
            return url;
        }
         
        public string DataTableToExcel(DataTable dt, string fileName)
        {
            string url 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值