最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

asp.net導(dǎo)出excel的簡單方法實(shí)例

 更新時(shí)間:2014年02月25日 15:24:33   作者:  
這篇文章主要介紹了asp.net導(dǎo)出excel的簡單方法實(shí)例,需要的朋友可以參考下

excel的操作,最常用的就是導(dǎo)出和導(dǎo)入,廢話不多說上代碼。

本例使用NPOI實(shí)現(xiàn)的,不喜勿噴哈。。。。

復(fù)制代碼 代碼如下:

/// <summary>
        /// 導(dǎo)出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");
        }
//這個(gè)是下載到桌面的方法,沒實(shí)現(xiàn)自選路徑
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();
        }

上面是導(dǎo)出,下面我介紹下導(dǎo)入。

復(fù)制代碼 代碼如下:

/// <summary>
        /// 導(dǎo)入數(shù)據(jù)
        /// </summary>
        /// <param name="file"></param>
        /// <returns>true表示導(dǎo)入成功</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");

                //最大行數(shù)
                int rowsCount = sheet1.PhysicalNumberOfRows;

                //判斷首行是否符合規(guī)范  也就是Excel中的列名
                IRow firstRow = sheet1.GetRow(0);
                if (
                    !(firstRow.GetCell(0).ToString() == "名稱" && firstRow.GetCell(1).ToString() == "簡稱" &&
                      firstRow.GetCell(2).ToString() == "分類" && firstRow.GetCell(3).ToString() == "參考價(jià)" &&
                      firstRow.GetCell(4).ToString() == "商品介紹"))
                {
                    return false;
                }


                //跳過類型不正確的品項(xiàng)
                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;
        }

相關(guān)文章

  • 淺析MVP模式中V-P交互問題及案例分享

    淺析MVP模式中V-P交互問題及案例分享

    如果從層次關(guān)系來講,MVP屬于Presentation層的設(shè)計(jì)模式。對(duì)于一個(gè)UI模塊來說,它的所有功能被分割為三個(gè)部分,分別通過Model、View和Presenter來承載。Model、View和Presenter相互協(xié)作,完成對(duì)最初數(shù)據(jù)的呈現(xiàn)和對(duì)用戶操作的響應(yīng),它們具有各自的職責(zé)劃分。
    2014-05-05
  • CentOS上運(yùn)行ZKEACMS的詳細(xì)過程

    CentOS上運(yùn)行ZKEACMS的詳細(xì)過程

    這篇文章主要為大家介紹了CentOS上運(yùn)行ZKEACMS的詳細(xì)過程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • HTML服務(wù)器控件和WEB服務(wù)器控件的區(qū)別和聯(lián)系介紹

    HTML服務(wù)器控件和WEB服務(wù)器控件的區(qū)別和聯(lián)系介紹

    學(xué)習(xí)asp.net的時(shí)候一會(huì)用Html服務(wù)器控件,一會(huì)用Web服務(wù)器控件,起初做起例子來也挺迷糊的,下面對(duì)這兩個(gè)控件研究了一下做個(gè)筆記在此與大家分享下,感興趣的朋友可以了解下
    2013-08-08
  • asp.net TextBox控件設(shè)置ReadOnly后,不能回傳。

    asp.net TextBox控件設(shè)置ReadOnly后,不能回傳。

    當(dāng)把一個(gè)TextBox控件ReadOnly屬性設(shè)置為True后,這個(gè)控件就不回傳了。
    2009-05-05
  • .NET示波器控件的實(shí)例代碼分析

    .NET示波器控件的實(shí)例代碼分析

    本篇文章是對(duì).NET示波器控件進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • .net獲取本機(jī)公網(wǎng)IP地址示例

    .net獲取本機(jī)公網(wǎng)IP地址示例

    本文主要介紹了.net獲取本機(jī)公網(wǎng)IP地址的方法,使用了ip138的數(shù)據(jù),大家參考使用吧
    2014-01-01
  • 使用ajax局部刷新gridview進(jìn)行數(shù)據(jù)綁定示例

    使用ajax局部刷新gridview進(jìn)行數(shù)據(jù)綁定示例

    很多用戶都有這樣需求,比如:點(diǎn)擊按鈕,刷新 GridView 中的數(shù)據(jù),而不是這個(gè)頁面刷新。使用簡單的 XMLHttpRequest就可以直接實(shí)現(xiàn)
    2014-02-02
  • asp.net c#采集需要登錄頁面的實(shí)現(xiàn)原理及代碼

    asp.net c#采集需要登錄頁面的實(shí)現(xiàn)原理及代碼

    當(dāng)我們采集頁面的時(shí)候,如果被采集的網(wǎng)站需要登錄才能采集,原理搞清楚了,就好辦了,我們所要做的僅僅是在采集的時(shí)候(或者說HttpWebRequest提交數(shù)據(jù)的時(shí)候),將Cookie信息放入Http請(qǐng)求頭里面就可以了,感興趣的朋友可以了解下,或許對(duì)你有所幫助
    2013-02-02
  • 輕量級(jí)ORM框架Dapper應(yīng)用之安裝Dapper

    輕量級(jí)ORM框架Dapper應(yīng)用之安裝Dapper

    這篇文章介紹了輕量級(jí)ORM框架Dapper的安裝方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-03-03
  • 淺談ASP.NET中多層架構(gòu)

    淺談ASP.NET中多層架構(gòu)

    Asp.net的多層架構(gòu)主要是為了解決數(shù)據(jù)層,邏輯層,表示層等之間的關(guān)系。我的做法是這樣的:首先建立一個(gè)DataCore的基類。基類里面封裝了一些低層的數(shù)據(jù)庫的基本操作,比如說數(shù)據(jù)庫聯(lián)接,調(diào)用存儲(chǔ)過程等等。
    2015-06-06

最新評(píng)論

潍坊市| 黑河市| 开平市| 治县。| 平度市| 房产| 峨山| 洪雅县| 南靖县| 榆树市| 峨山| 永德县| 庆安县| 来安县| 长顺县| 沐川县| 饶阳县| 长宁区| 泽普县| 金坛市| 丰宁| 阜宁县| 睢宁县| 丹东市| 苍山县| 潜江市| 贡嘎县| 思南县| 通海县| 乐山市| 鹤山市| 胶南市| 齐齐哈尔市| 贞丰县| 乳山市| 扶余县| 吉林省| 巩义市| 桐城市| 临潭县| 益阳市|