txt格式数据转换成dbf格式(C#)

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.esriSystem;
using System.Windows.Forms;
using ESRI.ArcGIS.DataSourcesFile;

namespace txttodbf
{
public class TxtToDbf
{
private string[] _ListName;

public string[] ListName
{
get { return _ListName; }
set { _ListName = value; }
}
private string _FileName;

public string FileName
{
get { return _FileName; }
set { _FileName = value; }
}
private string _FiledWater;

public string FiledWater
{
get { return _FiledWater; }
set { _FiledWater = value; }
}
private string _FiledTemp;

public string FiledTemp
{
get { return _FiledTemp; }
set { _FiledTemp = value; }
}
private string _OutPathWater;

public string OutPathWater
{
get { return _OutPathWater; }
set { _OutPathWater = value; }
}
private string _OutPathTemp;

public string OutPathTemp
{
get { return _OutPathTemp; }
set { _OutPathTemp = value; }
}

/// <summary>
/// 构造函数
/// </summary>
public TxtToDbf(string[] ListName, string FileName, string FiledWater, string FiledTemp, string OutPathWater, string OutPathTemp)
{
this.ListName = ListName;
this.FileName = FileName;
this.FiledWater = FiledWater;
this.FiledTemp = FiledTemp;
this.OutPathWater = OutPathWater;
this.OutPathTemp = OutPathTemp;
}

public TxtToDbf()
{
}
/// <summary>
///读取txt数据首行,取到数据位置索引
/// </summary>
private int[] MethodTxt()
{
int[] local=new int[4];
using (StreamReader sr = new StreamReader(FileName))//读取文件txt
{
string sLine = sr.ReadLine();
string dLine = sLine.Trim();
if (dLine.Length < 1)
{
return null;
}
else
{
//分解文本
List<string> strList = new List<string>();//泛型
string[] strs = dLine.Split(' ');
for (int i = 0; i < strs.Length; i++)
{
if (strs[i] != "")//过滤且分割
{
strList.Add(strs[i]);
}
}

for (int i = 0; i < strList.Count; i++)
{
if (strList[i].ToString() == "区站号")
{
local[0] = i;
}
}
for (int i = 0; i < strList.Count; i++)
{
if (strList[i].ToString() == "月")
{
local[1] = i;
}
}
for (int i = 0; i < strList.Count; i++)
{
if (strList[i].ToString() == "降水量")
{
local[2] = i;
}
}
for (int i = 0; i < strList.Count; i++)
{
if (strList[i].ToString() == "平均气温")
{
local[3] = i;
}
}
}
}
return local;
}
/// <summary>
///读取txt数据导出dbf
/// </summary>
public void MethodTxtToDbf()
{
try
{
int[] local = MethodTxt();
ITable WaterTable = BulitWaterTable();
ITable TempTable = BulitTempTable();
Dictionary<string, IRow> dicWater = new Dictionary<string, IRow>();
Dictionary<string, IRow> dicTemp = new Dictionary<string, IRow>();
int iLine = 0;
using (StreamReader sr = new StreamReader(FileName))//读取文件txt
{
while (!sr.EndOfStream)
{

iLine++;
string sLine = sr.ReadLine();

if (sLine.Length < 1 || iLine==1)
{
continue;
}
else
{
string dLine = sLine.Trim();
//分解文本
List<string> strList = new List<string>();//泛型
string[] strs = dLine.Split(' ');
for (int i = 0; i < strs.Length; i++)
{
if (strs[i] != "")//过滤且分割
{
strList.Add(strs[i]);
}
}
string station = strList[local[0]];
//降水
if (!dicWater.ContainsKey(station))
{
IRow pRow;
int i = WaterTable.FindField(ListName[0]);
pRow = WaterTable.CreateRow();
pRow.set_Value(i, station);
dicWater.Add(station, pRow);
int wmouth = Convert.ToInt32(strList[local[1]]);
int wm = wmouth + 1;
pRow.set_Value(wm, strList[local[2]]);
pRow.Store();
}
else
{
IRow pRow = dicWater[station];
int wmouth = Convert.ToInt32(strList[local[1]]);
int wm = wmouth + 1;
pRow.set_Value(wm, strList[local[2]]);
pRow.Store();
}

//
if (!dicTemp.ContainsKey(station))
{
IRow tRow;
int j = TempTable.FindField(ListName[0]);
tRow = TempTable.CreateRow();
tRow.set_Value(j, station);
dicTemp.Add(station, tRow);
int tmouth = Convert.ToInt32(strList[local[1]]);
int tm = tmouth+1;
tRow.set_Value(tm, strList[local[2]]);
tRow.Store();
}
else
{
IRow tRow = dicTemp[station];
int tmouth = Convert.ToInt32(strList[local[1]]);
int tm = tmouth + 1;
tRow.set_Value(tm, strList[local[3]]);
tRow.Store();
}

}

}
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}

}
/// <summary>
/// 生成dbf表
/// </summary>
private ITable BulitWaterTable()
{
//解析文件名
string myFactoryPath = System.IO.Path.GetDirectoryName(OutPathWater);
IWorkspaceFactory pShpFactory = new ShapefileWorkspaceFactoryClass();
IFeatureWorkspace pShpWS = pShpFactory.OpenFromFile(myFactoryPath, 0) as IFeatureWorkspace;

//创建结果文件
ITable pTable = this.CreateWaterTable(pShpWS);
if (pTable == null)
return null;
return pTable;
}
/// <summary>
/// 生成dbf表
/// </summary>
private ITable BulitTempTable()
{
//解析文件名
string myFactoryPath = System.IO.Path.GetDirectoryName(OutPathTemp);
IWorkspaceFactory pShpFactory = new ShapefileWorkspaceFactoryClass();
IFeatureWorkspace pShpWS = pShpFactory.OpenFromFile(myFactoryPath, 0) as IFeatureWorkspace;

//创建结果文件
ITable pTable = this.CreateTempTable(pShpWS);
if (pTable == null)
return null;
return pTable;
}
/// <summary>
/// 创建dbf表
/// </summary>
/// <param name="pFeatureWorkspace"></param>
/// <param name="pFeatureClass"></param>
/// <returns></returns>
private ITable CreateWaterTable(IFeatureWorkspace pFeatureWorkspace)
{
try
{
//创建所需参数
ESRI.ArcGIS.esriSystem.UID pUid = new ESRI.ArcGIS.esriSystem.UIDClass();
pUid.Value = "esriGeoDatabase.Object";
ESRI.ArcGIS.Geodatabase.IObjectClassDescription pObjectClassDescription = new ESRI.ArcGIS.Geodatabase.ObjectClassDescriptionClass();
IFields pFields = pObjectClassDescription.RequiredFields;
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;

//站字段
IField pStationField = new FieldClass();
IFieldEdit pStationFieldEdit = pStationField as IFieldEdit;
pStationFieldEdit.Name_2 = ListName[0];
pStationFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldsEdit.AddField(pStationField);

//月字段
for (int i = 1; i < 13; i++)
{
IField pCodeCountField = new FieldClass();
IFieldEdit pCodeCountFieldEdit = pCodeCountField as IFieldEdit;
pCodeCountFieldEdit.Name_2 = ListName[i];
pCodeCountFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
pFieldsEdit.AddField(pCodeCountField);
}
//平均值字段
IField pAveField = new FieldClass();
IFieldEdit pAveFieldEdit = pAveField as IFieldEdit;
pAveFieldEdit.Name_2 = ListName[13];
pAveFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
pFieldsEdit.AddField(pAveField);

//字段检查
ESRI.ArcGIS.Geodatabase.IFieldChecker fieldChecker = new ESRI.ArcGIS.Geodatabase.FieldCheckerClass();
ESRI.ArcGIS.Geodatabase.IEnumFieldError enumFieldError = null;
ESRI.ArcGIS.Geodatabase.IFields validatedFields = null;
fieldChecker.ValidateWorkspace = (ESRI.ArcGIS.Geodatabase.IWorkspace)pFeatureWorkspace;
fieldChecker.Validate(pFields, out enumFieldError, out validatedFields);
//创建
ITable pTable = pFeatureWorkspace.CreateTable(System.IO.Path.GetFileNameWithoutExtension(OutPathWater), validatedFields, pUid, null, "");

return pTable;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}

/// <summary>
/// 创建dbf表
/// </summary>
/// <param name="pFeatureWorkspace"></param>
/// <param name="pFeatureClass"></param>
/// <returns></returns>
private ITable CreateTempTable(IFeatureWorkspace pFeatureWorkspace)
{
try
{
//创建所需参数
ESRI.ArcGIS.esriSystem.UID pUid = new ESRI.ArcGIS.esriSystem.UIDClass();
pUid.Value = "esriGeoDatabase.Object";
ESRI.ArcGIS.Geodatabase.IObjectClassDescription pObjectClassDescription = new ESRI.ArcGIS.Geodatabase.ObjectClassDescriptionClass();
IFields pFields = pObjectClassDescription.RequiredFields;
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;

//站字段
IField pStationField = new FieldClass();
IFieldEdit pStationFieldEdit = pStationField as IFieldEdit;
pStationFieldEdit.Name_2 = ListName[0];
pStationFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldsEdit.AddField(pStationField);

//月字段
for (int i = 1; i < 13; i++)
{
IField pCodeCountField = new FieldClass();
IFieldEdit pCodeCountFieldEdit = pCodeCountField as IFieldEdit;
pCodeCountFieldEdit.Name_2 = ListName[i];
pCodeCountFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
pFieldsEdit.AddField(pCodeCountField);
}
//平均值字段
IField pAveField = new FieldClass();
IFieldEdit pAveFieldEdit = pAveField as IFieldEdit;
pAveFieldEdit.Name_2 = ListName[13];
pAveFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
pFieldsEdit.AddField(pAveField);
//字段检查
ESRI.ArcGIS.Geodatabase.IFieldChecker fieldChecker = new ESRI.ArcGIS.Geodatabase.FieldCheckerClass();
ESRI.ArcGIS.Geodatabase.IEnumFieldError enumFieldError = null;
ESRI.ArcGIS.Geodatabase.IFields validatedFields = null;
fieldChecker.ValidateWorkspace = (ESRI.ArcGIS.Geodatabase.IWorkspace)pFeatureWorkspace;
fieldChecker.Validate(pFields, out enumFieldError, out validatedFields);
//创建
ITable pTable = pFeatureWorkspace.CreateTable(System.IO.Path.GetFileNameWithoutExtension(OutPathTemp), validatedFields, pUid, null, "");

return pTable;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}

}
}

转载于:https://www.cnblogs.com/lxc-binary/archive/2013/05/15/3080290.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值