欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

mvc如何導(dǎo)入excel-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)mvc如何導(dǎo)入excel的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

在盤錦等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站制作、做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作按需求定制網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,成都全網(wǎng)營銷推廣,外貿(mào)營銷網(wǎng)站建設(shè),盤錦網(wǎng)站建設(shè)費(fèi)用合理。

準(zhǔn)備工作:

1.在項(xiàng)目中添加對(duì)NPOI的引用,NPOI下載地址:http://npoi.codeplex.com/releases/view/38113

2.NPOI學(xué)習(xí)

NPOI下載,里面有五個(gè)dll,需要引用到你的項(xiàng)目,我這邊用的mvc4+三層的方式架構(gòu)的項(xiàng)目

我用的工具是(vs2012+sql2014)

準(zhǔn)備工作做完,我們開始進(jìn)入主題

1.前端頁面,代碼:

<div class="filebtn"> 
        @using (Html.BeginForm("importexcel", "foot", FormMethod.Post, new { enctype = "multipart/form-data" }))
          {
            <samp>請(qǐng)選擇要上傳的Excel文件:</samp>
            <span id="txt_Path"></span>
            <strong>選擇文件<input name="file" type="file" id="file" /></strong>@*
            @Html.AntiForgeryToken() //防止跨站請(qǐng)求偽造(CSRF:Cross-site request forgery)攻擊
           *@<input type="submit" id="ButtonUpload" value="提交"  class="offer"/> 
          }
      </div>

2.接下來就是控制器

public class footController : Controller
  {
    //
    // GET: /foot/
    private static readonly String Folder = "/files";
    public ActionResult excel()
    {
      return View();
    }

    /// 導(dǎo)入excel文檔
    public ActionResult importexcel()
    {
      //1.接收客戶端傳過來的數(shù)據(jù)
      HttpPostedFileBase file = Request.Files["file"];
      if (file == null || file.ContentLength <= 0)
      {
        return Json("請(qǐng)選擇要上傳的Excel文件", JsonRequestBehavior.AllowGet);
      }
      //string filepath = Server.MapPath(Folder);
      //if (!Directory.Exists(filepath))
      //{
      //  Directory.CreateDirectory(filepath);
      //}
      //var fileName = Path.Combine(filepath, Path.GetFileName(file.FileName));
      // file.SaveAs(fileName);
      //獲取一個(gè)streamfile對(duì)象,該對(duì)象指向一個(gè)上傳文件,準(zhǔn)備讀取改文件的內(nèi)容
      Stream streamfile = file.InputStream;
      DataTable dt = new DataTable();
      string FinName = Path.GetExtension(file.FileName);
      if (FinName != ".xls" && FinName != ".xlsx")
      {
        return Json("只能上傳Excel文檔",JsonRequestBehavior.AllowGet);
      }
      else
      {
        try
        {
          if (FinName == ".xls")
          {
            //創(chuàng)建一個(gè)webbook,對(duì)應(yīng)一個(gè)Excel文件(用于xls文件導(dǎo)入類)
            HSSFWorkbook hssfworkbook = new HSSFWorkbook(streamfile);
            dt = excelDAL.ImExport(dt, hssfworkbook);
          }
          else
          {
            XSSFWorkbook hssfworkbook = new XSSFWorkbook(streamfile);
            dt = excelDAL.ImExport(dt, hssfworkbook);
          }
          return Json("",JsonRequestBehavior.AllowGet);
        }
        catch(Exception ex)
        {
          return Json("導(dǎo)入失敗 !"+ex.Message, JsonRequestBehavior.AllowGet);
        }
    }
      
    }

}

3.業(yè)務(wù)邏輯層[excelDAL]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using System.Data;
using NPOI.XSSF.UserModel;

