【转】ArcGIS Engine ITable 与System.DataTable相互转换



 源码不知道出自哪了!没有验证过,但应该可以用

  /// <summary>

  /// 打开dbf

  /// </summary>

  /// <param name="pathName"></param>

  /// <param name="tableName"></param>

  /// <returns></returns>

  public static ITable OpenTable(stringpathName, 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(ITablemTable)

  {

   try

   {

   DataTablepTable = new DataTable();

   for (int i = 0; i < mTable.Fields.FieldCount; i++)

   {

   pTable.Columns.Add(mTable.Fields.get_Field(i).Name);

   }

 

   ICursorpCursor = mTable.Search(null, false);

   IRow pRrow= pCursor.NextRow();

   while (pRrow!= null)

   {

    DataRowpRow = 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();

   }

 

   returnpTable;

   }

   catch (Exceptionex)

   {

   return null;

   }

  }

 

  /// <summary>

  /// DataTable转为ITable,tempPath 不含文件名的问价夹路径

  /// </summary>

  /// <param name="mTable"></param>

  /// <returns></returns>

  public static ITable ToITable(DataTablemTable, string tempPath)

  {

   try

   {

   #region 新建表字段

 

   IFieldpField = null;

 

   IFieldsfields = new FieldsClass();

   IFieldsEditfieldsEdit = (IFieldsEdit)fields;

   fieldsEdit.FieldCount_2 = 3;

 

   pField = new FieldClass();

   IFieldEditfieldEdit = (IFieldEdit)pField;

   fieldEdit.Name_2 = "FromField";

   fieldEdit.AliasName_2 = "开始字段值";

   fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;

   fieldEdit.Editable_2 = true;

 

   //添加开始字段

   fieldsEdit.set_Field(0, pField);

 

   IFieldpField1 = new FieldClass();

   IFieldEditfieldEdit1 = (IFieldEdit)pField1;

   fieldEdit1.Name_2 = "ToField";

   fieldEdit1.AliasName_2 = "结束字段值";

   fieldEdit1.Type_2 = esriFieldType.esriFieldTypeDouble;

   fieldEdit1.Editable_2 = true;

 

   //添加结束字段

   fieldsEdit.set_Field(1, pField1);

 

   IFieldpField2 = new FieldClass();

   IFieldEditfieldEdit2 = (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);

   IFeatureWorkspacepFWS = 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();

 

    DataRowpRrow = 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();

    }

   }

   returnpTable;

   }

   catch (Exceptionex)

   {

   return null;

   }

  }

 

  /// <summary>

  ///保存DataTable表位DBF ,tempPath 文件完整路径

  /// </summary>

  /// <param name="mTable"></param>

  /// <returns></returns>

  public static bool SaveTable(DataTablemTable, string tempPath)

  {

   try

   {

   #region 新建表字段

 

   IFieldpField = null;

 

   IFieldsfields = new FieldsClass();

   IFieldsEditfieldsEdit = (IFieldsEdit)fields;

   fieldsEdit.FieldCount_2 = 3;

 

   pField = new FieldClass();

   IFieldEditfieldEdit = (IFieldEdit)pField;

   fieldEdit.Name_2 = "FromField";

   fieldEdit.AliasName_2 = "开始字段值";

   fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;

   fieldEdit.Editable_2 = true;

 

   //添加开始字段

   fieldsEdit.set_Field(0, pField);

 

   IFieldpField1 = new FieldClass();

   IFieldEditfieldEdit1 = (IFieldEdit)pField1;

   fieldEdit1.Name_2 = "ToField";

   fieldEdit1.AliasName_2 = "结束字段值";

   fieldEdit1.Type_2 = esriFieldType.esriFieldTypeDouble;

   fieldEdit1.Editable_2 = true;

 

   //添加结束字段

   fieldsEdit.set_Field(1, pField1);

 

   IFieldpField2 = new FieldClass();

   IFieldEditfieldEdit2 = (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);

   stringfileName = System.IO.Path.GetFileName(tempPath);

 

   ShapefileWorkspaceFactoryClass class2 = new ShapefileWorkspaceFactoryClass();

   ESRI.ArcGIS.Geodatabase.IWorkspace pWorkspace = class2.OpenFromFile(path,0);

   IFeatureWorkspacepFWS = 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();

 

    DataRowpRrow = 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 (Exceptionex)

   {

   return false;

   }

  }

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值