如何在C#中使用文本到列? [英] How to use text to columns in C#?

查看:76
本文介绍了如何在C#中使用文本到列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Friends ...



我开发了以下代码来生成C#中的.csv文件。



Hello Friends...

I have developed following code to generate .csv file in C#.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.IO;

namespace ProcessExcelFile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string _filename = @"E:\Import File\Working\20_04_2015_21_00_57_B14.csv";
            StreamReader ObjStreamReader = new StreamReader(File.OpenRead(_filename));
            string[] line = ObjStreamReader.ReadLine().Split(';');
            string[] machinename = _filename.Split('_');
            for (int i = 2; i < line.Length; i++)
            {
                line[i] = string.Concat(line[i].Trim('"'), "_" ,machinename[machinename.Length - 1].Remove(machinename[machinename.Length - 1].IndexOf(".csv")));
            }
            
            List<System.Collections.Generic.List<string>> ObjListColumns = new List<List<string>>();
            //here 
            for (int i = 1; i < line.Length; i++)
            {
                ObjListColumns.Add(new List<string>());
            }

            string[] values = null;
            while (!ObjStreamReader.EndOfStream)
            {
                values = null;
                values = ObjStreamReader.ReadLine().Split(';');
                for (int i = 0; i < ObjListColumns.Count; i++)
                {
                    ObjListColumns[i].Add(values[i + 1]);
                }
            }
            ObjStreamReader.Close();

            for (int i = 0; i < ObjListColumns[0].Count; i++)
            {
                ObjListColumns[0][i] = ObjListColumns[0][i].Trim('"').Trim('\\').Trim('"');
            }
            for (int i = 0; i < ObjListColumns.Count; i++)
            {
                for (int j = 0; j < ObjListColumns[i].Count; j++)
                {
                    ObjListColumns[i][j] = ObjListColumns[i][j].Trim('"');
                }
            }
            FileStream ObjFileStream = new FileStream(@"E:\Import File\Working\abc1.csv", FileMode.OpenOrCreate);
            StreamWriter ObjWriter = new StreamWriter(ObjFileStream);

            string text=null;
            ObjWriter.WriteLine("[Data]");
            ObjWriter.WriteLine("Tagname;TimeStamp;Value;DataQuality");

            for (int i = 2; i < line.Length; i++)
            {
                
                for (int j = 0; j < ObjListColumns[i-1].Count; j++)
                {

                    text=null;
                    text=line[i].ToString()+";"+ObjListColumns[0][j].ToString()+";"+ObjListColumns[i - 1][j].ToString()+";"+"Good";
                    ObjWriter.WriteLine(text);

                }
            }
            ObjWriter.Close();

            DateTime endtime = DateTime.Now;
            MessageBox.Show("Compeleted in ");

        }
    }
}





正在生成Excel文件但数据在Excel文件中我们输入相同。我想使用C#中的Microsoft Excel中的文本到列选项。请提供一个代码将文本转换为列。



An Excel file is being generated but Data in Excel file is same as we put in. I want to use Text To Columns Option which is there in Microsoft Excel using C#. Please provide me a code to convert text to columns.

推荐答案

根据问题,您的机器运行的国家/地区可能是您的CSV格式。



首先,文本到列是一个Excel指南,与C#无关,从C#代码启动该指南没有多大意义,它是否会立即需要人工交互因此不再是批处理过程。

(参考: http: //www.excel-easy.com/examples/text-to-columns.html [ ^ ])



现在问题可能是发送者和接收者之间的国家列表分隔符是不一样的,例如你使用分号和csv文件使用冒号,除非在几个国家之一,但一个统一的方法来解决这个问题是为了确保你重新使用相同的版本,如果在本地运行,请确保使用System.Globalization.CultureInfo.GetCultureInfo(yourculturename即da-DK)。TextInfo.ListSeparator对分隔符进行硬编码的实例。



我怀疑这是你的问题所在,我花了一些时间来解释那里的csv格式: C#代码只写入excel中的第一列数据 [ ^ ]
Depending on what nationale your machine is running under the issue could be your CSV formatting.

First of all text to columns is an Excel guide and has nothing to do with C#, and launching that guide from C# code doesn't make much sense, is it will immediately require human interaction and as such stops being a batch process.
(ref: http://www.excel-easy.com/examples/text-to-columns.html[^])

Now perhaps the problem is that the nationionale list seperator between sender and reciever is not the same, for instance you're using semi-colon and a csv file uses colon, unless in one of several countries, but a uniform way to address this is to ensure that you're using the same version and if running locally make sure to use System.Globalization.CultureInfo.GetCultureInfo(yourculturename i.e. "da-DK").TextInfo.ListSeparator instad of hardcoding the seperator.

I suspect that's where your problem recides, i've taken some more time to explain csv format there: C# code write only first column data in excel[^]


虽然我不能真正得到你的要求,但似乎你想要读或写一个csv文件。



http: //www.codeproject.com/Articles/415732/Reading-and-Writing-CSV-Files-in-Csharp
Though I can't really get what you are asking, but it seems you want to either read or write a csv file.

http://www.codeproject.com/Articles/415732/Reading-and-Writing-CSV-Files-in-Csharp






我建议你按照下面的链接w有一个解决方案。



http://stackoverflow.com/questions/16732343/converting-excel-file-from-csv-to-xlsx [ ^ ]



http://stackoverflow.com/questions/26648641/c- sharp-convert-csv-to-xls-using-existing-csv-file [ ^ ]



解析CSV:http://epplus.codeplex.com/ [ ^ ]



-V
Hi,

I would suggest you to follow the link below which has a solution.

http://stackoverflow.com/questions/16732343/converting-excel-file-from-csv-to-xlsx[^]

http://stackoverflow.com/questions/26648641/c-sharp-convert-csv-to-xls-using-existing-csv-file[^]

Parsing CSV: http://epplus.codeplex.com/[^]

-V


这篇关于如何在C#中使用文本到列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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