主頁(yè) > 知識(shí)庫(kù) > asp.net導(dǎo)出excel的簡(jiǎn)單方法實(shí)例

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

熱門(mén)標(biāo)簽:騰訊地圖標(biāo)注手機(jī) 柳州電銷機(jī)器人公司 昆明語(yǔ)音電銷機(jī)器人價(jià)格 太原400電話上門(mén)辦理 百度地圖怎樣做地圖標(biāo)注 征途美甲店地圖標(biāo)注 電銷語(yǔ)音機(jī)器人型號(hào)參數(shù) 400電話如何申請(qǐng)取消 浦發(fā)電話機(jī)器人提醒還款

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

本例使用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è)是下載到桌面的方法,沒(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;
        }

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

標(biāo)簽:白山 新疆 蘭州 德陽(yáng) 江蘇 張家界 天門(mén) 陽(yáng)泉

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net導(dǎo)出excel的簡(jiǎn)單方法實(shí)例》,本文關(guān)鍵詞  asp.net,導(dǎo)出,excel,的,簡(jiǎn)單,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《asp.net導(dǎo)出excel的簡(jiǎn)單方法實(shí)例》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于asp.net導(dǎo)出excel的簡(jiǎn)單方法實(shí)例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章