ASP.NET MVC3.0 上传文件并导入
2013-01-14 10:06
197 查看
View
@model BSServer.Models.PagerIndexModel
@{
ViewBag.Title = "站点信息一览";
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>站点信息一览</title>
<link href="@Url.Content("~/Content/style/TableCss.css")" rel="stylesheet" type="text/css"/>
</head>
<body>
<fieldset style="border-color: #B1CDE3">
<p>
@Html.ActionLink("新增站点 ", "AddSiteInfo", "SiteManage")
@Html.ActionLink("导出站点信息 ", "ExportSiteInfo", "SiteManage", new { style = "Color: #46A3FF;" })
</p>
<p>
@if (ViewBag.Flag == 1)
{
<script type="text/javascript">alert('导入成功!')</script>
}
@using (Html.BeginForm("ImportSiteInfo", "SiteManage", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.Label("导入站点")<input id="fileSite" name="fileSite" type="file" title="导入站点信息" style="color: #46A3FF;" />
<input type="submit" value="导入" style="background-color: #B1CDE3" />
}
</p>
<p>
@using (Html.BeginForm("SiteInfoList", "SiteManage"))
{
<label id="localName">
区域名称</label><input id="txtLocalName" name="txtLocalName" value="@ViewBag.LocalName" type="text" maxlength='20'/>
<label id="locName">
站点名称</label><input id="txtSiteName" name="txtSiteName" value="@ViewBag.SiteName" type="text" maxlength='20'/>
<input type="submit" value="查询" style="background-color: #B1CDE3" />
}
</p>
<table width="100%">
<tr>
<td style="background-color: #B1CDE3">
站点ID
</td>
<td style="background-color: #B1CDE3">
区域
</td>
<td style="background-color: #B1CDE3">
站点名称
</td>
<td style="background-color: #B1CDE3">
操作
</td>
</tr>
@foreach (var site in Model.SiteInfomationList)
{
<tr>
<td>
@Html.DisplayFor(modelItem => site.ID)
</td>
<td>
@if (site.LocalID != -1)
{
@:@(BSServer.Business.BasicInfo.SiteManageBusiness.GetLocalNameByLocalId(site.LocalID))
}
else
{
@:@(string.Empty)
}
</td>
<td>
@Html.DisplayFor(modelItem => site.Name)
</td>
<td>
@Html.ActionLink("查看", "SiteInfoDetails", new { id = site.ID }) |
@Html.ActionLink("编辑", "EditeSiteInfo", new { id = site.ID }) |
@Html.ActionLink("删除", "DeleteSite", new { siteId = site.ID }, new { onclick = "return confirm('是否要删除?');" })
</td>
</tr>
}
<tr>
<td colspan='4' align='right'>
@Html.Partial("_Page", Model)
</td>
</tr>
</table>
</fieldset>
</body>
</html>
Controller
/// <summary>
/// 站点控制器
/// </summary>
public class SiteManageController : Controller
{
/// <summary>
/// 导入Excel方法
/// </summary>
/// <param name="fileSite"></param>
/// <returns></returns>
public ActionResult ImportSiteInfo()
{
try
{
#region 文件上传
//文件路径
string path = string.Empty;
//文件名称
string filename=string.Empty;
//文件与路径
string filePath = string.Empty;
foreach (string upload in Request.Files)
{
//判断当前上传控件是否为空
if (Request.Files.Count > 0)
{
//判断当前上传控件是否为空
if (Request.Files[upload] != null && Request.Files[upload].ContentLength > 0)
//当前工程路径与上传文件夹
path = AppDomain.CurrentDomain.BaseDirectory + "uploadSite/";
//文件名称
filename = Path.GetFileName(Request.Files[upload].FileName);
//文件与路径
filePath = Path.Combine(path, filename);
//保存在当前工程文件夹
Request.Files[upload].SaveAs(filePath);
}
}
#endregion
#region 导入文件
//判断是否为空导入
if (string.IsNullOrEmpty(path))
{
return RedirectToAction("SiteInfoList", "SiteManage");
}
else
{
//判断当前文件是否为Excel
if (!filename.Contains("xls"))
return RedirectToAction("SiteInfoList", "SiteManage");
}
ImportExcel importEx = new ImportExcel();
//获取Excel中的数据
DataTable dtSite = importEx.ExcelDataSource(filePath, "Sheet1").Tables[0];
//添加到数据库
foreach (DataRow item in dtSite.Rows)
{
Site s = new Site()
{
Name = item["Name"].ToString().Trim(),
LocalID = int.Parse(item["LocalID"].ToString().Trim())
};
//判断当前站点是否存在
if (!string.IsNullOrEmpty(SiteManageBusiness.GetSiteNameByName(s.Name)))
continue;
SiteManageBusiness.AddSiteInfo(s);
}
#endregion
return RedirectToAction("SiteInfoList", "SiteManage", new { import = 1 });
}
catch (Exception ex)
{
return RedirectToAction("SystemError", "SysException", new { ExInfo = "堆栈消息:" + ex.StackTrace + "\n\n 异常消息:" + ex.Message });
}
}
}
Common
public class ImportExcel
{
/// <summary>
/// 获取Excel数据方法
/// </summary>
/// <param name="filepath">文件路径</param>
/// <param name="sheetname">sheet名称</param>
/// <returns></returns>
public DataSet ExcelDataSource(string filepath, string sheetname)
{
try
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "$]", strConn);
DataSet ds = new DataSet();
oada.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw ex;
}
}
}
@model BSServer.Models.PagerIndexModel
@{
ViewBag.Title = "站点信息一览";
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>站点信息一览</title>
<link href="@Url.Content("~/Content/style/TableCss.css")" rel="stylesheet" type="text/css"/>
</head>
<body>
<fieldset style="border-color: #B1CDE3">
<p>
@Html.ActionLink("新增站点 ", "AddSiteInfo", "SiteManage")
@Html.ActionLink("导出站点信息 ", "ExportSiteInfo", "SiteManage", new { style = "Color: #46A3FF;" })
</p>
<p>
@if (ViewBag.Flag == 1)
{
<script type="text/javascript">alert('导入成功!')</script>
}
@using (Html.BeginForm("ImportSiteInfo", "SiteManage", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.Label("导入站点")<input id="fileSite" name="fileSite" type="file" title="导入站点信息" style="color: #46A3FF;" />
<input type="submit" value="导入" style="background-color: #B1CDE3" />
}
</p>
<p>
@using (Html.BeginForm("SiteInfoList", "SiteManage"))
{
<label id="localName">
区域名称</label><input id="txtLocalName" name="txtLocalName" value="@ViewBag.LocalName" type="text" maxlength='20'/>
<label id="locName">
站点名称</label><input id="txtSiteName" name="txtSiteName" value="@ViewBag.SiteName" type="text" maxlength='20'/>
<input type="submit" value="查询" style="background-color: #B1CDE3" />
}
</p>
<table width="100%">
<tr>
<td style="background-color: #B1CDE3">
站点ID
</td>
<td style="background-color: #B1CDE3">
区域
</td>
<td style="background-color: #B1CDE3">
站点名称
</td>
<td style="background-color: #B1CDE3">
操作
</td>
</tr>
@foreach (var site in Model.SiteInfomationList)
{
<tr>
<td>
@Html.DisplayFor(modelItem => site.ID)
</td>
<td>
@if (site.LocalID != -1)
{
@:@(BSServer.Business.BasicInfo.SiteManageBusiness.GetLocalNameByLocalId(site.LocalID))
}
else
{
@:@(string.Empty)
}
</td>
<td>
@Html.DisplayFor(modelItem => site.Name)
</td>
<td>
@Html.ActionLink("查看", "SiteInfoDetails", new { id = site.ID }) |
@Html.ActionLink("编辑", "EditeSiteInfo", new { id = site.ID }) |
@Html.ActionLink("删除", "DeleteSite", new { siteId = site.ID }, new { onclick = "return confirm('是否要删除?');" })
</td>
</tr>
}
<tr>
<td colspan='4' align='right'>
@Html.Partial("_Page", Model)
</td>
</tr>
</table>
</fieldset>
</body>
</html>
Controller
/// <summary>
/// 站点控制器
/// </summary>
public class SiteManageController : Controller
{
/// <summary>
/// 导入Excel方法
/// </summary>
/// <param name="fileSite"></param>
/// <returns></returns>
public ActionResult ImportSiteInfo()
{
try
{
#region 文件上传
//文件路径
string path = string.Empty;
//文件名称
string filename=string.Empty;
//文件与路径
string filePath = string.Empty;
foreach (string upload in Request.Files)
{
//判断当前上传控件是否为空
if (Request.Files.Count > 0)
{
//判断当前上传控件是否为空
if (Request.Files[upload] != null && Request.Files[upload].ContentLength > 0)
//当前工程路径与上传文件夹
path = AppDomain.CurrentDomain.BaseDirectory + "uploadSite/";
//文件名称
filename = Path.GetFileName(Request.Files[upload].FileName);
//文件与路径
filePath = Path.Combine(path, filename);
//保存在当前工程文件夹
Request.Files[upload].SaveAs(filePath);
}
}
#endregion
#region 导入文件
//判断是否为空导入
if (string.IsNullOrEmpty(path))
{
return RedirectToAction("SiteInfoList", "SiteManage");
}
else
{
//判断当前文件是否为Excel
if (!filename.Contains("xls"))
return RedirectToAction("SiteInfoList", "SiteManage");
}
ImportExcel importEx = new ImportExcel();
//获取Excel中的数据
DataTable dtSite = importEx.ExcelDataSource(filePath, "Sheet1").Tables[0];
//添加到数据库
foreach (DataRow item in dtSite.Rows)
{
Site s = new Site()
{
Name = item["Name"].ToString().Trim(),
LocalID = int.Parse(item["LocalID"].ToString().Trim())
};
//判断当前站点是否存在
if (!string.IsNullOrEmpty(SiteManageBusiness.GetSiteNameByName(s.Name)))
continue;
SiteManageBusiness.AddSiteInfo(s);
}
#endregion
return RedirectToAction("SiteInfoList", "SiteManage", new { import = 1 });
}
catch (Exception ex)
{
return RedirectToAction("SystemError", "SysException", new { ExInfo = "堆栈消息:" + ex.StackTrace + "\n\n 异常消息:" + ex.Message });
}
}
}
Common
public class ImportExcel
{
/// <summary>
/// 获取Excel数据方法
/// </summary>
/// <param name="filepath">文件路径</param>
/// <param name="sheetname">sheet名称</param>
/// <returns></returns>
public DataSet ExcelDataSource(string filepath, string sheetname)
{
try
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "$]", strConn);
DataSet ds = new DataSet();
oada.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw ex;
}
}
}
相关文章推荐
- [导入]更新:让UpdatePanel支持上传文件
- [导入]最大可上传文件限制为120MB,总量为1GB的网络U盘
- 头像上传与使用POI导入导出文件列表
- 批量导入(单文件的文件上传 + 解析上传的csv文件 ,导入至数据库 ,反馈信息)
- [导入]在asp.net中利用FileUplad控件从同一个页面上传多个文件
- 使用七牛上传图片代码,导入文件常遇问题解决方法
- asp上传excle文件并导入到access数据库
- Excel文件上传并导入Sqlserver数据库
- [导入].net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等
- [导入]Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- 用Thinkphp上传导入csv文件并去除重复数据
- tp3.2中多个excel文件上传导入数据库示例
- SpringMVC简单实现上传Excel文件,并将Excel中的数据导入数据库
- mysql导入sql数据库提示。您可能正在上传很大的文件,请参考文档来寻找解决方法
- [导入]Django中动态改变ImageField和FileField中文件的上传路径
- FileUpload上传Excel文件,Aspose导入数据至数据库(web)
- [导入]IIS+Tomcat整合上传以及静态文件缓存问题!
- Excel文件一键上传并解析导入数据库
- 上传文件csv 导入功能
- 基于Spring3 MVC实现基于form表单上传Excel文件,批量导入数据!