C#实现DataTable转TXT文件

实现DataTable转TXT文件代码如下:
 public ExecutionResult DataTableToTxt(DataTable vContent, string vOutputFilePath)
        {
            ExecutionResult sResult = new ExecutionResult();
            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);
                sResult.Status = true;
            }
            catch (Exception ex)
            {
                sResult.Message = ex.Message;
                sResult.Status = false;
            }
            return sResult;
        }

C#实现DataTable转TXT文件

上一篇:分享一个 vue3 composition-api 的中文翻译


下一篇:C#实现EXCEL表格转DataTable