Loading

ArcEngine ITable 与System.DataTable相互转换

/// <summary>
/// 打开dbf表
/// </summary>
/// <param name="pathName"></param>
/// <param name="tableName"></param>
/// <returns></returns>
public static ITable OpenTable(string pathName, string tableName)
{
    // Create the workspace name object.
    IWorkspaceName workspaceName = new WorkspaceNameClass();
    workspaceName.PathName = pathName;
    workspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.shapefileworkspacefactory";
    // Create the table name object.
    IDatasetName dataSetName = new TableNameClass();
    dataSetName.Name = tableName;
    dataSetName.WorkspaceName = workspaceName;
    // Open the table.
    IName name = (IName)dataSetName;
    ITable table = (ITable)name.Open();
    return table;
}

/// <summary>
/// 将ITable转换为DataTable
/// </summary>
/// <param name="mTable"></param>
/// <returns></returns>
public static DataTable ToDataTable(ITable mTable)
{
    try
    {
        DataTable pTable = new DataTable();
        for (int i = 0; i < mTable.Fields.FieldCount; i++)
            pTable.Columns.Add(mTable.Fields.get_Field(i).Name);

        ICursor pCursor = mTable.Search(null, false);
        IRow pRrow = pCursor.NextRow();
        while (pRrow != null)
        {
            DataRow pRow = pTable.NewRow();
            string[] StrRow = new string[pRrow.Fields.FieldCount];
            for (int i = 0; i < pRrow.Fields.FieldCount; i++)
                StrRow[i] = pRrow.get_Value(i).ToString();
            pRow.ItemArray = StrRow;
            pTable.Rows.Add(pRow);
            pRrow = pCursor.NextRow();
        }

        return pTable;
    }
    catch (Exception ex)
    {
        return null;
    }
}

/// <summary>
/// 把DataTable转为ITable ,tempPath 不含文件名的问价夹路径
/// </summary>
/// <param name="mTable"></param>
/// <returns></returns>
public static  ITable  ToITable(DataTable  mTable,string tempPath)
{
    try
    {
        #region 新建表字段
            
		IField pField = null;
        IFields fields = new FieldsClass();
        IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
        fieldsEdit.FieldCount_2 = 3;

        pField = new FieldClass();
        IFieldEdit fieldEdit = (IFieldEdit)pField;
        fieldEdit.Name_2 = "FromField";
        fieldEdit.AliasName_2 = "开始字段值";
        fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
        fieldEdit.Editable_2 = true;

        //添加开始字段
        fieldsEdit.set_Field(0, pField);

        IField pField1 = new FieldClass();
        IFieldEdit fieldEdit1 = (IFieldEdit)pField1;
        fieldEdit1.Name_2 = "ToField";
        fieldEdit1.AliasName_2 = "结束字段值";
        fieldEdit1.Type_2 = esriFieldType.esriFieldTypeDouble;
        fieldEdit1.Editable_2 = true;

        //添加结束字段
        fieldsEdit.set_Field(1, pField1);

        IField pField2 = new FieldClass();
        IFieldEdit fieldEdit2 = (IFieldEdit)pField2;
        fieldEdit2.Name_2 = "outField";
        fieldEdit2.AliasName_2 = "分类字段值";
        fieldEdit2.Type_2 = esriFieldType.esriFieldTypeDouble;
        fieldEdit2.Editable_2 = true;
        //添加重分类字段
        fieldsEdit.set_Field(2, pField2);

        #endregion

        ShapefileWorkspaceFactoryClass class2 = new ShapefileWorkspaceFactoryClass();
        ESRI.ArcGIS.Geodatabase.IWorkspace pWorkspace = class2.OpenFromFile(tempPath, 0);
        IFeatureWorkspace pFWS = pWorkspace as IFeatureWorkspace;

        //删除已有的
        if (System.IO.File.Exists(tempPath + "重分类.dbf"))
            System.IO.File.Delete(tempPath + "重分类.dbf");

        //创建空表
        ESRI.ArcGIS.Geodatabase.ITable pTable;
        pTable = pFWS.CreateTable("重分类", fieldsEdit, null, null, "");

        //获取表中记录数
        int count=mTable .Rows .Count ;

        //转换为ITable中的数据
        for(int k=0;k<count ;k++)
        {
            //ITable 的记录
            IRow row = pTable.CreateRow();
            DataRow pRrow=mTable .Rows[k];
            //列元素
            int rowNum= pRrow .ItemArray.Length;
            // 添加记录
            for (int n=1;n<rowNum+1 ;n++)
            {
                row.set_Value(n,pRrow.ItemArray.GetValue(n-1));
                row.Store ();
            }
        }
        return pTable ;
    }
    catch (Exception ex)
    {
        return null;
    }
}

