/// <summary> /// thực hiện xuất file theo store /// </summary> /// <param name="store">Tên store lấy Data</param> /// <param name="SheetName">Tên Sheet trên file Excel</param> /// <param name="fileNameExport">Tên file xuất ra</param> /// <param name="type"> /// các dạng file có thể xuất /// [StringValue("XLS")] /// Excel_97_2003 = 1, /// Excel_2007 = 2, /// Comma_Separated_Values = 5, /// OpenOffice_Spreadsheet = 6, /// Hyper_Text_Markup_Language = 7, /// </param> /// <param name="FileMashName">Tên file Mash /// FileMash: là file map giưa các columnName và header hiển thị trên file Excel /// </param> /// <param name="paras"> /// Danh sách parramter chuyền vào cho store /// </param> public static void Flush(string store, string SheetName, string fileNameExport, FileType type, string FileMashName, params object[] paras) { string FileMashPath = ExcelHelper.GetFileMashFullPath(FileMashName); fileNameExport += "." + type.GetStringValue(); DataSet ds = new DataSet(); SqlHelper.ExecuteDataset(ds, store, paras); List <ExportMash> lstMash = ListMash(FileMashPath); List <DataColumn> clRemove = new List <DataColumn>(); bool CheckColumn; foreach (DataColumn item in ds.Tables[0].Columns) { CheckColumn = false; foreach (ExportMash Maitem in lstMash) { if (item.ColumnName == Maitem.ColumName) { item.ColumnName = Maitem.MashName.Trim(); CheckColumn = true; break; } } if (!CheckColumn) { clRemove.Add(item); } } foreach (DataColumn dr in clRemove) { ds.Tables[0].Columns.Remove(dr); } ExcelHelper.ToExcel(ds, fileNameExport, SheetName, type); }
/// <summary> /// Lấy parramter dành cho inport /// trả về 3 tham số /// 0: chuỗi kiểm tra đã tồn tại bảng tạm chưa nếu tồn tại rồi thì xóa đi /// 1: chuổi lấy dữ liệu từ file xml ra đưa vào bảng tạm /// 2: Đường dẫn file xml ( dùng để xóa file khi import thành công ) /// </summary> /// <param name="file"></param> /// <param name="fileNameMash"></param> /// <param name="TableTmp"></param> /// <param name="langID"></param> /// <returns></returns> public static List <string> GetParramterImport(HttpPostedFile file, string FileMashName, string TableTmp, Int16?langID, out string FilePath) { List <string> list = new List <string>(); list.Add("IF EXISTS ( SELECT name FROM sysobjects WHERE name = N'" + TableTmp + "') DROP TABLE " + TableTmp + " "); FilePath = string.Empty; string Resul = " Declare @xml VARCHAR(MAX) Declare @i as int select @xml=BulkColumn from openrowset(bulk '", TableName = string.Empty, strWhere = " Where "; string FileMash = ExcelHelper.GetFileMashFullPath(FileMashName); if (string.IsNullOrEmpty(FileMash)) { return(list); } List <ExportMash> lstmash = Export.ListMash(FileMash); FilePath = ExcelHelper.SaveFileExcelToXml(file, lstmash, out TableName); if (string.IsNullOrEmpty(FilePath)) { return(list); } Resul += FilePath + "', single_clob) as cse EXEC sp_xml_preparedocument @i OUTPUT,@xml " + "Select * INTO " + TableTmp + " From OpenXML(@i,'/NewDataSet/" + TableName + "',2) With("; if (lstmash.Count > 0) { foreach (ExportMash ExMash in lstmash) { if (ExMash.IsInsert) { Resul += ExMash.ColumName + " " + ExMash.DataType + ","; strWhere += " " + ExMash.ColumName + " IS NOT NULL or"; } } strWhere = strWhere.Trim(); strWhere = strWhere.Substring(0, strWhere.Length - 2); Resul = Resul.Trim(','); Resul += ")"; list.Add(Resul + strWhere); list.Add(FilePath); return(list); } return(list); }
/// <summary> /// trả về danh sách các record trong file excel /// </summary> /// <param name="file">có thể lấ từ thuộc tính của fileuploadfield (fufImport.PostedFile)</param> /// <param name="fileNameMash">truyền vào tên của file xml để lấy template</param> /// <returns></returns> public static List <object> ListObjImport(HttpPostedFile file, string FileMashName) { List <object> listReturn = new List <object>(); DataSet ds = Import.GetData(file); string FileMash = ExcelHelper.GetFileMashFullPath(FileMashName); List <ExportMash> lstmash = Export.ListMash(FileMash); foreach (DataRow row in ds.Tables[0].Rows) { List <object> Param = new List <object>(); foreach (ExportMash ExMash in lstmash) { if (ExMash.IsInsert) { Param.Add(Import.ConvertType(row[ExMash.MashName].ToString(), ExMash.DataType)); } } listReturn.Add(Param); } return(listReturn); }
public static void Flush(DataSet ds, string SheetName, string fileNameExport, string Filetype, string FileMashName) { FileType type = GetFileType(Filetype.ToUpper()); string FileMashPath = ExcelHelper.GetFileMashFullPath(FileMashName); fileNameExport += "." + type.GetStringValue(); List <ExportMash> lstMash = ListMash(FileMashPath); DataTable tmp = ds.Tables[0].Copy(); ds.Tables[0].Constraints.Clear(); if (lstMash.Count > 0) { foreach (DataColumn item in tmp.Columns) { IEnumerable <ExportMash> results = ( from obj in lstMash where obj.ColumName == item.ColumnName select obj); if (results.Count <ExportMash>() <= 0) { ds.Tables[0].Columns[item.ColumnName].AllowDBNull = true; ds.Tables[0].Columns.Remove(item.ColumnName); } } foreach (DataColumn item in ds.Tables[0].Columns) { IEnumerable <ExportMash> results = ( from obj in lstMash where obj.ColumName == item.ColumnName select obj); if (results.Count <ExportMash>() > 0) { item.ColumnName = results.Single().MashName.Trim(); } } } ExcelHelper.ToExcel(ds, fileNameExport, SheetName, type); }