ما با استفاده از ارائه دهنده داده OLEDB.NET به یک کتاب کار Microsoft Excel متصل می شویم، داده ها را استخراج می کنیم و سپس داده ها را در یک View نمایش می دهیم.
ابتدا یک ImportExcel در Home Controller ایجاد می کنیم که یک View را برمی گرداند. این روش یک View برای دریافت درخواست برمی گرداند. اکنون روش دیگری ImportExcel1 ایجاد می کنیم و آن را با ویژگی [HttpPost] تزئین می کنیم. از آنجایی که در MVC 2 Methods نمیتواند نام متد یکسانی داشته باشد، میتوانیم 2 عمل را با استفاده از ویژگی Action Name فراخوانی کنیم. بنابراین ImportExcel1 را با [ActionName("Importexcel")] تزئین می کنیم. حالا اگر یک درخواست دریافت کنیم، ImportExcel و برای درخواست پست ImportExcel1 فراخوانی می شود. کد زیر برای خواندن فایل های اکسل است.
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.OleDb;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using ExcelUpload.Models;
- namespace ExcelUpload.Controllers
- {
- public class HomeController : Controller
- {
- public ActionResult ImportExcel()
- {
- return View();
- }
- [ActionName("Importexcel")]
- [HttpPost]
- public ActionResult Importexcel1()
- {
- if (Request.Files["FileUpload1"].ContentLength > 0)
- {
- string extension = System.IO.Path.GetExtension(Request.Files["FileUpload1"].FileName).ToLower();
- string query = null;
- string connString = "";
- string[] validFileTypes = { ".xls", ".xlsx", ".csv" };
- string path1 = string.Format("{0}/{1}", Server.MapPath("~/Content/Uploads"), Request.Files["FileUpload1"].FileName);
- if (!Directory.Exists(path1))
- {
- Directory.CreateDirectory(Server.MapPath("~/Content/Uploads"));
- }
- if (validFileTypes.Contains(extension))
- {
- if (System.IO.File.Exists(path1))
- { System.IO.File.Delete(path1); }
- Request.Files["FileUpload1"].SaveAs(path1);
- if(extension==".csv")
- {
- DataTable dt= Utility.ConvertCSVtoDataTable(path1);
- ViewBag.Data = dt;
- }
- //Connection String to Excel Workbook
- else if (extension.Trim() == ".xls")
- {
- connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path1 + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
- DataTable dt = Utility.ConvertXSLXtoDataTable(path1,connString);
- ViewBag.Data = dt;
- }
- else if (extension.Trim() == ".xlsx")
- {
- connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path1 + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
- DataTable dt = Utility.ConvertXSLXtoDataTable(path1, connString);
- ViewBag.Data = dt;
- }
- }
- else
- {
- ViewBag.Error = "Please Upload Files in .xls, .xlsx or .csv format";
- }
- }
- return View();
- }
- }
- }
در اینجا ما یک کلاس استاتیک Utility ایجاد کرده ایم که شامل 2 متد ConvertCSVtoDataTable و ConvertXSLXtoDataTable است. کد زیر برای کلاس Utility است.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Data.SqlClient;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Data.OleDb;
- namespace ExcelUpload.Models
- {
- public static class Utility
- {
- public static DataTable ConvertCSVtoDataTable(string strFilePath)
- {
- DataTable dt = new DataTable();
- using (StreamReader sr = new StreamReader(strFilePath))
- {
- string[] headers = sr.ReadLine().Split(',');
- foreach (string header in headers)
- {
- dt.Columns.Add(header);
- }
- while (!sr.EndOfStream)
- {
- string[] rows = sr.ReadLine().Split(',');
- if (rows.Length > 1)
- {
- DataRow dr = dt.NewRow();
- for (int i = 0; i < headers.Length; i++)
- {
- dr[i] = rows[i].Trim();
- }
- dt.Rows.Add(dr);
- }
- }
- }
- return dt;
- }
- public static DataTable ConvertXSLXtoDataTable(string strFilePath,string connString)
- {
- OleDbConnection oledbConn = new OleDbConnection(connString);
- DataTable dt=new DataTable();
- try
- {
- oledbConn.Open();
- using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn))
- {
- OleDbDataAdapter oleda = new OleDbDataAdapter();
- oleda.SelectCommand = cmd;
- DataSet ds = new DataSet();
- oleda.Fill(ds);
- dt= ds.Tables[0];
- }
- }
- catch
- {
- }
- finally
- {
- oledbConn.Close();
- }
- return dt;
- }
- }
- }
اکنون یک نمای ایجاد می کنیم که شامل کنترل آپلود فایل و یک دکمه است. هنگامی که درخواست ImportExcel از Home Controller انجام می شود، کنترل آپلود فایل را با کنترل دکمه نشان خواهیم داد. وقتی فایلی را انتخاب می کنیم و دکمه را فشار می دهیم، یک درخواست ارسال به Home Controller می دهد و روش ImportExcel1 فراخوانی می شود. در زیر نمای Razor برای هر دو درخواست است.
- @using System.Data;
- @{
- ViewBag.Title = "ImportExcel";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- <h2>ImportExcel</h2>
- <!--[if !IE]><!-->
- <style type="text/css">
- /* Generic Styling, for Desktops/Laptops */
- table {
- width: 100%;
- border-collapse: collapse;
- }
- /* Zebra striping */
- tr:nth-of-type(odd) {
- background: #eee;
- }
- th {
- background: #333;
- color: white;
- font-weight: bold;
- }
- td, th {
- padding: 6px;
- border: 1px solid #ccc;
- text-align: left;
- }
- /*
- Max width before this PARTICULAR table gets nasty
- This query will take effect for any screen smaller than 760px
- and also iPads specifically.
- */
- @@media only screen and (max-width: 760px),
- (min-device-width: 768px) and (max-device-width: 1024px) {
- /* Force table to not be like tables anymore */
- table, thead, tbody, th, td, tr {
- display: block;
- }
- /* Hide table headers (but not display: none;, for accessibility) */
- thead tr {
- position: absolute;
- top: -9999px;
- left: -9999px;
- }
- tr { border: 1px solid #ccc; }
- td {
- /* Behave like a "row" */
- border: none;
- border-bottom: 1px solid #eee;
- position: relative;
- padding-left: 50%;
- }
- td:before {
- /* Now like a table header */
- position: absolute;
- /* Top/left values mimic padding */
- top: 6px;
- left: 6px;
- width: 45%;
- padding-right: 10px;
- white-space: nowrap;
- }
- /*
- Label the data
- */
- td:before {
- content: attr(data-title);
- }
- }
- </style>
- <!--<![endif]-->
- @using (Html.BeginForm("ImportExcel","Home",FormMethod.Post,new { enctype = "multipart/form-data" } ))
- {
- <table>
- <tr><td>Excel file</td><td><input type="file" id="FileUpload1" name="FileUpload1" /></td></tr>
- <tr><td></td><td><input type="submit" id="Submit" name="Submit" value="Submit" /></td></tr>
- </table>
- }
- <div>
- <table id="">
- @if (ViewBag.Data != null)
- {
- <thead>
- @foreach (DataColumn column in (ViewBag.Data as System.Data.DataTable).Columns)
- {
- <th>@column.ColumnName.ToUpper()</th>
- }
- </thead>
- if ((ViewBag.Data as System.Data.DataTable).Rows.Count > 0)
- {
- foreach (DataRow dr in (ViewBag.Data as System.Data.DataTable).Rows)
- {
- <tr>
- @foreach (DataColumn column in (ViewBag.Data as System.Data.DataTable).Columns)
- {
- <td data-title='@column.ColumnName'>
- @dr[column].ToString()
- </td>
- }
- </tr>
- }
- }
- else
- {
- int count = (ViewBag.Data as System.Data.DataTable).Columns.Count;
- <tr>
- <td colspan='@count' style="color:red;" >
- No Data Found.
- </td>
- </tr>
- }
- }
- else
- {
- if (ViewBag.Error != null)
- {
- <tr>
- <td style = "color:red;" >
- @(ViewBag.Error != null ? ViewBag.Error.ToString() : "")
- </td >
- </tr >
- }
- }
- </table>
- </div>