XY display having a plurality of fields stored as Excel spreadsheets and point layer

Highlights! ! ! shapefile attribute table field names layer is indefinite length (specifically how long you can look at in ArcGIS), Excel field name not too long, otherwise, when reading table creation featureClass will judge wrong.
As the above data into Excel point layer (.shp)As the above data into Excel point layer (.shp)

Function and Description
(1)
Function: Excel data creation IFeatureClass
input data: Excel full path
output data: IfeatureClass

public IFeatureClass getExcelTable(string path)
        {
            string name = System.IO.Path.GetFileNameWithoutExtension(path);
            ESRI.ArcGIS.esriSystem.IPropertySet proset = new ESRI.ArcGIS.esriSystem.PropertySetClass();
            string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + @";Extended Properties=Excel 8.0";
            proset.SetProperty("CONNECTSTRING", strcon);

            IWorkspaceFactory workspf = new ESRI.ArcGIS.DataSourcesOleDB.OLEDBWorkspaceFactoryClass();
            IFeatureWorkspace fworkfac = workspf.Open(proset, 0) as IFeatureWorkspace;    
            IQueryDef myQueryDef = fworkfac.CreateQueryDef();
            myQueryDef.Tables = "["+name+"$]";
            myQueryDef.SubFields = "*";
            myQueryDef.WhereClause = "";    
            ICursor myCursor = myQueryDef.Evaluate();
            IFields myfields = myCursor.Fields;    
            IRow myRow;
            IPoint point = new PointClass();
            
            IFeatureClass newfeatureclass = creatFeatureClass(myfields,name);
            ISpatialReferenceFactory srFactory = new SpatialReferenceEnvironmentClass();
            ISpatialReference sr1;
            IGeographicCoordinateSystem pcs = srFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
            sr1 = pcs;

            while ((myRow = myCursor.NextRow()) != null)
            {
                string str="",strr="";
                point.PutCoords(double.Parse(str=myRow.get_Value(3).ToString()), double.Parse(strr=myRow.get_Value(4).ToString()));
                point.SpatialReference = sr1;
                IFeatureCursor m_cursor = newfeatureclass.Insert(true);
                IFeatureBuffer m_buffer = newfeatureclass.CreateFeatureBuffer();
                m_buffer.Shape = point;
                for (int i = 0; i < myfields.FieldCount; i++)
                {
                    string sttt = "";
                    if (myfields.get_Field(i).Name.Equals("经度(度)") || myfields.get_Field(i).Name.Equals("纬度(度)") || myfields.get_Field(i).Name.Equals("海拔(米)") || myfields.get_Field(i).Name.Equals("V13305") || myfields.get_Field(i).Name.Equals("V12001_701") || myfields.get_Field(i).Name.Equals("总辐射"))
                    {
                        double a;
                        m_buffer.set_Value(newfeatureclass.FindField(sttt = myfields.get_Field(i).Name), a=double.Parse(myRow.get_Value(i).ToString()));
                    }
                    else
                    {
                        m_buffer.set_Value(newfeatureclass.FindField(sttt = myfields.get_Field(i).Name), myRow.get_Value(i).ToString());                            
                    }                        
                }
                m_cursor.InsertFeature(m_buffer);
                m_cursor.Flush();
            }            
            return newfeatureclass;
        }

(2)
Function: Function (1) call the subroutine, create FeatureClass
input data: Field and Excel spreadsheet worksheet name or the name of the acquired table (table name or sheet name as the name of FeatureClass, form fields as a new FeatureClass field).
Output function: IFeatureClass

