public static void ExcelImport(TkDbContext context, string strFileName, Tk5ListMetaData metaData, ImportResultData result) { string sheetName = metaData.Table.TableDesc; ISheet sheet = null; string fileExt = Path.GetExtension(strFileName).ToLower(ObjectUtil.SysCulture); using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read)) { if (fileExt == ".xls") { HSSFWorkbook hssfworkbook = new HSSFWorkbook(file); sheet = hssfworkbook.GetSheet(sheetName); } else if (fileExt == ".xlsx") { XSSFWorkbook xssfworkbook = new XSSFWorkbook(file); sheet = xssfworkbook.GetSheet(sheetName); } else { throw new WebPostException("上传的文件不是Excel文件,请确认上传文件的格式"); } } if (sheet != null) { SheetImport(context, metaData, sheet, result); } }
public OutputData DoAction(IInputData input) { try { FileInfo info = input.PostObject.Convert <FileInfo>(); if (string.IsNullOrEmpty(info.FileName)) { throw new WebPostException(string.Empty, new FieldErrorInfo("Import", "FileName", "没有文件,请上传Excel文件")); } ImportResultData result = new ImportResultData(fMetaData); ExcelImporter.ExcelImport(Context, info.ServerPath, fMetaData, result); var errorCollection = fResolver.Import(result.ImportDataSet, input); result.ImportFieldInfoError(errorCollection); result.SaveTemp(); return(OutputData.CreateObject(result)); } catch (WebPostException ex) { return(OutputData.CreateToolkitObject(ex.CreateErrorResult())); } catch (Exception ex) { return(OutputData.CreateToolkitObject(new WebErrorResult(ex.Message))); } }
public OutputData DoAction(IInputData input) { ImportResultData result = ImportUtil.GetResultData(input); MetaDataTableResolver resolver = fCreator.CreateObject( new TempSource(result.ImportDataSet, this)).Convert <MetaDataTableResolver>(); resolver.Decode((PageStyleClass)PageStyle.List); return(OutputData.Create(result.ImportDataSet)); }
public OutputData DoAction(IInputData input) { ImportResultData result = ImportUtil.GetResultData(input); fBag.Count = result.ImportTable.Rows.Count; string previewUrl = string.Format(ObjectUtil.SysCulture, "~/c/import/CPreviewSuccess/{0}?Key={1}", input.SourceInfo.Source, input.QueryString[ExcelUtil.KEY_NAME]); fBag.PreviewUrl = WebUtil.ResolveUrl(previewUrl); return(OutputData.CreateObject(fBag)); }
public static ImportResultData GetResultData(IInputData input) { string key = input.QueryString[ExcelUtil.KEY_NAME]; //ImportResultData result = WebGlobalVariable.Session[ImportResultData.SESSION_KEY] // as ImportResultData; ImportResultData result = null; if (result == null || result.Key != key) { throw new ErrorPageException("非法操作", "系统检测出你尝试非法侵入上传系统,请按照正常步骤操作!"); } return(result); }
public override OutputData DoAction(IInputData input) { ImportResultData importResult = ImportUtil.GetResultData(input); MetaDataTableResolver resolver = fCreator.CreateObject(this). Convert <MetaDataTableResolver>(); using (resolver) { resolver.Import(importResult.ImportDataSet, input); resolver.UpdateDatabase(); } WebSuccessResult result = new WebSuccessResult("CloseDialog") { AlertMessage = "导入成功" }; return(OutputData.CreateToolkitObject(result)); }
public IContent WritePage(ISource source, IPageData pageData, OutputData outputData) { IPageMaker jsonPageMaker = new JsonObjectPageMaker(); if (outputData.Data is WebErrorResult) { return(jsonPageMaker.WritePage(source, pageData, outputData)); } ImportResultData resultData = outputData.Data.Convert <ImportResultData>(); //WebGlobalVariable.Session[ImportResultData.SESSION_KEY] = resultData; string url = string.Format(ObjectUtil.SysCulture, "c/import/C{1}/{0}?Key={2}", pageData.SourceInfo.Source, resultData.ErrorTable.Rows.Count > 0 ? ImportConst.ERROR_IMPORT : ImportConst.IMPORT, resultData.Key); WebSuccessResult result = new WebSuccessResult(url.AppVirutalPath()); return(jsonPageMaker.WritePage(source, pageData, OutputData.CreateToolkitObject(result))); }
private static void SheetImport(TkDbContext context, Tk5ListMetaData metaInfos, ISheet sheet, ImportResultData result) { Dictionary <string, Tk5FieldInfoEx> dicOfInfo = new Dictionary <string, Tk5FieldInfoEx>(); foreach (Tk5FieldInfoEx info in metaInfos.Table.TableList) { dicOfInfo.Add(info.DisplayName, info); } IRow headerRow = sheet.GetRow(0); int firstCell = headerRow.FirstCellNum; int colLength = headerRow.LastCellNum - firstCell; List <BaseColumnReader> readers = new List <BaseColumnReader>(colLength); string[] colNames = new string[colLength]; for (int i = 0; i < colLength; i++) { int cellIndex = i + firstCell; string name = headerRow.GetCell(cellIndex).ToString(); Tk5FieldInfoEx fieldInfo = dicOfInfo[name]; if (fieldInfo != null) { BaseColumnReader reader = null; if (fieldInfo.InternalControl != null && fieldInfo.InternalControl.SrcControl == ControlType.CheckBox) { reader = new BoolColumnReader(fieldInfo, cellIndex); } else if (fieldInfo.Decoder != null && fieldInfo.Decoder.Type == DecoderType.CodeTable) { reader = new CodeTableColumnReader(fieldInfo, context, cellIndex); } else if (fieldInfo.Decoder != null && fieldInfo.Decoder.Type == DecoderType.EasySearch) { reader = new EasySearchColumnReader(fieldInfo, cellIndex); } else { reader = new NormalColumnReader(fieldInfo, cellIndex); } readers.Add(reader); } } for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) { IRow row = sheet.GetRow(i); DataRow dataRow = result.ImportTable.NewRow(); try { dataRow.BeginEdit(); try { foreach (var reader in readers) { reader.ReadColumn(dataRow, row, context); } dataRow[ImportResultData.ROW_INDEX] = i; } finally { dataRow.EndEdit(); } result.ImportTable.Rows.Add(dataRow); } catch (ImportConvertException ex) { result.AddErrorItem(ex.Row, ex.NickName, ex.Message); DataRow errorRow = result.ErrorTable.NewRow(); errorRow.BeginEdit(); try { foreach (var reader in readers) { reader.ReadErrorColumn(errorRow, row); } errorRow[ImportResultData.ROW_INDEX] = i; } finally { errorRow.EndEdit(); } } } }
// 设置DataTable的值 private static void ReadColumn(DataRow dataRow, string columnName, Tk5FieldInfoEx fieldInfo, string strValue, int indexOfRow, ImportResultData result) { ImportWarningItem imResult = null; string asgValue = null; if (fieldInfo != null) { bool valueError = false; if (fieldInfo.Decoder != null && fieldInfo.Decoder.Type == DecoderType.CodeTable) { IEnumerable <IDecoderItem> data = ExcelUtil.GetDecoderItem(fieldInfo, null); if (string.IsNullOrEmpty(strValue)) { valueError = true; } else { foreach (IDecoderItem item in data) { if (item.Name == strValue) { asgValue = item.Value; valueError = true; break; } } } } else { if (fieldInfo.InternalControl != null && fieldInfo.InternalControl.SrcControl == ControlType.CheckBox) { if (strValue == "√") { asgValue = ((fieldInfo.Extension == null) ? "1" : fieldInfo.Extension.CheckValue); valueError = true; } if (strValue == "X" || string.IsNullOrEmpty(strValue)) { asgValue = ((fieldInfo.Extension == null) ? "0" : fieldInfo.Extension.UnCheckValue); valueError = true; } } else { asgValue = strValue; valueError = true; } } try { if (!valueError) { throw new Exception("value in the cell is invalid"); } else { dataRow[fieldInfo.NickName] = asgValue; } } catch (Exception ex) { imResult = new ImportWarningItem(indexOfRow, columnName, asgValue, ex.Message); } } //return imResult; }