# Region Export File
Private static void ImportPage (HttpContext Context, string app, string entity, string id)
{
# Region
AppEntity. App AppObj = AppEntity. GetApp (app );
AppEntity. Entity En = AppObj. GetEntity (entity );
# Endregion
String columns = Context. Request. Form ["columns"];
String striphtml = Context. Request. Form ["striphtml"];
XmlDocument ColDom = new XmlDocument ();
Hashtable param = new Hashtable ();
Param. Add ("paietting", AppObj. AppDom );
Param. Add ("entitysetting", En. EntityDom );
Param. Add ("urn: functions-sbsinc-com: ext", new SBS. Common. XslExtensionObject ());
Param. Add ("urn: functions-sbsinc-com: AppEntity", new SBS. AppEntity. AppEntityXslExtension ());
Param. Add ("app", app );
Param. Add ("entity", entity );
Param. Add ("action", "export ");
Param. Add ("columns", ColDom );
String Folder = AppEntity. GetAppFolder () + app + "http://www.cnblogs.com/jacd/admin/file://entity//" + entity + "http://www.cnblogs.com/jacd/admin/file://data ";
String html = SBS. Common. Convert. xml2html ("<root/>", AppEntity. GetThemePath () + "common \ import. xsl", param );
Context. Response. Write (html );
}
Private static void DownTemplate (HttpContext Context, string app, string entity, string id)
{
# Region
AppEntity. App AppObj = AppEntity. GetApp (app );
AppEntity. Entity En = AppObj. GetEntity (entity );
Var columns = En. columns. ToHashSet ();
Var sysColumnDict = AppEntity. GetAppBuilderSetting (). Descendants ("syscol"). Elements ("col"). Select (
N => n. GetAttrValue ("name"). ToHashSet ();
# Endregion
SysColumnDict. Remove ("title ");
SysColumnDict. Remove ("description ");
Columns. inclutwith (sysColumnDict );
String filename = AppObj. name + "-" + En. title + ". csv ";
StreamWriter sw = new StreamWriter (Context. Response. OutputStream, Encoding. UTF8 );
StringBuilder sb = new StringBuilder ();
Foreach (var c in columns)
{
Sb. Append (c + ",");
}
Sw. Write (sb. ToString (). Substring (0, sb. Length-1 ));
Sw. Write ("\ r \ n ");
If (filename! = "")
{
Context. Response. ContentEncoding = Encoding. UTF8;
Context. Response. AddHeader ("Content-Disposition", "attachment; filename =" + filename );
// Context. Response. ContentType = "application/octet-stream ";
// Context. Response. ContentType = "application/csv ";
Sw. Close ();
Context. Response. End ();
}
}
# Region download Xls files
Private static void DownTemplateXls (HttpContext Context, string app, string entity, string id)
{
# Region
AppEntity. App AppObj = AppEntity. GetApp (app );
AppEntity. Entity En = AppObj. GetEntity (entity );
String tableName = "ColumnExample ";
Var columns = En. columns. ToHashSet ();
Var sysColumnDict = AppEntity. GetAppBuilderSetting (). Descendants ("syscol"). Elements ("col"). Select (
N => n. GetAttrValue ("name"). ToHashSet ();
SysColumnDict. Remove ("title ");
SysColumnDict. Remove ("description ");
Columns. inclutwith (sysColumnDict );
StringBuilder sb = new StringBuilder ();
Sb. Append ("create table" + tableName + "(");
Foreach (var c in columns)
{
Sb. Append (c + "VARCHAR (99 ),");
}
String CreateSql = sb. ToString ();
CreateSql = CreateSql. Trim (',');
CreateSql = CreateSql + ")";
# Endregion
String newFile = (Context. Server. MapPath (".") + "\" + "TableColumn.xls ");
File. Delete (newFile );
File. Copy (@ "D: \ tasktoday.353.us \ fb.tasktoday.com \ TableColumn.xls", newFile, true );
String cnStr = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" +
NewFile + "; Extended Properties = Excel 8.0 ";
Using (var cn = new OleDbConnection (cnStr ))
{
Cn. Open ();
Var cmd = new OleDbCommand (CreateSql, cn );
Cmd. ExecuteNonQuery ();
Cmd. Dispose ();
Cn. Close ();
}
MemoryStream MS = new MemoryStream (File. ReadAllBytes (newFile ));
Ms. WriteTo (Context. Response. OutputStream );
Ms. Close ();
Context. Response. ContentEncoding = Encoding. UTF8;
String filename = AppObj. name + "-" + En. title + ". xls ";
Context. Response. AddHeader ("Content-Disposition", "attachment; filename =" + filename );
Context. Response. ContentType = "application/ms-excel ";
Context. Response. End ();
}
# Endregion
# Endregion
/// <Summary>
/// Import a CSV file to TABLE
/// </Summary>
/// <Param name = "Context"> </param>
/// <Param name = "app"> </param>
/// <Param name = "entity"> </param>
Private static void FileImport (HttpContext Context, string app, string entity)
{
AppEntity. Entity En = AppEntity. GetApp (app). GetEntity (entity );
String Doublequotes = Context. Request. Form ["Doublequotes"];
Char SplitStr = Context. Request. Form ["SplitStr"] [0];
HttpPostedFile httpFile = Context. Request. Files ["CSVfile"];
String url = "/common /? App = "+ app +" & entity = "+ entity +" & action = import ";
If (httpFile. ContentLength <1)
{
Context. Response. Write ("file cannot be blank ");
Return;
}
If (httpFile. FileName. ToUpper (). IndexOf ("csv". ToUpper ()> 0)
{
WriteCSV (Context, En, Doublequotes, httpFile, url );
}
If (httpFile. FileName. ToUpper (). IndexOf ("xls". ToUpper ()> 0)
{
WriteXls (Context, En, Doublequotes, httpFile, url );
}
}
Private static void WriteCSV (HttpContext Context, AppEntity. Entity En, string Doublequotes, HttpPostedFile httpFile, string url)
{
String dir = Path. GetTempPath ();
String fileName = Path. Combine (dir, "importdata.csv ");
File. Delete (fileName );
HttpFile. SaveAs (fileName );
String tableName = "[importdata.csv]";
String cnStr =
"Provider = Microsoft. Jet. OLEDB.4.0;" +
"Extended Properties = 'text; HDR = Yes; FMT = delimited';" +
"Data Source =" + dir + ";";
WriteToTable (cnStr, Context, En, Doublequotes, url, dir, tableName );
}
Private static void WriteXls (HttpContext Context, AppEntity. Entity En, string Doublequotes, HttpPostedFile httpFile, string url)
{
String dir = Context. Server. MapPath (".");
String fileName = Path. Combine (dir, "TableColumnEntity.xls ");
File. Delete (fileName );
HttpFile. SaveAs (fileName );
String tableName = "[ColumnExample $]";
String cnStr = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" +
FileName + "; Extended Properties = Excel 8.0 ";
WriteToTable (cnStr, Context, En, Doublequotes, url, fileName, tableName );
}
Private static void WriteToTable (string cnStr, HttpContext Context, AppEntity. Entity En, string Doublequotes, string url, string DataSouce, string tableName)
{
DataTable dTab = new DataTable ();
Int ValueCount = 0;
Using (var cn = new OleDbConnection (cnStr ))
{
Cn. Open ();
String SQL = "SELECT * FROM" + tableName;
OleDbDataAdapter da = new OleDbDataAdapter (SQL, cn );
Da. Fill (dTab );
For (int I = 0; I <dTab. Rows. Count; I ++)
{
AppEntity. Record row = En. Create ();
Foreach (DataColumn column in dTab. Columns)
{
String value = dTab. Rows [I] [column]. ToString ();
String ColumnName = column. ColumnName;
If (Doublequotes = "N ")
{
Value = value. Trim ('"');
}
If (En. GetColumn (ColumnName). type = "double" |
En. GetColumn (ColumnName). type = "integer ")
{
Value = (value. ToDouble (0). ToString ();
}
If (En. GetColumn (ColumnName). type = "datetime" |
En. GetColumn (ColumnName). type = "date ")
{
Value = SBS. Common. Utility. GetNowTime (value. ToDateTime (DateTime. Now ));
}
Row. SetColumnValue (ColumnName, value );
}
Try
{
Row. Add ();
ValueCount ++;
}
Catch
{
}
}
Context. Response. Redirect (url + "& ValueCount =" + ValueCount );
}
}