/// <summary>
///保存DataTable表位DBF ,tempPath 文件完整路径
/// </summary>
/// <param name="mTable"></param>
/// <returns></returns>
public static bool  SaveTable(DataTable mTable, string tempPath)
{
    try
    {
        #region 新建表字段

        IField pField = null;
        IFields fields = new FieldsClass();
        IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
        fieldsEdit.FieldCount_2 = 3;

        pField = new FieldClass();
        IFieldEdit fieldEdit = (IFieldEdit)pField;
        fieldEdit.Name_2 = "FromField";
        fieldEdit.AliasName_2 = "开始字段值";
        fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
        fieldEdit.Editable_2 = true;

        //添加开始字段
        fieldsEdit.set_Field(0, pField);
        IField pField1 = new FieldClass();
        IFieldEdit fieldEdit1 = (IFieldEdit)pField1;
        fieldEdit1.Name_2 = "ToField";
        fieldEdit1.AliasName_2 = "结束字段值";
        fieldEdit1.Type_2 = esriFieldType.esriFieldTypeDouble;
        fieldEdit1.Editable_2 = true;

        //添加结束字段
        fieldsEdit.set_Field(1, pField1);

        IField pField2 = new FieldClass();
        IFieldEdit fieldEdit2 = (IFieldEdit)pField2;
        fieldEdit2.Name_2 = "outField";
        fieldEdit2.AliasName_2 = "分类字段值";
        fieldEdit2.Type_2 = esriFieldType.esriFieldTypeDouble;
        fieldEdit2.Editable_2 = true;
        //添加重分类字段
        fieldsEdit.set_Field(2, pField2);

        #endregion

        string path = System.IO.Path.GetDirectoryName(tempPath);
        string fileName = System.IO.Path.GetFileName(tempPath);

        ShapefileWorkspaceFactoryClass class2 = new ShapefileWorkspaceFactoryClass();
        ESRI.ArcGIS.Geodatabase.IWorkspace pWorkspace = class2.OpenFromFile(path, 0);
        IFeatureWorkspace pFWS = pWorkspace as IFeatureWorkspace;

        //删除已有的
        if (System.IO.File.Exists(tempPath))
            System.IO.File.Delete(tempPath);

        fileName = fileName.Split('.')[0];
        //创建空表
        ESRI.ArcGIS.Geodatabase.ITable pTable;
        pTable = pFWS.CreateTable(fileName, fieldsEdit, null, null, "");

        //获取表中记录数
        int count = mTable.Rows.Count;

        //转换为ITable中的数据
        for (int k = 0; k < count; k++)
        {
            //ITable 的记录
            IRow row = pTable.CreateRow();
            DataRow pRrow = mTable.Rows[k];
            //列元素
            int rowNum = pRrow.ItemArray.Length;
            // 添加记录
            for (int n = 1; n < rowNum + 1; n++)
            {
                row.set_Value(n, pRrow.ItemArray.GetValue(n - 1));
                row.Store();
            }
        }

        return true ;
    }
    catch (Exception ex)
    {
        return false ;
    }
}
posted @ 2019-09-17 10:55  F(x)_King  阅读(454)  评论(0编辑  收藏  举报