欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现DataTable转TXT文件

程序员文章站 2022-06-22 11:08:06
实现DataTable转TXT文件代码如下: public ExecutionResult DataTableToTxt(DataTable vContent, string vOutputFilePath) { ExecutionResult sResult = new ExecutionResu ......
实现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;
        }