asp.net导出excel示例代码
asp.net导出excel的简单方法。
excel的操作,最常用的就是导出和导入。
本例使用NPOI实现。
代码:/// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <returns></returns>
        public ActionResult Export(FormCollection frm)
        {
            DataTable dts = new DataTable();
            dts = _shopMemeber.ExportMemberData(frm);
            IWorkbook workbook = new XSSFWorkbook();
            ISheet sheet = workbook.CreateSheet();
            IRow headerRow = sheet.CreateRow(0);
            foreach (DataColumn column in dts.Columns)
                headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);
            int rowIndex = 1;
            foreach (DataRow row in dts.Rows)
            {
                IRow dataRow = sheet.CreateRow(rowIndex);
                foreach (DataColumn column in dts.Columns)
                {
                    dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
                }
                rowIndex++;
            }
            string filepath = Server.MapPath("/") + @"用户列表.xlsx";
            FileStream file = new FileStream(filepath, FileMode.Create);
            workbook.Write(file);
            ExcelHelper.DownLoad(@"/用户列表.xlsx");
            #region 不启用
            #endregion
            return SuccessMsg("AdminMemberMemberIndex");
        }
//这个是下载到桌面的方法,没实现自选路径
public static void DownLoad(string FileName)
 {
             FileInfo fileInfo = new FileInfo(HttpContext.Current.Server.MapPath(FileName));
             //以字符流的形式下载文件
             FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(FileName), FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
              fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            HttpContext.Current.Response.ContentType = "application/octet-stream";
               //通知浏览器下载文件而不是打开
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(fileInfo.Name, System.Text.Encoding.UTF8));
          HttpContext.Current.Response.BinaryWrite(bytes);
           HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }
上面是导出,下面我介绍下导入。
复制代码 代码如下:

