arcEngine根据某一唯一字段,批量挂接xls表格的字段值到shp中,适用于地理国情、基础测绘或者类似大量数据的字段挂接。需要office依赖支持。

37 篇文章 3 订阅
3 篇文章 1 订阅

arcEngine根据某一唯一字段,批量挂接xls表格的字段值到shp中,适用于地理国情、基础测绘或者类似大量数据的字段挂接。需要office依赖支持。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using System.IO;
using Microsoft.Office.Interop.Excel;
using System.Diagnostics;

namespace AETest101
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);

            InitializeComponent();
        }

        private void btn_AppdendAttribute_Click(object sender, EventArgs e)
        {
            //1.读取Shp并添加字段

            string shpPath = @"D:\TestData\范围\全省市面2020.shp";

            IFeatureClass pFClass = GetShpFile(shpPath);

            ITable pTable = pFClass as ITable;     //use ITable or IClass 
            int oldFieldsCount = pFClass.Fields.FieldCount;
            int k = 0;
            for (int i = 0; i < pFClass.FeatureCount(null); i++)
            {
                IFeature pFeature = pFClass.GetFeature(i);
                k = pFeature.Fields.FindField("分类标识");

                string xlsName = pFeature.get_Value(k);
                string xlsPath = System.IO.Path.GetDirectoryName(shpPath) + "\\" + xlsName + ".xls";
                if (File.Exists(xlsPath))
                {
                    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                    excel.Visible = false;
                    Workbook wb = excel.Workbooks.Open(xlsPath);
                    Worksheet ws = wb.Sheets[1] as Worksheet;

                    int rowCount = 0;//有效行,索引从1开始
                    try
                    {
                        //循环行
                        for (int j = 1; j <= ws.UsedRange.Rows.Count; j++)//
                        {
                            //循环列
                            for (int m = 1; m <= ws.UsedRange.Columns.Count; m++)
                            {
                                if (ws.Rows[i] != null)
                                {
                                    string val = ws.Cells[j, m].ToString();//取单元格值

                                    for (int n = oldFieldsCount; n < oldFieldsCount + ws.UsedRange.Rows.Count; n++)
                                    {
                                        pFeature.set_Value(n, val);
                                        pFeature.Store();
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    finally
                    {
                        ClosePro(xlsPath, excel, wb);
                    }
                }
            }
        }
        
        /// <summary>
        /// 关闭Excel进程
        /// </summary>
        /// <param name="excelPath"></param>
        /// <param name="excel"></param>
        /// <param name="wb"></param>
        public void ClosePro(string excelPath, Microsoft.Office.Interop.Excel.Application excel, Microsoft.Office.Interop.Excel.Workbook wb)
        {
            Process[] localByNameApp = Process.GetProcessesByName(excelPath);//获取程序名的所有进程
            if (localByNameApp.Length > 0)
            {
                foreach (var app in localByNameApp)
                {
                    if (!app.HasExited)
                    {
                        #region
                        设置禁止弹出保存和覆盖的询问提示框   
                        //excel.DisplayAlerts = false;
                        //excel.AlertBeforeOverwriting = false;

                        保存工作簿   
                        //excel.Application.Workbooks.Add(true).Save();
                        保存excel文件   
                        //excel.Save("D:" + "\\test.xls");
                        确保Excel进程关闭   
                        //excel.Quit();
                        //excel = null; 
                        #endregion
                        app.Kill();//关闭进程  
                    }
                }
            }
            if (wb != null)
                wb.Close(true, Type.Missing, Type.Missing);
            excel.Quit();
            // 安全回收进程
            System.GC.GetGeneration(excel);
        }

        private static IFeatureClass GetShpFile(string fileFullPath)
        {
            string filePath = System.IO.Path.GetDirectoryName(fileFullPath);
            string fileName = System.IO.Path.GetFileName(fileFullPath);

            IWorkspaceFactory shapefileWSF = new ShapefileWorkspaceFactory();
            IWorkspace shapefileWS = shapefileWSF.OpenFromFile(filePath, 0);
            IFeatureWorkspace pFeatureWorkspace = shapefileWS as IFeatureWorkspace;

            IFeatureClass pFeatureClass;
            try
            {
                pFeatureClass = pFeatureWorkspace.OpenFeatureClass(fileName);
            }
            catch
            {
                pFeatureClass = null;
            }

            return pFeatureClass;
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木易GIS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值