国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當(dāng)前位置: 首頁 - 科技 - 知識(shí)百科 - 正文

手把手教你mvc導(dǎo)入excel

來源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:35:13
文檔

手把手教你mvc導(dǎo)入excel

手把手教你mvc導(dǎo)入excel:準(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)
推薦度:
導(dǎo)讀手把手教你mvc導(dǎo)入excel:準(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)

準(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ù)庫

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

手把手教你mvc導(dǎo)入excel

手把手教你mvc導(dǎo)入excel:準(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)
推薦度:
  • 熱門焦點(diǎn)

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 国产精品亚洲国产三区 | 亚洲午夜久久久久中文字幕 | 欧美极品欧美日韩 | 日韩在线专区 | 日韩精品在线观看免费 | 国产在线观看免费 | 成a人片亚洲日本久久 | 欧美日韩精品免费一区二区三区 | 欧美高清在线不卡免费观看 | 国产成人久久 | 免费黄色网址在线观看 | 小说区 亚洲 自拍 另类 | 国内不卡1区2区 | 国产精品手机视频一区二区 | 国产精品电影一区二区 | 日韩欧美一区二区三区在线视频 | 91亚洲国产成人久久精品网址 | 欧美三级一区二区 | 国产亚洲三级 | 在线亚洲精品国产成人二区 | 日韩高清一区 | 国产高清一区二区三区 | 欧美在线一区二区三区 | 久久一区二区三区四区 | 美日韩免费视频 | 国产一在线 | 欧美激情网站 | 韩国一区二区三区 | 国产真实乱人视频在线看 | 欧美αv日韩αv另类综合 | 91大神在线观看精品一区 | 91精品观看91久久久久久 | 日本久热 | 日韩免费一区二区三区 | 日韩精品一区二区三区 在线观看 | 国产一区二区精品久久凹凸 | 亚洲黄色一区二区 | 亚洲国产成人久久综合碰 | 北条麻妃手机在线 | 日本不卡视频在线观看 | 中国特黄毛片 |