using DevExpress.Spreadsheet; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Geodatabase; using KGIS.Framework.AE; using KGIS.Framework.Maps; using KGIS.Framework.Platform; using KGIS.Framework.Utils; using KGIS.Framework.Utils.Helper; using KGIS.Framework.Views; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Controls; using Worksheet = DevExpress.Spreadsheet.Worksheet; using KUI.Windows; using System.Linq; namespace Kingo.Plugin.DLTB_IDG.View { /// /// 核查记录挂接.xaml 的交互逻辑 /// public partial class HCRecordConnectionView : UserControl, IDockPanel2 { /// /// 提取图斑视图数据 /// private DataTable IntoData { get; set; } private IFeatureLayer DLTBBG_Layer_Temp = null; private string BGBSMs = string.Empty; private string excelPath = string.Empty; private IFeatureLayer featureBGLayer = null; Dictionary> fieldValueDict = null; Dictionary keyValuePairs = null; public HCRecordConnectionView() { InitializeComponent(); Title = "核查记录挂接"; DevExpress.Xpf.Core.ThemeManager.SetTheme(this, DevExpress.Xpf.Core.Theme.Office2013LightGray); DockAreas = DockStyle.DockRight | DockStyle.DockBottom | DockStyle.DockLeft; FloatSize = new System.Drawing.Size(1030, 660); DockWidth = 660; DockHeight = 380; DefaultArea = DockStyle.DockBottom; ShowCloseButton = true; ShowAutoHideButton = false; IsShowInMap = true; } #region 继承属性 public bool IsShowInMap { get; set; } public Guid ID { get; set; } public DockStyle DockAreas { get; set; } public System.Drawing.Size FloatSize { get; set; } public int DockWidth { get; set; } public int DockHeight { get; set; } public DockStyle DefaultArea { get; set; } public bool ShowCloseButton { get; set; } public bool ShowAutoHideButton { get; set; } public string Title { get; set; } public event EventHandler CloseViewHandler; #endregion /// /// 数据提取 /// /// /// private void BtnDataExtract_Click(object sender, RoutedEventArgs e) { try { excelPath = string.Empty; //@"E:\软件产品测试总目录\普陀区\浙江扣除系数问题\(330903)浙江省舟山市普陀区问题图斑核实记录表.xlsx"; KGIS.Framework.Utils.Dialog.OpenFileDialog openFileDialog = new KGIS.Framework.Utils.Dialog.OpenFileDialog(); openFileDialog.Title = "导入扣除系数表EXCEL"; openFileDialog.Filter = "Excel Files(*.xlsx)| *.xlsx"; openFileDialog.FileName = string.Empty; openFileDialog.FilterIndex = 1; if (openFileDialog.ShowDialog()) { excelPath = openFileDialog.FileName; } if (featureBGLayer == null) featureBGLayer = MapsManager.Instance.MapService.GetFeatureLayerByName("DLTBBG"); if (featureBGLayer == null || string.IsNullOrWhiteSpace(excelPath)) return; this.ShowLoading("正在进行核查记录挂接数据提取.........", 0, 0); // 打开 Excel 文件 FileStream stream = new FileStream(excelPath, FileMode.Open); Workbook workbook = new Workbook(); workbook.LoadDocument(stream, DocumentFormat.OpenXml); Worksheet sheet = workbook.Worksheets[0]; // 获取 Excel 表格中的数据范围 Range usedRange = sheet.GetUsedRange(); int rowCount = usedRange.RowCount; int columnCount = usedRange.ColumnCount; // 获取 Excel 表格中的字段名和值 List fieldNames = new List(); fieldValueDict = new Dictionary>(); int indexValue = 1; for (int i = 0; i < columnCount; i++) { string fieldName = sheet.Rows[indexValue][i].DisplayText.ToString(); while (i == 0 && fieldName != "省代码") { indexValue++; fieldName = sheet.Rows[indexValue][i].DisplayText.ToString(); } fieldNames.Add(fieldName); } for (int j = 2; j <= rowCount; j++) { string BSMValue = sheet.Rows[j][4].DisplayText.ToString(); if (string.IsNullOrWhiteSpace(BSMValue)) continue; List fieldValues = new List(); for (int i = 0; i < columnCount; i++) { object value = sheet.Rows[j][i].Value; if (value != null) { fieldValues.Add(value); } } fieldValueDict[BSMValue] = fieldValues; } DLTBBG_Layer_Temp = GeoDBAPI.CreateFeatureLayerInmemeory("DLTBBG", "地类图斑变更", (featureBGLayer.FeatureClass as IGeoDataset).SpatialReference, featureBGLayer.FeatureClass.ShapeType, featureBGLayer.FeatureClass.Fields); IFeatureClassAPI featureClassAPI = new FeatureClassAPI(featureBGLayer.FeatureClass); featureClassAPI.FcToFc(DLTBBG_Layer_Temp.FeatureClass, null, false); //创建要素类字段 foreach (string fieldName in fieldNames) { IField field = new FieldClass(); IFieldEdit fieldEdit = (IFieldEdit)field; fieldEdit.Name_2 = fieldName + "_HC"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.Length_2 = 255; DLTBBG_Layer_Temp.FeatureClass.AddField(field); } IFeatureCursor cursorBG = DLTBBG_Layer_Temp.FeatureClass.Update(null, true); IFeature featurebg = null; IFields fieldsBg = DLTBBG_Layer_Temp.FeatureClass.Fields; while ((featurebg = cursorBG.NextFeature()) != null) { string BSMBGValue = featurebg.Value[fieldsBg.FindField("BSM")].ToString(); if (!fieldValueDict.ContainsKey(BSMBGValue)) continue; for (int i = 0; i < fieldsBg.FieldCount; i++) { IField field = fieldsBg.get_Field(i); if (field.Name.Contains("_HC")) { foreach (var item in fieldValueDict[BSMBGValue]) { featurebg.Value[i] = item; i++; if (!string.IsNullOrWhiteSpace(item.ToString()) && item.ToString().Contains("扣除系数:不通过")) BGBSMs += string.Format("'{0}',", BSMBGValue); } break; } } featurebg.Store(); } cursorBG.Flush(); IQueryFilter queryFilter = new QueryFilterClass() { WhereClause = "标识码_HC Is NULL OR 标识码_HC=''" }; (DLTBBG_Layer_Temp as ITable).DeleteSearchedRows(queryFilter); LoadData(DLTBBG_Layer_Temp); this.CloseLoading(); MessageHelper.ShowTips("核查记录挂接数据提取完成!"); if (workbook != null) workbook.Dispose(); } catch (Exception ex) { LogAPI.Debug(ex.Message); LogAPI.Debug(ex); } finally { this.CloseLoading(); } } /// /// 数据挂接 /// /// /// private void BtnDataHitch_Click(object sender, RoutedEventArgs e) { try { if (DLTBBG_Layer_Temp == null || DLTBBG_Layer_Temp.FeatureClass.FeatureCount(null) == 0) return; this.ShowLoading("正在进行核查记录数据挂接.........", 0, 0); IFeatureLayer featureJC = MapsManager.Instance.MapService.GetFeatureLayerByLayerName("地类图斑"); int JCKCDLXSIndex = featureJC.FeatureClass.FindField("KCXS"); int JCGDPDJBIndex = featureJC.FeatureClass.FindField("GDPDJB"); IQueryFilter queryFilter = new QueryFilterClass() { WhereClause = string.Format("BSM In({0})", BGBSMs.Trim(',')) }; IFeatureCursor featureBGCur = featureBGLayer.FeatureClass.Update(queryFilter, true); IFeature feature = null; int BGKCXSIndex = featureBGLayer.FeatureClass.FindField("KCXS"); int BGGDPDJBIndex = featureBGLayer.FeatureClass.FindField("GDPDJB"); int BGBSMIndex = featureBGLayer.FeatureClass.FindField("BSM"); keyValuePairs = new Dictionary(); while ((feature = featureBGCur.NextFeature()) != null) { //keyValuePairs[BSMValue] = "非关联数据"; List features = FeatureAPI.Identify2(feature.ShapeCopy, featureJC); if (features.Count == 1 && (feature.Value[BGKCXSIndex] != features[0].Value[JCKCDLXSIndex] || feature.Value[BGGDPDJBIndex] != features[0].Value[JCGDPDJBIndex])) { string BSMValue = feature.Value[BGBSMIndex].ToString(); keyValuePairs[BSMValue] = "关联数据"; feature.Value[BGKCXSIndex] = features[0].Value[JCKCDLXSIndex]; feature.Value[BGGDPDJBIndex] = features[0].Value[JCGDPDJBIndex]; feature.Store(); } } featureBGCur.Flush(); this.CloseLoading(); MessageHelper.ShowTips("挂接完成!"); } catch (Exception ex) { MessageHelper.ShowError(ex.Message); LogAPI.Debug(ex); } finally { this.CloseLoading(); } } /// /// 结果导出 /// /// /// private void BtnDataExport_Click(object sender, RoutedEventArgs e) { try { var sourcesfile = SysAppPath.GetCurrentAppPath() + string.Format(@"TempalateReports\核查挂接结果模板\{0}", "核查挂接结果.xls"); Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(sourcesfile); workbook.CalculateFormula(true); Aspose.Cells.Worksheet sheet = workbook.Worksheets[0]; string xlsFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff"); string OutPath = string.Empty; KGIS.Framework.Utils.Dialog.FolderBrowserDialog pBrowser = new KGIS.Framework.Utils.Dialog.FolderBrowserDialog { ShowNewFolderButton = true }; if (pBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK) { OutPath = pBrowser.SelectedPath; } else return; string TempPath = string.Format(@"{0}\{1}{2}.xls", OutPath, "核查挂接结果", xlsFileName); int IndexValue = 1;//行 int XHValue = 1; if (fieldValueDict == null) return; foreach (var item in fieldValueDict) { for (int j = 0; j < 3; j++) { Aspose.Cells.Cell cell = sheet.Cells[IndexValue, j]; if (j == 0) { cell.PutValue(XHValue); XHValue++; continue; } else if (j == 1) { cell.PutValue(item.Key); continue; } else { if (keyValuePairs.ContainsKey(item.Key)) cell.PutValue(keyValuePairs[item.Key]); else cell.PutValue("非关联数据"); } } IndexValue++; } workbook.Save(TempPath); MessageHelper.ShowTips("挂接结果导出成功!"); } catch (Exception ex) { MessageHelper.ShowTips(ex.Message); LogAPI.Debug(ex); } } /// /// 加载视图数据 /// private void LoadData(IFeatureLayer FcAPI_DLTBHRGC) { IFeatureCursor cursor = null; IFeature BgFeature = null; try { if (FcAPI_DLTBHRGC.FeatureClass == null || FcAPI_DLTBHRGC.FeatureClass.FeatureCount(null) <= 0) return; IntoData = new DataTable();//从地类图斑图层取数据 ConstructColumn(FcAPI_DLTBHRGC.FeatureClass.Fields); IQueryFilter queryfilter = new QueryFilterClass { WhereClause = string.Format("") }; cursor = FcAPI_DLTBHRGC.FeatureClass.Search(queryfilter, true); while ((BgFeature = cursor.NextFeature()) != null) { DataRow dr = IntoData.NewRow(); dr[0] = false; for (int i = 0; i < IntoData.Columns.Count; i++) { if (i == 0) { dr[i] = false; continue; } object obj = BgFeature.get_Value((int)IntoData.Columns[i].ExtendedProperties["index"]); if (obj == null) { continue; } else { if (obj.ToString().Contains("1899/12/30 0:00:00")) { obj = DBNull.Value; } } if (obj is string) obj = obj.ToString().Trim(); dr[i] = obj; } IntoData.Rows.Add(dr); } dgInto2.ItemsSource = null; dgInto2.ItemsSource = IntoData; } catch (Exception ex) { LogAPI.Debug("加载区划调整数据失败,异常信息如下:"); LogAPI.Debug(ex); MessageHelper.ShowError("加载区划调整数据失败:" + ex.Message); } } /// /// 根据地类图斑划入过程构造列 /// /// private void ConstructColumn(IFields fields) { if (fields != null) { IntoData = new DataTable(); DataColumn col1 = new DataColumn(); col1.ColumnName = "IsValid"; col1.Caption = "选中"; col1.DataType = typeof(bool); IntoData.Columns.Add(col1); for (int i = 0; i < fields.FieldCount; i++) { IField field = fields.get_Field(i); if (field.Name.ToUpper().StartsWith("SHAPE")) continue; if ("SFJZ,NYJY,NYYPDL,TZ,BGDL,BGFW,WBGLX,SJLY,SFJCTB,BZ,BGZT,JCZT,XZQTZLX".Contains(field.Name)) continue; DataColumn col = new DataColumn(); col.ExtendedProperties.Add("index", i); col.ColumnName = field.Name; col.Caption = field.AliasName; switch (field.Type) { case esriFieldType.esriFieldTypeSmallInteger: col.DataType = typeof(short); break; case esriFieldType.esriFieldTypeInteger: col.DataType = typeof(int); break; case esriFieldType.esriFieldTypeSingle: break; case esriFieldType.esriFieldTypeDouble: col.DataType = typeof(double); break; case esriFieldType.esriFieldTypeString: col.DataType = typeof(string); break; case esriFieldType.esriFieldTypeDate: col.DataType = typeof(DateTime); break; case esriFieldType.esriFieldTypeOID: col.DataType = typeof(Int32); break; case esriFieldType.esriFieldTypeGeometry: break; case esriFieldType.esriFieldTypeBlob: break; case esriFieldType.esriFieldTypeRaster: break; case esriFieldType.esriFieldTypeGUID: break; case esriFieldType.esriFieldTypeGlobalID: break; case esriFieldType.esriFieldTypeXML: break; default: break; } col.ReadOnly = !field.Editable; IntoData.Columns.Add(col); Marshal.ReleaseComObject(field); } } } public void ClosePanel() { Platform.Instance.CloseView(this); } public void ClosePanelInvoke() { CloseViewHandler?.Invoke(this, null); } public void ShowPanel() { Platform.Instance.OpenView(this, false); } } }