如何在csv文件中写入两个重复组。 [英] How to write the two repeating group in csv file.

查看:75
本文介绍了如何在csv文件中写入两个重复组。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,







这里是我得到的文件结构: - < br $>


FileName,Docname,Repeat1,Repeat2(这些是文件的标题)

tronox,tronoxdoc,one,

tronox,tronoxdoc,两个,

tronox,tronoxdoc,三,



预期输出为: -



FileName,Docname,Repeat1,Repeat2(这些是文件的标题)

tronox,tronoxdoc,one,five

tronox ,tronoxdoc,两个,五个

tronox,tronoxdoc,三个,



我没有得到如何实现这个目标。



以下是我的代码: -



Hi Experts,



here is the file structure what I am getting:-

FileName ,Docname, Repeat1, Repeat2(these are the header of the file)
tronox, tronoxdoc, one,
tronox, tronoxdoc, two,
tronox, tronoxdoc, three,

expected output is:-

FileName ,Docname, Repeat1, Repeat2(these are the header of the file)
tronox, tronoxdoc, one, five
tronox, tronoxdoc, two, five
tronox, tronoxdoc, three,

I am not getting how to achieve this.

below is my code:-

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Export();

        }
        public void Export()
        {
            string ExportFileName = @"F:\Mukesh\Tronox\try.csv";


            //check if file exists
            if (!File.Exists(ExportFileName))
            {
                //file does not exist, add the header line
                string header = @"FileName," + "DocumentName," + "Repeat1," + "Repeat2," +

                    Environment.NewLine;
                File.WriteAllText(ExportFileName, header);
            }
            string repgr1 = string.Empty;

            string filename = "tronox";
            string docname = "tronoxdoc";
            string Print = filename + "," + docname+",";
            string[] rep1 = {"one","two","three" };
            string[] rep2 = {"five","five" };
            string row = string.Empty;

            if(rep1.Count()>0)
            {
                for (int i = 0; i < rep1.Count(); i++ )
                {
                    repgr1 = rep1[i];

                    row = Print + repgr1 + "," + Environment.NewLine;
                    File.AppendAllText(ExportFileName, row);
                }
            }

            else
            {
            File.AppendAllText(ExportFileName, Print);
            }
        }
    }
    }











请有人帮助我..

先谢谢






Please anyone help me..
Thanks in Advance

推荐答案

试试这个:

Try this:
string ExportFileName = @"F:\Mukesh\Tronox\try.csv";

//check if file exists
if (!File.Exists(ExportFileName))
{
    //file does not exist, add the header line
    string header = @"FileName," + "DocumentName," + "Repeat1," + "Repeat2," +  Environment.NewLine;
    File.WriteAllText(ExportFileName, header);
}

string filename = "tronox";
string docname = "tronoxdoc";
string Print = filename + "," + docname + ",";
string[] rep1 = { "one", "two", "three" };
string[] rep2 = { "five", "five" };

int repCount = Math.Max(rep1.Length, rep2.Length);
if (repCount > 0)
{
    StringBuilder text = new StringBuilder();

    for (int i = 0; i < repCount; i++)
    {
        text.Append(Print);

        if (i < rep1.Length)
            text.Append(rep1[i]);

        text.Append(",");

        if (i < rep2.Length)
            text.Append(rep2[i]);

        text.AppendLine();
    }

    File.WriteAllText(ExportFileName, text.ToString());
}
else
{
    File.AppendAllText(ExportFileName, Print);
}



简而言之,我正在使用 StringBuilder 收集所有necessery文本而我是如果当前行有任何值,则附加 rep1 rep2 值。


In short I'm using StringBuilder to collect all the necessery text and I'm appending the rep1 and rep2 values if there is any for the current row.


这篇关于如何在csv文件中写入两个重复组。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