好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > asp.net導出excel的簡單方法實例

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

熱門標簽:騰訊地圖標注手機 柳州電銷機器人公司 昆明語音電銷機器人價格 太原400電話上門辦理 百度地圖怎樣做地圖標注 征途美甲店地圖標注 電銷語音機器人型號參數 400電話如何申請取消 浦發電話機器人提醒還款

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;
        }

您可能感興趣的文章:
  • ASP.NET使用GridView導出Excel實現方法
  • asp.net導出excel數據的常見方法匯總
  • ASP.NET導出數據到Excel的實現方法
  • Asp.net中DataTable導出到Excel的方法介紹
  • ASP.NET用DataSet導出到Excel的方法
  • .Net中導出數據到Excel(asp.net和winform程序中)
  • asp.net生成Excel并導出下載五種實現方法
  • asp.net Grid 導出Excel實現程序代碼
  • asp.net GridView導出到Excel代碼
  • ASP.NET 導出到Excel時保留換行的代碼
  • asp.net實現Gradview綁定數據庫數據并導出Excel的方法

標簽:白山 新疆 蘭州 德陽 江蘇 張家界 天門 陽泉

巨人網絡通訊聲明:本文標題《asp.net導出excel的簡單方法實例》,本文關鍵詞  asp.net,導出,excel,的,簡單,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《asp.net導出excel的簡單方法實例》相關的同類信息!
  • 本頁收集關于asp.net導出excel的簡單方法實例的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 把胸罩扒了狂揉gif动态图| 黄色在线免费观看| 伊人色综合网一区二区三区| 日韩AV午夜福利在线观看| 蜜芽768mon一区忘忧草| 久久中文字幕日韩精品| 日本漫画工番口番全彩免费bd| 天天射天天做| 日本无码H肉动漫在线先下载| 麻豆午夜视频| 深夜在线观看| 伊人АV影院| 中文电影网| 污污的黄文| 小s货再浪些再咬紧点h| 欧洲人A片在7777| 啪啪国产视频| 岛国AV无码精品一区二区三区| 日韩国产精品欧美一区二区| 大胸美女脱个精光视频| 131美女做爰视频网站| 丁香综合缴情六月婷婷| 国产福利免费视频| 乡野欲潮76章太硬了大壮 | chinesehd国语普通话对白| 韩国电影毛片| 69人妻偷拍??熟女丝| 国产真实乱人视频| 国产成人久久精品激情91| 午夜剧场18岁免进| 亚洲日韩在线| G奶巨爆乳美女少妇| 黄色片免费试看| 黄色小视频下载| 色天使色| 双性少年的假期调教H| 羞羞视频麻豆| 真精华布衣天下123456今天开奖号| 高辣h文乱乳H文浪荡护士视频| 和野男人打野战好爽| 嫩模尺度私拍在线视频|