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;
        }

相关文章:

  • 2022-02-14
  • 2021-12-14
  • 2022-12-23
  • 2022-12-23
  • 2021-12-05
  • 2022-01-02
  • 2021-12-29
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-19
  • 2022-12-23
相关资源
相似解决方案