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;
}
}
}