/// <summary>
        /// 导入数据
        /// </summary>
        /// <param name="file"></param>
        /// <returns>true表示导入成功</returns>
        public bool Impoart(HttpPostedFileBase file)
        {
            try
            {
                //保存excel
                string path = HttpContext.Current.Server.MapPath("/");
                file.SaveAs(path + file.FileName);
                //读取
                FileStream sw = File.Open(path + file.FileName, FileMode.Open, FileAccess.Read);
                IWorkbook workbook = new XSSFWorkbook(sw);
                ISheet sheet1 = workbook.GetSheet("Sheet1");
                //最大行数
                int rowsCount = sheet1.PhysicalNumberOfRows;
                //判断首行是否符合规范  也就是Excel中的列名
                IRow firstRow = sheet1.GetRow(0);
                if (
                    !(firstRow.GetCell(0).ToString() == "名称" && firstRow.GetCell(1).ToString() == "简称" &&
                      firstRow.GetCell(2).ToString() == "分类" && firstRow.GetCell(3).ToString() == "参考价" &&
                      firstRow.GetCell(4).ToString() == "商品介绍"))
                {
                    return false;
                }

//跳过类型不正确的品项
                for (int i = 1; i < rowsCount; i++)
                {
                    IRow row = sheet1.GetRow(i);
                    Shop_Product product = new Shop_Product();
                    string category = row.GetCell(2) != null ? row.GetCell(2).ToString() : null;
                    if (!string.IsNullOrEmpty(category))
                    {
                        var cate =
                            _unitOfWork.Shop_ProductCategoryRepository().GetAll().FirstOrDefault(t => t.Name == category);
                        if (cate != null)
                        {
                            product.ProductCategoryName = cate.Name;
                            product.Shop_ProductCategory_ID = cate.ID;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        continue;
                    }
                    product.PName = row.GetCell(0) != null ? row.GetCell(0).ToString() : null;
                    product.PCName = row.GetCell(1) != null ? row.GetCell(1).ToString() : null;
                    if (row.GetCell(3) != null)
                    {
                        product.Price = Double.Parse(row.GetCell(3).ToString());
                    }
                    product.Description = row.GetCell(4) != null ? row.GetCell(4).ToString() : null; // www.jbxue.com
           _unitOfWork.Shop_ProductRepository().Insert(product);
                }
                _unitOfWork.Save();
            }
            catch
            {
                return false;
            }
            return true;
        }

posted on 2014-02-26 06:45 snowfly123 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/linuxnotes/p/3568221.html

asp.net导出excel示例代码相关推荐

  1. 【译】Asp.Net 导出 Excel 数据的9种方案

    简介 Excel 的强大之处在于它不仅仅只能打开Excel格式的文档,它还能打开CSV格式.Tab格式.website table 等多钟格式的文档.它具备自动识别行号,字符,格式化数字等功能,例如: ...

  2. 关于ASP.NET导出Excel表格的个人总结归纳

    2019独角兽企业重金招聘Python工程师标准>>> 之前一直想研究ASP.NET导出Excel表格来着,但一直没有时间,这几天因为一个项目的需要,所以就钻研了一下.有百度,但网上 ...

  3. NPOI导出Excel示例

    摘要:使用开源程序NPOI导出Excel示例.NPOI首页地址:http://npoi.codeplex.com/,NPOI示例博客:http://tonyqus.sinaapp.com/. 示例编写 ...

  4. JavaScript导出Excel通用代码。

    2019独角兽企业重金招聘Python工程师标准>>> js导出Excel通用代码. html代码如下: <!DOCTYPE html PUBLIC "-//W3C/ ...

  5. python爬虫获取服务器信息,通过python自动化获取服务器信息,并写入到excel(示例代码)...

    简介这篇文章主要介绍了通过python自动化获取服务器信息,并写入到excel(示例代码)以及相关的经验技巧,文章约943字,浏览量170,点赞数4,值得参考! 博主目前在电信外包工作,比较坑,因为涉 ...

  6. struts2 poi导出excel实例代码下载

    原文:struts2 poi导出excel实例代码下载 代码下载地址:http://www.zuidaima.com/share/1550463233526784.htm 页面展现成表格形式,添加ex ...

  7. php导出excel数据代码,phpspreadsheet导出数据到Excel的方法介绍(代码示例)

    本篇文章给大家带来的内容是关于phpspreadsheet导出数据到Excel的方法介绍(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 之前我们使用PHP导出Excel数 ...

  8. asp.net导出excel-一行代码实现excel、xml、pdf、word、html、csv等7种格式文件导出功能而且美观-SNF快速开发平台...

    分享: 腾讯微博  新浪微博   搜狐微博   网易微博  腾讯朋友  百度贴吧  豆瓣   QQ好友  人人网 作者:王春天  原文地址:http://www.cnblogs.com/spring_ ...

  9. ASP.Net导出EXCEL表(小结)

    这个问题困扰了我好几天,现在终于解决了,现公布整个过程的注意点.通过.net导出Excel表,除了注意导出的方法外,还要关心资源的回收问题,中间涉及到COM组件的关闭等. 因为对windows系统.. ...

最新文章

  1. 【Java面试题】37 说出ArrayList,Vector, LinkedList的存储性能和特性
  2. 基于深度学习的视觉三维重建研究总结
  3. 互联网团队协作:可追溯【连载三】
  4. python做电脑软件-程序员带你十天快速入门Python,玩转电脑软件开发(一)
  5. Zabbix学习之路(五)之MySQL监控
  6. oracle exp cluster n,oracle cluster verfication utility failed
  7. Scala入门到精通——第二十二节 高级类型 (一)
  8. Codeforces 1096F(dp + 树状数组)
  9. Oracle备份恢复概要
  10. 初中物理凸透镜成像动态图_人教版初中物理八年级上册 平面镜成像 公开课优质课课件教案视频...
  11. 视频播功能及画面协同操作注意事项
  12. html背景图片加载慢,javascript – 如何加快我网站的背景图片加载速度?
  13. 基于Kivy的HDR拍摄软件案例分享
  14. Pytorch自动求梯度
  15. 分布式系统的特点及问题
  16. (转)Unity 之 UGUI 小总结
  17. Attention机制介绍(原理+代码)
  18. iOS-Core-Animation-Advanced-Techniques(六)
  19. 美标 三段式、四段式 耳机头 以旧换新
  20. 什么是NP问题,NP-complete和NP-hard问题.

热门文章

  1. Jquery实现简单图片切换
  2. Alter-有意思的小游戏
  3. Android 事件处理
  4. IDEA去除mapper.xml文件中的sql语句的背景色
  5. (转)dp动态规划分类详解
  6. Kali源库配置和拼音安装
  7. [4]Telerik Grid 简单使用方法
  8. Exchange2003-2010迁移系列之二,迁移前的准备工作(上)
  9. 新增两款Skin(clover与Valentine)
  10. 刚认识女孩说不要浪费时间_不要浪费时间寻找学习数据科学的最佳方法