public FileResult downloadTemplate(long id) { if (id != 0) { DataStructureManager dataStructureManager = new DataStructureManager(); StructuredDataStructure dataStructure = new StructuredDataStructure(); dataStructure = dataStructureManager.StructuredDataStructureRepo.Get(id); if (dataStructure != null) { ExcelTemplateProvider provider = new ExcelTemplateProvider("BExISppTemplate_Clean.xlsm"); provider.CreateTemplate(dataStructure); string path = ""; XmlNode resources = dataStructure.TemplatePaths.FirstChild; XmlNodeList resource = resources.ChildNodes; foreach (XmlNode x in resource) { if (x.Attributes.GetNamedItem("Type").Value == "Excel") path = x.Attributes.GetNamedItem("Path").Value; } string rgxPattern = "[<>?\":|\\\\/*]"; string rgxReplace = "-"; Regex rgx = new Regex(rgxPattern); string filename = rgx.Replace(dataStructure.Name, rgxReplace); if (filename.Length > 50) filename = filename.Substring(0, 50); return File(Path.Combine(AppConfiguration.DataPath, path), "application/xlsm", "Template_" + dataStructure.Id + "_" + filename + ".xlsm"); } } return File(Path.Combine(AppConfiguration.GetModuleWorkspacePath("RPM"), "Template", "BExISppTemplate_Clean.xlsm"), "application/xlsm", "Template_" + id + "_No_Data_Structure.xlsm"); }
/// <summary> /// /// </summary> /// <remarks></remarks> /// <seealso cref=""/> /// <param name="datasetId"></param> /// <param name="datasetVersionOrderNr"></param> /// <param name="dataStructureId"></param> /// <param name="title"></param> /// <param name="extention"></param> /// <returns></returns> public string CreateFile(long datasetId, long datasetVersionOrderNr, long dataStructureId, string title, string extention) { string dataPath = GetFullStorePath(datasetId, datasetVersionOrderNr, title, extention); //Template will not be filtered by columns if (this.VisibleColumns == null) { #region generate file with full datastructure string dataStructureFilePath = GetDataStructureTemplatePath(dataStructureId, extention); //dataPath = GetStorePath(datasetId, datasetVersionOrderNr, title, extention); try { SpreadsheetDocument dataStructureFile = SpreadsheetDocument.Open(dataStructureFilePath, true); SpreadsheetDocument dataFile = SpreadsheetDocument.Create(dataPath, dataStructureFile.DocumentType); foreach (OpenXmlPart part in dataStructureFile.GetPartsOfType<OpenXmlPart>()) { OpenXmlPart newPart = dataFile.AddPart<OpenXmlPart>(part); } dataFile.WorkbookPart.Workbook.Save(); dataStructureFile.Dispose(); dataFile.Dispose(); } catch (Exception ex) { throw new Exception(ex.Message.ToString()); } #endregion } // create a file with a subset of variables if (this.VisibleColumns != null) { /// call templateprovider from rpm ExcelTemplateProvider provider = new ExcelTemplateProvider(); string path = GetStorePath(datasetId, datasetVersionOrderNr); string newTitle = GetNewTitle(datasetId, datasetVersionOrderNr, title, extention); provider.CreateTemplate(getVariableIds(this.VisibleColumns), dataStructureId, path, newTitle); } return dataPath; }
/// <summary> /// /// </summary> /// <remarks></remarks> /// <seealso cref=""/> /// <param name="dataStructureId"></param> /// <param name="extention"></param> /// <returns></returns> public string GetDataStructureTemplatePath(long dataStructureId, string extention) { DataStructureManager dataStructureManager = new DataStructureManager(); StructuredDataStructure dataStructure = dataStructureManager.StructuredDataStructureRepo.Get(dataStructureId); string dataStructureTitle = dataStructure.Name; // load datastructure from db an get the filepath from this object ExcelTemplateProvider provider = new ExcelTemplateProvider("BExISppTemplate_Clean.xlsm"); string path = ""; if (dataStructure.TemplatePaths != null) { XmlNode resources = dataStructure.TemplatePaths.FirstChild; XmlNodeList resource = resources.ChildNodes; foreach (XmlNode x in resource) { if (x.Attributes.GetNamedItem("Type").Value == "Excel") if (File.Exists(x.Attributes.GetNamedItem("Path").Value)) { path = x.Attributes.GetNamedItem("Path").Value; } else { path = provider.CreateTemplate(dataStructure); } } //string dataPath = AppConfiguration.DataPath; //Path.Combine(AppConfiguration.WorkspaceRootPath, "Data"); return Path.Combine(AppConfiguration.DataPath, path); } path = provider.CreateTemplate(dataStructure); return Path.Combine(AppConfiguration.DataPath, path); }
public DataStructure CreateDataStructure(string dataSetID, DataTable mapVariables, List<string> variableNames) { DataStructureManager dataStructureManager = new DataStructureManager(); DataContainerManager attributeManager = new DataContainerManager(); StructuredDataStructure dataStructure = new StructuredDataStructure(); UnitManager unitManager = new UnitManager(); // values of DataStructure ExcelTemplateProvider provider = new ExcelTemplateProvider(); string name = "oldBExIS" + dataSetID; string description = "old BExIS datastructure, created for " + dataSetID + ", possibly used by other"; //metadata.Title; string xsdFileName = "";//"xsdFileName"; string xslFileName = "";//"xslFileName"; DataStructureCategory indexerType = DataStructureCategory.Generic; // if dataStructure not exists StructuredDataStructure existDS = existingDataStructures(mapVariables, dataSetID); if (existDS.Name == null) { // create dataStructure dataStructure = dataStructureManager.CreateStructuredDataStructure(name, description, xsdFileName, xslFileName, indexerType, null); foreach (string varName in variableNames) { // values of variables string attName = ""; string convFactor = ""; string varDescription = ""; int Block = -999; long AttributeId = -999, UnitId = -999, VarUnitId = -999; foreach (DataRow mapRow in mapVariables.Select("DatasetId = '" + dataSetID + "'")) { if (mapRow["Name"].ToString() == varName) { attName = mapRow["Attribute"].ToString(); convFactor = mapRow["ConvFactor"].ToString(); varDescription = mapRow["Description"].ToString(); Block = int.Parse(mapRow["Block"].ToString()); if (attName.Length > 1) // if not mapped yet { AttributeId = Convert.ToInt64(mapRow["AttributeId"].ToString()); UnitId = Convert.ToInt64(mapRow["UnitId"].ToString()); if (mapRow["Unit"].ToString().Length > 0) VarUnitId = Convert.ToInt64(mapRow["VarUnitId"].ToString()); } } } if (AttributeId > 0 && Block == 0) // if not mapped yet AND variable is in block 0 { // find existing attribute for each variable DataAttribute attribute = attributeManager.DataAttributeRepo.Get(AttributeId); Unit varUnit = null; if (VarUnitId > 0) { varUnit = unitManager.Repo.Get(VarUnitId); } // add variables to dataStructure Variable variable = dataStructureManager.AddVariableUsage(dataStructure, attribute, true, varName, null, null, varDescription, varUnit); dataStructure.Variables.Add(variable); } } provider.CreateTemplate(dataStructure); return dataStructure; } else { return existDS; } }
public static void deleteTemplate(long dataStructureId) { ExcelTemplateProvider provider = new ExcelTemplateProvider(); provider.deleteTemplate(dataStructureId); }