上一篇文章是简单的读取并输出,这里稍微加深一点,将读取到的内容按字段存入DataTable中。
1 StringBuilder sb = new StringBuilder(); 2 NotesSession ns = new NotesSession(); 3 //ns.Initialize("test1234"); 4 ns.Initialize(); 5 if (ns == null) 6 { 7 MessageBox.Show("未能初始化"); 8 return; 9 } 10 //NotesDatabase db = ns.GetDatabase("", @"names.nsf", false); 11 NotesDatabase db = ns.GetDatabase("", @"todo/120006.nsf", false); 12 if (db == null) 13 { 14 MessageBox.Show("未能初始化数据库"); 15 return; 16 } 17 NotesView view = db.GetView(@"V5\01.待办文件"); 18 if (view == null) return; 19 DataTable dt = new DataTable(); 20 object[] cols = view.ColumnNames; 21 Dictionary<int, object> dic = new Dictionary<int, object>(); 22 if (cols != null) 23 { 24 int ix = 0; 25 foreach (object obj in cols) 26 { 27 dic.Add(ix, obj); 28 if (obj != null) 29 { 30 sb.Append(string.Format("{0};", obj)); 31 if (!dt.Columns.Contains(obj.ToString())) 32 { 33 DataColumn dc = new DataColumn(obj.ToString()); 34 dt.Columns.Add(dc); 35 } 36 37 } 38 ix++; 39 } 40 } 41 NotesDocument doc = view.GetFirstDocument(); 42 while (doc != null) 43 { 44 object[] objs = (object[])doc.ColumnValues; 45 if (objs == null) return; 46 int ix = 0; 47 DataRow dr = dt.NewRow(); 48 foreach (object obj in objs) 49 { 50 if (obj == null) continue; 51 KeyValuePair<int, object> kv = dic.FirstOrDefault(m => m.Key == ix); 52 Type tp = obj.GetType(); 53 if (tp.Name.Contains("Object[]")) 54 { 55 object[] nobjs = (object[])obj; 56 List<string> list = new List<string>(); 57 foreach (var nobj in nobjs) 58 { 59 sb.Append(string.Format("{0}\r\n", nobj)); 60 list.Add(string.Format("{0}",nobj)); 61 } 62 dr[kv.Value.ToString()] = string.Join("|", list); 63 } 64 else 65 { 66 sb.Append(string.Format("{0}\r\n", obj)); 67 dr[kv.Value.ToString()] = obj; 68 } 69 ix++; 70 } 71 dt.Rows.Add(dr); 72 doc = view.GetNextDocument(doc); 73 }