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

簡(jiǎn)單的excel導(dǎo)入導(dǎo)出示例分享

 更新時(shí)間:2014年03月04日 09:15:10   作者:  
這篇文章主要介紹了簡(jiǎn)單的excel導(dǎo)入導(dǎo)出示例分享,需要的朋友可以參考下

復(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è)是下載到桌面的方法,沒(méi)實(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";
               //通知瀏覽器下載文件而不是打開(kāi)
            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() == "簡(jiǎn)稱" &&
                      firstRow.GetCell(2).ToString() == "分類" && firstRow.GetCell(3).ToString() == "參考價(jià)" &&
                      firstRow.GetCell(4).ToString() == "商品介紹"))
                {
                    return false;
                }


                //跳過(guò)類型不正確的品項(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)文章

  • c# 線程定時(shí)器 System.Threading.Timer的使用

    c# 線程定時(shí)器 System.Threading.Timer的使用

    本文主要介紹了c# 線程定時(shí)器 System.Threading.Timer的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • C#中的協(xié)變與逆變方式

    C#中的協(xié)變與逆變方式

    協(xié)變和逆變是C#中處理泛型類型參數(shù)可變性的兩個(gè)重要概念,協(xié)變?cè)试S將派生類型的泛型參數(shù)轉(zhuǎn)換為基類型的泛型參數(shù),而逆變?cè)试S將基類型的泛型參數(shù)轉(zhuǎn)換為派生類型的泛型參數(shù),通過(guò)協(xié)變和逆變,可以提高代碼的靈活性和可重用性,但也需要注意類型參數(shù)的限制和安全性
    2024-12-12
  • C#定義簡(jiǎn)單的反射工廠實(shí)例分析

    C#定義簡(jiǎn)單的反射工廠實(shí)例分析

    這篇文章主要介紹了C#定義簡(jiǎn)單的反射工廠的用法,實(shí)例分析了反射工廠的原理與使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • C#中序列化實(shí)現(xiàn)深拷貝,實(shí)現(xiàn)DataGridView初始化刷新的方法

    C#中序列化實(shí)現(xiàn)深拷貝,實(shí)現(xiàn)DataGridView初始化刷新的方法

    下面小編就為大家?guī)?lái)一篇C#中序列化實(shí)現(xiàn)深拷貝,實(shí)現(xiàn)DataGridView初始化刷新的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • Unity Sockect實(shí)現(xiàn)畫面實(shí)時(shí)傳輸案例原理解析

    Unity Sockect實(shí)現(xiàn)畫面實(shí)時(shí)傳輸案例原理解析

    Socket是比較常用的一種通信方式,本文通過(guò)案例給大家介紹Unity Sockect實(shí)現(xiàn)畫面實(shí)時(shí)傳輸功能,感興趣的朋友一起看看吧
    2021-08-08
  • WPF中的導(dǎo)航框架概述

    WPF中的導(dǎo)航框架概述

    這篇文章介紹了WPF中的導(dǎo)航框架,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • .NET使用C#導(dǎo)入Excel文件數(shù)據(jù)到數(shù)據(jù)庫(kù)

    .NET使用C#導(dǎo)入Excel文件數(shù)據(jù)到數(shù)據(jù)庫(kù)

    將Excel文件中的數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)中不僅能夠提升數(shù)據(jù)處理的效率和準(zhǔn)確性,還能極大地促進(jìn)數(shù)據(jù)分析和決策制定的過(guò)程,本文將介紹如何在.NET平臺(tái)使用C#導(dǎo)入Excel文件數(shù)據(jù)到數(shù)據(jù)庫(kù)中,需要的可以參考下
    2024-12-12
  • 使用C#發(fā)送帶附件的電子郵件的方法的代碼示例分析

    使用C#發(fā)送帶附件的電子郵件的方法的代碼示例分析

    這篇文章主要介紹了使用C#發(fā)送帶附件的電子郵件的方法的代碼示例分析,文中還提到了利用163的SMTP服務(wù)器發(fā)郵件的方法,需要的朋友可以參考下
    2016-02-02
  • unity置灰處理的實(shí)現(xiàn)

    unity置灰處理的實(shí)現(xiàn)

    本文主要介紹了unity置灰處理的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • .Net(c#)漢字和Unicode編碼互相轉(zhuǎn)換實(shí)例

    .Net(c#)漢字和Unicode編碼互相轉(zhuǎn)換實(shí)例

    下面小編就為大家?guī)?lái)一篇.Net(c#)漢字和Unicode編碼互相轉(zhuǎn)換實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02

最新評(píng)論

凉城县| 松阳县| 北流市| 石景山区| 金平| 饶河县| 乐安县| 松滋市| 曲靖市| 盐边县| 龙州县| 正安县| 眉山市| 广丰县| 曲周县| 巨鹿县| 酒泉市| 呈贡县| 黑河市| 武川县| 湟中县| 小金县| 淅川县| 天等县| 汾西县| 凯里市| 凭祥市| 友谊县| 电白县| 慈溪市| 邯郸县| 彭泽县| 民勤县| 新野县| 昭觉县| 濮阳县| 庆元县| 绥宁县| 平阳县| 商河县| 海晏县|