Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何读取多个文件并写入单个.csv文件c_C# - Fatal编程技术网

C# 如何读取多个文件并写入单个.csv文件c

C# 如何读取多个文件并写入单个.csv文件c,c#,C#,我的意图是打开两个文件,一个.txt和一个.csv,过滤一些文本,最后将其写入单个.csv文件的一列中。这是我的密码 OpenFileDialog fopen = new OpenFileDialog(); fopen.Multiselect = true; fopen.Filter = "(All type)|*.*"; fopen.ShowDialog(); if (fopen.FileNames != null) { //try //{ Excel.Application app =

我的意图是打开两个文件,一个.txt和一个.csv,过滤一些文本,最后将其写入单个.csv文件的一列中。这是我的密码

OpenFileDialog fopen = new OpenFileDialog();
fopen.Multiselect = true;
fopen.Filter = "(All type)|*.*";
fopen.ShowDialog();
if (fopen.FileNames != null)
{
 //try
 //{
 Excel.Application app = new Excel.Application();
 Excel.Workbook wb = app.Workbooks.Add();
 //Excel.Workbook wb = app.Workbooks.Open(fopen.FileName);
 Excel.Worksheet sheet = wb.Sheets[1];
 Excel.Range range = sheet.UsedRange;
 int row = 1;
 int col = 1;
 foreach (string file in fopen.FileNames)
 {

 textBox1.Text = fopen.FileName;

 string save = fopen.FileName;
 string save1 = save.Split('.')[0];

 string[] text = File.ReadAllLines(file);
 for (int i = 0; i < lines; i++)
 {
    textBox2.AppendText(text[i] + "\n");
    if (text[i].Contains("<LABEL-NAME>"))
     {
        if (text[i + 1].Contains("<MAP-LABEL-NAME>"))
         {
           string split = text[i].Split('<', '>')[2];
           string split1 = text[i + 1].Split('<', '>')[2];
           textBox3.AppendText(split + "\n");
           textBox3.AppendText(split1 + "\n");
           textBox4.Text = (split + ";" + split1);
           string split2 = textBox4.Text;
           range.Cells.set_Item(row, col, split2);
           row++;
           }
           }
            if (text[i].Contains("float32"))
              {

               string split = text[i].Split('f')[1];
               textBox3.AppendText(split + "\n");
               textBox4.Text = split;

               range.Cells.set_Item(row, col, split);
               row++;

                    }
                    textBox5.Text = row.ToString();
                }

                app.DisplayAlerts = false;
                wb.SaveAs(save1 + ".csv", Excel.XlFileFormat.xlCSVWindows);
                wb.Close();       //save as
                app.Workbooks.Close();
                app.Quit();
我试图传递错误,但它还有一个问题,就是只有从第二个文件过滤的数据才会写入.csv文件。 谁能告诉我我做错了什么?
谢谢。

您的片段不完整,但您似乎在foreach循环之外打开Excel,但在结束之前关闭了它,因此如果您打开多个文件,它就不存在了

Excel.Application app = new Excel.Application();
.
.
foreach (string file in fopen.FileNames)
{
    //file stuff

//} foreach loop should be closed here.

            app.DisplayAlerts = false;
            wb.SaveAs(save1 + ".csv", Excel.XlFileFormat.xlCSVWindows);
            wb.Close();       //save as
            app.Workbooks.Close();
            app.Quit();
//} missing foreach closing bracket but implied

片段的其余部分丢失,但您需要在关闭Excel之前关闭foreach循环。

错误在哪里?为什么要使用Excel保存一堆字符串以供以后写入?请将此代码设置为最小可行代码,以便我们可以在我同时打开2个文件时运行错误,错误将停止在range.Cells.set_Itemrow,col,split;。但是当我只打开一个时,没关系。看起来您有并发问题,Excel编写CSV不是最佳选择。IMHO,您是否考虑过将信息存储在内存中,并在循环创建CSV之后?您能给出更具体的信息吗?我是c的新手。非常感谢。
Excel.Application app = new Excel.Application();
.
.
foreach (string file in fopen.FileNames)
{
    //file stuff

//} foreach loop should be closed here.

            app.DisplayAlerts = false;
            wb.SaveAs(save1 + ".csv", Excel.XlFileFormat.xlCSVWindows);
            wb.Close();       //save as
            app.Workbooks.Close();
            app.Quit();
//} missing foreach closing bracket but implied