public short Open(String fileName) { OpenFromTemplate = false; try { if (!string.IsNullOrWhiteSpace(template)) { GxFile temp = new GxFile(Path.GetDirectoryName(template), template); if (temp.Exists()) { GXLogging.Debug(log, "Opening Template " + template); p = new ExcelPackage(temp.GetStream()); OpenFromTemplate = true; } else { errCod = 4; errDescription = "Invalid template."; return(errCod); } } else { GxFile file = new GxFile(fileName, fileName, GxFileType.Private); if (string.IsNullOrEmpty(Path.GetExtension(fileName)) && !file.Exists()) { fileName += Constants.EXCEL2007Extension; } if (file.IsExternalFile) { p = new ExcelPackage(file.GetStream()); } else { p = new ExcelPackage(new FileInfo(fileName)); } } workBook = (ExcelWorkbook)p.Workbook; workBook.CalcMode = ExcelCalcMode.Automatic; this.selectFirstSheet(); xlsFileName = fileName.ToString(); } catch (Exception e) { GXLogging.Error(log, "Error opening " + fileName, e); errCod = 10; errDescription = "Could not open file." + e.Message + (e.InnerException != null ? e.InnerException.Message : ""); return(errCod); } return(0); }
public short Open(String fileName) { try { GXLogging.Debug(log, "GetType " + nmspace + ".ExcelFile"); Type classType = ass.GetType(nmspace + ".ExcelFile", false, true); ef = Activator.CreateInstance(classType); GxFile file = new GxFile(Path.GetDirectoryName(fileName), fileName, GxFileType.Private); if (!String.IsNullOrEmpty(template)) { GxFile templateFile = new GxFile(Path.GetDirectoryName(template), template); if (templateFile.Exists()) { GXLogging.Debug(log, "Opening Template " + template); var stream = templateFile.GetStream(); stream.Position = 0; GxExcelUtils.Invoke(ef, "LoadXls", new object[] { stream }); } else { errCod = 4; errDescription = "Invalid template."; return(errCod); } } else if (file.Exists()) { var stream = file.GetStream(); stream.Position = 0; GxExcelUtils.Invoke(ef, "LoadXls", new object[] { stream }); } else { object worksheets = GxExcelUtils.GetPropValue(ef, "Worksheets"); object ws = GxExcelUtils.Invoke(worksheets, "Add", new object[] { "Sheet1" }); GxExcelUtils.SetPropValue(worksheets, "ActiveWorksheet", ws); } xlsFileName = fileName; } catch (Exception e) { GXLogging.Error(log, "Error opening " + fileName, e); errCod = 10; errDescription = "Could not open file." + e.Message + (e.InnerException != null ? e.InnerException.Message:""); return(errCod); } return(0); }