很久没写ArcEngine的内容了,最近碰巧有个朋友需要利用excel文件生成点shp,所以就和大家分享一下利用ArcEngine读取excel文件生成点shp的代码。测试的excel如下所示,一共三个字段。
一般我们会使用NPOI对excel文件进行读写,工程中利用NuGet引入NPOI即可,代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
// ESRI
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
// NPOI
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;
namespace WindowsFormsApplication1
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void btnCreateShp_Click(object sender, EventArgs e)
{
// 创建要素类
IFeatureClass pFeatureClass = CreateFeatureClass(@"C:\Users\DSF\Desktop\point.shp");
// 插入要素
InsertFeatures(pFeatureClass, @"C:\Users\DSF\Desktop\test.xlsx");
}
/// <summary>
/// 创建WGS-84参考
/// </summary>
/// <returns></returns>
private ISpatialReference CreateSpatialReference()
{
ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironment();
ISpatialReference pSpatialReference = pSpatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
return pSpatialReference;
}
/// <summary>
/// 创建要素类
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
private IFeatureClass CreateFeatureClass(string filePath)
{
IGeometryDef pGeometryDef = new GeometryDef();
IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
pGeometryDefEdit.HasM_2 = false;
pGeometryDefEdit.HasZ_2 = false;
pGeometryDefEdit.SpatialReference_2 = CreateSpatialReference();
// 字段集合
IFields pFields = new Fields();
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
// Shape
IField pField = new Field();
IFieldEdit pFieldEdit = pField as IFieldEdit;
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
pFieldEdit.GeometryDef_2 = pGeometryDef;
pFieldEdit.AliasName_2 = "Shape";
pFieldEdit.Name_2 = "Shape";
pFieldEdit.IsNullable_2 = false;
pFieldEdit.Required_2 = true;
pFieldsEdit.AddField(pField);
// 经度
pField = new Field();
pFieldEdit = pField as IFieldEdit;
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldEdit.AliasName_2 = "经度";
pFieldEdit.Name_2 = "经度";
pFieldsEdit.AddField(pField);
// 纬度
pField = new Field();
pFieldEdit = pField as IFieldEdit;
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldEdit.AliasName_2 = "纬度";
pFieldEdit.Name_2 = "纬度";
pFieldsEdit.AddField(pField);
// 名称
pField = new Field();
pFieldEdit = pField as IFieldEdit;
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldEdit.AliasName_2 = "名称";
pFieldEdit.Name_2 = "名称";
pFieldsEdit.AddField(pField);
// 创建要素类
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(filePath), 0);
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
IFeatureClass pFeatureClass = pFeatureWorkspace.CreateFeatureClass(System.IO.Path.GetFileName(filePath), pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
return pFeatureClass;
}
/// <summary>
/// 插入点数据
/// </summary>
/// <param name="pFeatureClass"></param>
/// <param name="filePath"></param>
private void InsertFeatures(IFeatureClass pFeatureClass, string filePath)
{
using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
NPOI.SS.UserModel.IWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(fs);
NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(0);
NPOI.SS.UserModel.IRow row = null;
// 要素游标
IFeatureBuffer pFeatureBuffer = pFeatureClass.CreateFeatureBuffer();
IFeatureCursor pFeatureCursor = pFeatureClass.Insert(true);
// 字段索引
int fieldIndex_X = pFeatureClass.Fields.FindField("经度");
int fieldIndex_Y = pFeatureClass.Fields.FindField("纬度");
int fieldIndex_Z = pFeatureClass.Fields.FindField("名称");
// 字段值
string fieldValue_X = string.Empty;
string fieldValue_Y = string.Empty;
string fieldValue_Z = string.Empty;
// 遍历excel数据行
for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
{
row = sheet.GetRow(i);
// 读取单元格
fieldValue_X = row.Cells[0].ToString();
fieldValue_Y = row.Cells[1].ToString();
fieldValue_Z = row.Cells[2].ToString();
// 创建坐标点
ESRI.ArcGIS.Geometry.IPoint pPoint = new ESRI.ArcGIS.Geometry.Point();
pPoint.X = double.Parse(fieldValue_X);
pPoint.Y = double.Parse(fieldValue_Y);
// 设置字段值
pFeatureBuffer.Shape = pPoint;
pFeatureBuffer.set_Value(fieldIndex_X, fieldValue_X);
pFeatureBuffer.set_Value(fieldIndex_Y, fieldValue_Y);
pFeatureBuffer.set_Value(fieldIndex_Z, fieldValue_Z);
pFeatureCursor.InsertFeature(pFeatureBuffer);
}
pFeatureCursor.Flush();
// 释放游标
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureBuffer);
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
}
}
}
}
结果如下所示: