public static void ImportTextToTable(string strFileName, string strTablename, bool isCovered) { SysCommon.CProgress vProgress = new SysCommon.CProgress("进度条"); vProgress.EnableCancel = false; vProgress.ShowDescription = true; vProgress.FakeProgress = false; vProgress.TopMost = true; //vProgress.MaxValue = 5; vProgress.ShowProgress(); vProgress.SetProgress("导入Text文档"); IFeatureWorkspace pFeatureWks = null; ITable pTable = null; try { pFeatureWks = Plugin.ModuleCommon.TmpWorkSpace as IFeatureWorkspace; pTable = pFeatureWks.OpenTable(strTablename); } catch { vProgress.Close(); return; } if (pTable == null) { vProgress.Close(); return; } if (!System.IO.File.Exists(strFileName)) { vProgress.Close(); return; } ///记录读取txt中的字段名称 List <string> pLstFieldName = new List <string>(); ///记录一行字段存储的字段值与字段名一一对应 List <string[]> pLstFieldValue = new List <string[]>(); ///记录读取txt的第几行 int iNum = 1; using (System.IO.StreamReader sr = System.IO.File.OpenText(strFileName)) { String input; while ((input = sr.ReadLine()) != null) { string[] strValue = input.Split('\t'); if (iNum == 1) { for (int i = 0; i < strValue.Length; i++) { pLstFieldName.Add(strValue[i]); } } else if (strValue.Length != 0) { pLstFieldValue.Add(strValue); } iNum++; } } Dictionary <string, object> pDic = new Dictionary <string, object>(); SysCommon.Gis.SysGisTable pSystable = new SysCommon.Gis.SysGisTable(Plugin.ModuleCommon.TmpWorkSpace); Exception eError = null; try { if (isCovered) { vProgress.SetProgress("清除'" + strTablename + "'中记录"); pSystable.DeleteRows(strTablename, "", out eError); } vProgress.MaxValue = pLstFieldValue.Count; vProgress.SetProgress("将Text文档中内容写入'" + strTablename + "'"); pSystable.StartTransaction(out eError); for (int j = 0; j < pLstFieldValue.Count; j++) { vProgress.Step = j + 1; pDic.Clear(); try { for (int n = 0; n < pLstFieldName.Count; n++) { pDic.Add(pLstFieldName[n].ToString(), pLstFieldValue[j][n].ToString()); } pSystable.NewRowByAliasName(strTablename, pDic, out eError); //shduan 20110730暂时屏蔽 } catch { } } pSystable.EndTransaction(true, out eError); vProgress.Close(); } catch { MessageBox.Show("导入失败请查看txt文档格式是否正确!", "提示!"); vProgress.Close(); } }
//added by chulili 20110715 //导入excel文件,内容写入数据字典所选表格中 //excel文件第一行对应表格的字段名称(别名),第二行开始是记录内容 public static void ImportExcelToTable(string strFilename, string strTablename, bool isCovered) { SysCommon.CProgress vProgress = new SysCommon.CProgress("进度条"); vProgress.EnableCancel = false; vProgress.ShowDescription = true; vProgress.FakeProgress = false; vProgress.TopMost = true; vProgress.MaxValue = 5; vProgress.ShowProgress(); vProgress.SetProgress("导入Excel表格"); IFeatureWorkspace pFeatureWks = null; ITable pTable = null; try { pFeatureWks = Plugin.ModuleCommon.TmpWorkSpace as IFeatureWorkspace; pTable = pFeatureWks.OpenTable(strTablename); } catch { vProgress.Close(); return; } if (pTable == null) { vProgress.Close(); return; } string strConn; vProgress.SetProgress(1, "打开Excel文件"); strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strFilename + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); //获取EXCEL文件内工作表名称 added by chulili 20110924 DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); string SheetName = "Sheet1$"; if (dt != null) { if (dt.Rows.Count > 0) { SheetName = dt.Rows[0][2].ToString();//默认取第一张工作表 } } dt = null; OleDbCommand pCommand = null; OleDbDataReader pReader = null; try { vProgress.SetProgress("读取Excel文件表格内容"); pCommand = new OleDbCommand("SELECT * FROM [" + SheetName + "]", conn); pReader = pCommand.ExecuteReader(); } catch { conn.Close(); vProgress.Close(); return; } if (pReader == null) { conn.Close(); vProgress.Close(); return; } //Dictionary<int, int> pDicFieldname = new Dictionary<int, int>(); //for (int i = 0; i < pReader.FieldCount; i++) //{ // string strFieldname=pReader.GetName(i); // int j = pTable.Fields.FindFieldByAliasName(strFieldname); // pDicFieldname.Add(i, j); //} Dictionary <string, object> pDic = new Dictionary <string, object>(); SysCommon.Gis.SysGisTable pSystable = new SysCommon.Gis.SysGisTable(Plugin.ModuleCommon.TmpWorkSpace); Exception eError = null; try { if (isCovered) { vProgress.SetProgress("清除'" + strTablename + "'中记录"); pSystable.DeleteRows(strTablename, "", out eError); } vProgress.SetProgress("将Excel中表格内容写入'" + strTablename + "'"); pSystable.StartTransaction(out eError); while (pReader.Read()) { pDic.Clear(); for (int i = 0; i < pReader.FieldCount; i++) { pDic.Add(pReader.GetName(i).Trim(), pReader[i].ToString()); } pSystable.NewRowByAliasName(strTablename, pDic, out eError); //shduan 20110730暂时屏蔽 } pSystable.EndTransaction(true, out eError); pReader.Close(); conn.Close(); vProgress.Close(); } catch { pReader.Close(); conn.Close(); vProgress.Close(); } }