读取数据集前两列并写入到文本
... ... /// <summary> /// 创建Txt文本 /// </summary> /// <param name="name"></param> /// <param name="str"></param> public void CreateTxt(string name,string str) { using(StreamWriter sw=new StreamWriter(@"" + name + ".txt", false, Encoding.UTF8)) { sw.WriteLine(str); sw.Close(); } } /// <summary> /// 往指定的文档写数据 /// </summary> /// <param name="path">存储文档的路径,示例: string path=@"F:\Template\"</param> /// <param name="saveTxtName">保存数据的文件名,示例: string saveTxtName=@"1"</param> public void WriteData(string path,string saveTxtName) { string val = string.Empty; if (dt.Rows.Count > 0) { //读取数据集的前两列 DataTable data = dt.DefaultView.ToTable(false, new string[] { dt.Columns[0].ToString(), dt.Columns[1].ToString() }); for(int i = 0; i < data.Rows.Count; i++) { DataRow row = data.Rows[i]; val += row.ItemArray[0].ToString() + "\t" + row.ItemArray[0].ToString() + "/" + row.ItemArray[1].ToString() + "\r\n"; } try { //判断是否存在文件夹,无则创建 if (!Directory.Exists(path)) { DirectoryInfo info = Directory.CreateDirectory(path); } CreateTxt(path + saveTxtName, val);//根据设定的路径、名称创建txt文档 } catch(Exception) { throw; } } }