一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - asp.net導出excel的簡單方法實例

asp.net導出excel的簡單方法實例

2019-12-01 11:31ASP.NET教程網 ASP.NET教程

這篇文章主要介紹了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;

                    _unitOfWork.Shop_ProductRepository().Insert(product);
                }

                _unitOfWork.Save();
            }
            catch
            {
                return false;
            }

            return true;
        }

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 人人爱天天做夜夜爽88 | 77成人影视 | 91精品综合久久久久久五月天 | 色老汉| 免费全看男女拍拍拍的视频 | 亚洲午夜大片 | 王小军怎么了最新消息 | 艹出白浆| 歪歪私人影院成人毛片 | 国产免费色视频 | 美女mm131爽爽爽久久 | 99热这里只有精品在线 | 日韩免费毛片视频杨思敏 | 成人观看免费观看视频 | 超级乱淫 不详 | 私人影院在线播放 | 亚洲AV无码专区国产乱码网站 | 甜蜜惩罚小说 | 干操网 | 4hu影院在线观看 | 青青草原国产一区二区 | 亚洲 欧美 日韩 综合 | 91精品国产高清久久久久 | 免费看美女被靠到爽的视频 | 门卫老张和女警花小说 | caoporm碰最新免费公开视频 | 精灵之森高清在线 | 韩国一区二区三区 | 四虎导航 | 黄网在线观看免费网站台湾swag | re99热 | 99爱爱| 王雨纯 羞羞 | 欧美侏儒xxx | 高人先生免费观看全集 | 99视频福利 | 4tube欧美高清| 狠狠色狠狠色综合系列 | 人阁色第四影院在线观看 | 97视频人人| 国产伦精品一区二区三区免费迷 |