View
@ Model bsserver. Models. pagerindexmodel
@{
Viewbag. Title = "Site Information overview ";
}
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> site information overview </title>
<Link href = "@ URL. Content ("~ /Content/style/tablecss.css ")" rel = "stylesheet" type = "text/CSS"/>
</Head>
<Body>
<Fieldset style = "border-color: # b1cde3">
<P>
@ Html. actionlink ("add site", "addsiteinfo", "sitemanage ")
@ Html. actionlink ("Export site information", "exportsiteinfo", "sitemanage", new {style = "color: # 46a3ff ;"})
</P>
<P>
@ If (viewbag. Flag = 1)
{
<SCRIPT type = "text/JavaScript"> alert ('import successful! ') </SCRIPT>
}
@ Using (html. beginform ("importsiteinfo", "sitemanage", formmethod. Post, new {enctype = "multipart/form-Data "}))
{
@ HTML. label ("Import site") <input id = "filesite" name = "filesite" type = "file" Title = "Import site information" style = "color: # 46a3ff; "/>
<Input type = "Submit" value = "import" style = "background-color: # b1cde3"/>
}
</P>
<P>
@ Using (html. beginform ("siteinfolist", "sitemanage "))
{
<Label id = "localname">
Region name </label> <input id = "txtlocalname" name = "txtlocalname" value = "@ viewbag. localname" type = "text" maxlength = '20'/>
<Label id = "locname">
Site name </label> <input id = "txtsitename" name = "txtsitename" value = "@ viewbag. sitename" type = "text" maxlength = '20'/>
<Input type = "Submit" value = "query" style = "background-color: # b1cde3"/>
}
</P>
& Lt; table width = "100%" & gt;
<Tr>
<TD style = "background-color: # b1cde3">
Site ID
</TD>
<TD style = "background-color: # b1cde3">
Region
</TD>
<TD style = "background-color: # b1cde3">
Site name
</TD>
<TD style = "background-color: # b1cde3">
Operation
</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 ("View", "siteinfodetails", new {id = site. ID}) |
@ Html. actionlink ("edit", "editesiteinfo", new {id = site. ID}) |
@ Html. actionlink ("delete", "deletesite", new {siteid = site. ID}, new {onclick = "Return confirm ('Do you want to delete? ');"})
</TD>
</Tr>
}
<Tr>
<TD colspan = '4' align = 'right'>
@ Html. Partial ("_ page", Model)
</TD>
</Tr>
</Table>
</Fieldset>
</Body>
</Html>
Controller
/// <Summary>
/// Site Controller
/// </Summary>
Public class sitemanagecontroller: Controller
{
/// <Summary>
/// Excel import Method
/// </Summary>
/// <Param name = "filesite"> </param>
/// <Returns> </returns>
Public actionresult importsiteinfo ()
{
Try
{
# Region File Upload
// File path
String Path = string. empty;
// File name
String filename = string. empty;
// File and Path
String filepath = string. empty;
Foreach (string upload in request. files)
{
// Determine whether the current upload control is empty
If (request. Files. Count> 0)
{
// Determine whether the current upload control is empty
If (request. Files [upload]! = NULL & request. Files [upload]. contentlength> 0)
// Current project path and upload folder
Path = appdomain. currentdomain. basedirectory + "uploadsite /";
// File name
Filename = path. getfilename (request. Files [upload]. filename );
// File and Path
Filepath = path. Combine (path, filename );
// Save it to the current project folder
Request. Files [upload]. saveas (filepath );
}
}
# Endregion
# Region import File
// Judge whether the import is empty
If (string. isnullorempty (PATH ))
{
Return redirecttoaction ("siteinfolist", "sitemanage ");
}
Else
{
// Determine whether the current file is Excel
If (! Filename. Contains ("xls "))
Return redirecttoaction ("siteinfolist", "sitemanage ");
}
Importexcel importex = new importexcel ();
// Obtain data in Excel
Datatable dtsite = importex. exceldatasource (filepath, "sheet1"). Tables [0];
// Add to database
Foreach (datarow item in dtsite. Rows)
{
Site S = new site ()
{
Name = item ["name"]. tostring (). Trim (),
Localid = int. parse (item ["localid"]. tostring (). Trim ())
};
// Determine whether the current site exists
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 = "Stack message:" + ex. stacktrace + "\ n exception message:" + ex. message });
}
}
}
Common
Public class importexcel
{
/// <Summary>
/// How to obtain Excel Data
/// </Summary>
/// <Param name = "filepath"> file path </param>
/// <Param name = "sheetname"> Sheet Name </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;
}
}
}