private IFeatureClass creatFeatureClass(IFields myfields,string name)
        {

            string tempStorePath = @"C:\Users\Administrator\Desktop\newnew\t1";
            IWorkspaceFactory workspacefactory=new ShapefileWorkspaceFactory();
            IFeatureWorkspace featureworkspace = (IFeatureWorkspace)workspacefactory.OpenFromFile(tempStorePath, 0);
            string layerName = name;

            IFields pFields = new FieldsClass();
            IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;

            IField shapeField = new FieldClass();
            IFieldEdit shapeEdit = shapeField as IFieldEdit;
            shapeEdit.Name_2 = "Shape";
            shapeEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
            IGeometryDef pGeometoryDef = new GeometryDefClass();
            IGeometryDefEdit pGeometoryDefEdit = pGeometoryDef as IGeometryDefEdit;
            pGeometoryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
            ISpatialReferenceFactory srFactory = new SpatialReferenceEnvironmentClass();
            ISpatialReference sr1;
            int a = 0;
            IGeographicCoordinateSystem pcs = srFactory.CreateGeographicCoordinateSystem(a=(int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
            sr1 = pcs;
            pGeometoryDefEdit.SpatialReference_2 = sr1;
            shapeEdit.GeometryDef_2 = pGeometoryDefEdit;
            pFieldsEdit.AddField(shapeField);

            //List<Field> fids = new List<Field>();
            for (int i = 0; i < myfields.FieldCount; i++)
            {
                IField x = new FieldClass();
                IFieldEdit xx = x as IFieldEdit;
                xx.Name_2 = myfields.get_Field(i).Name;
                if (myfields.get_Field(i).Name.Equals("经度(度)") || myfields.get_Field(i).Name.Equals("纬度(度)") || myfields.get_Field(i).Name.Equals("海拔(米)") || myfields.get_Field(i).Name.Equals("V13305") || myfields.get_Field(i).Name.Equals("V12001_701") || myfields.get_Field(i).Name.Equals("总辐射"))
                {
                    xx.Type_2 = esriFieldType.esriFieldTypeDouble;
                }
                else
                {
                    xx.Type_2 = esriFieldType.esriFieldTypeString;
                }                
                pFieldsEdit.AddField(x);    
            }

(3)
Function: IFeatureClass obtained point to a derived layer
of input data: IFeatureClass and folder path (derived result is stored in this folder)

public void ExportFeature(IFeatureClass pInFeatureClass, string pPath)
        {   
            IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
            string parentPath = pPath.Substring(0, pPath.LastIndexOf('\\'));
            string fileName = pPath.Substring(pPath.LastIndexOf('\\') + 1, pPath.Length - pPath.LastIndexOf('\\') - 1);
            IWorkspaceName pWorkspaceName = pWorkspaceFactory.Create(parentPath, fileName, null, 0);   
            IName name = (IName)pWorkspaceName;  
            IWorkspace pOutWorkspace = (IWorkspace)name.Open();    
            IDataset pInDataset = pInFeatureClass as IDataset;
            IFeatureClassName pInFCName = pInDataset.FullName as IFeatureClassName;
            IWorkspace pInWorkspace = pInDataset.Workspace;
            IDataset pOutDataset = pOutWorkspace as IDataset;
            IWorkspaceName pOutWorkspaceName = pOutDataset.FullName as IWorkspaceName;
            IFeatureClassName pOutFCName = new FeatureClassNameClass();
            IDatasetName pDatasetName = pOutFCName as IDatasetName;
            pDatasetName.WorkspaceName = pOutWorkspaceName;
            pDatasetName.Name = pInFeatureClass.AliasName;
            IFieldChecker pFieldChecker = new FieldCheckerClass();
            pFieldChecker.InputWorkspace = pInWorkspace;
            pFieldChecker.ValidateWorkspace = pOutWorkspace;
            IFields pFields = pInFeatureClass.Fields;
            IFields pOutFields;
            IEnumFieldError pEnumFieldError;
            pFieldChecker.Validate(pFields, out pEnumFieldError, out pOutFields);
            IFeatureDataConverter pFeatureDataConverter = new FeatureDataConverterClass();
            pFeatureDataConverter.ConvertFeatureClass(pInFCName, null, null, pOutFCName, null, pOutFields, "", 100, 0);
        }

There are online reference code, articles, learning to use, welcomed the exchange of learning.

Guess you like

Origin blog.csdn.net/qq_40821274/article/details/84312007