namespace GJL.Compoent
{
  public class excelDAL
  {
    ///<summary>
    /// #region 兩種不同版本的操作excel
    /// 擴(kuò)展名*.xlsx
    /// </summary>
    public static DataTable ImExport(DataTable dt, XSSFWorkbook hssfworkbook)
    {
      NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt(0);
      System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
      for (int j = 0; j < (sheet.GetRow(0).LastCellNum); j++)
      {
        dt.Columns.Add(sheet.GetRow(0).Cells[j].ToString());
      }
      while (rows.MoveNext())
      {
        XSSFRow row = (XSSFRow)rows.Current;
        DataRow dr = dt.NewRow();
        for (int i = 0; i < row.LastCellNum; i++)
        {
          NPOI.SS.UserModel.ICell cell = row.GetCell(i);
          if (cell == null)
          {
            dr[i] = null;
          }
          else
          {
            dr[i] = cell.ToString();
          }
        }
        dt.Rows.Add(dr);
      }
      dt.Rows.RemoveAt(0);
      if (dt!=null && dt.Rows.Count != 0)
      {
        for (int i = 0; i < dt.Rows.Count; i++)
        {
          string categary = dt.Rows[i]["頁面"].ToString();
          string fcategary = dt.Rows[i]["分類"].ToString();
          string fTitle = dt.Rows[i]["標(biāo)題"].ToString();
          string fUrl = dt.Rows[i]["鏈接"].ToString();
          FooterDAL.Addfoot(categary, fcategary, fTitle, fUrl);
        }
      }
      return dt;
    }

    #region 兩種不同版本的操作excel
    ///<summary>
    /// 擴(kuò)展名*.xls
    /// </summary>
    public static DataTable ImExport(DataTable dt, HSSFWorkbook hssfworkbook)
    {
      // 在webbook中添加一個(gè)sheet,對(duì)應(yīng)Excel文件中的sheet,取出第一個(gè)工作表,索引是0 
      NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt(0);
      System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
      for (int j = 0; j < (sheet.GetRow(0).LastCellNum); j++)
      {
        dt.Columns.Add(sheet.GetRow(0).Cells[j].ToString());
      }
      while (rows.MoveNext())
      {
        HSSFRow row = (HSSFRow)rows.Current;
        DataRow dr = dt.NewRow();
        for (int i = 0; i < row.LastCellNum; i++)
        {
          NPOI.SS.UserModel.ICell cell = row.GetCell(i);
          if (cell == null)
          {
            dr[i] = null;
          }
          else 
          {
            dr[i] = cell.ToString();
          }
        }
        dt.Rows.Add(dr);
      }
      dt.Rows.RemoveAt(0);
      if (dt != null && dt.Rows.Count != 0)
      {
        for (int i = 0; i < dt.Rows.Count; i++)
        {
          string categary = dt.Rows[i]["頁面"].ToString();
          string fcategary = dt.Rows[i]["分類"].ToString();
          string fTitle = dt.Rows[i]["標(biāo)題"].ToString();
          string fUrl = dt.Rows[i]["鏈接"].ToString();
          FooterDAL.Addfoot(categary, fcategary, fTitle, fUrl);
        }

      }
      return dt;
    }
    #endregion
  }
}
 public static partial class FooterDAL
  {
    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="id"></param>
    /// <param name="catgary"></param>
    /// <param name="fcatgary"></param>
    /// <param name="fTitle"></param>
    /// <param name="fUrl"></param>
    /// <returns></returns>
    public static int Addfoot(string categary, string fcategary, string fTitle, string fUrl)
    {
      string sql = string.Format("insert into Foot (categary,fcategary,fTitle,fUrl)values(@categary,@fcategary,@fTitle,@fUrl)");
      SqlParameter[] parm = 
        { 
           new SqlParameter("@categary",categary)
          ,new SqlParameter("@fcategary",fcategary)
          ,new SqlParameter("@fTitle",fTitle)
          ,new SqlParameter("@fUrl",fUrl)
        };
      return new DBHelperSQL<Foot>(CommonTool.dbname).ExcuteSql(sql,parm);  
    }
}

//FooterDAL將datatable,就是excel里面的數(shù)據(jù)添加到sql數(shù)據(jù)庫

感謝各位的閱讀!關(guān)于“mvc如何導(dǎo)入excel”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

文章標(biāo)題:mvc如何導(dǎo)入excel-創(chuàng)新互聯(lián)
本文地址:http://www.chinadenli.net/article30/dhcjpo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)網(wǎng)站設(shè)計(jì)公司網(wǎng)站維護(hù)網(wǎng)站營銷網(wǎng)頁設(shè)計(jì)公司移動(dòng)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)