txt格式資料轉換成dbf格式(C#)

來源:互聯網
上載者:User

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;
}
}

}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.