C#實現DataTable轉TXT文件
代碼:
public object DataTableToTXT(DataTable vContent, string vOutputFilePath) { object resObj; StringBuilder sTxtContent; try { if (File.Exists(vOutputFilePath)) File.Delete(vOutputFilePath); sTxtContent = new StringBuilder(); //數據 foreach (DataRow row in vContent.Rows) { for (int i = 0; i < vContent.Columns.Count; i++) { sTxtContent.Append(row[i].ToString().Trim()); sTxtContent.Append(i == vContent.Columns.Count - 1 ? "\r\n" : "\t"); } } File.WriteAllText(vOutputFilePath, sTxtContent.ToString(), Encoding.Unicode); resObj = new object[] { 0, "OK" }; } catch (Exception ex) { resObj = new object[] { 0, "OK" }; } return resObj; }