行转换管分隔的文本文件为.csv的Excel文件时搞砸 [英] Rows messed up when converting pipe delimited text files to .CSV excel file

查看:121
本文介绍了行转换管分隔的文本文件为.csv的Excel文件时搞砸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I code此功能,所有管道转换成逗号,之后,将其转换到Excel .CSV文件。

I code this function which converts all pipes into commas, and after that converting it into an excel .CSV file.

不过,在那之后我意识到有一些问题,一些行。

However, after that I realize that there are some problems with some rows.

例如。名称[蔡卫龙(据说是在一列),结束了蔡威是在一列中,龙是在下一列。

E.g. Name [Chua Wei Loon] (supposedly to be in one column), ended up "Chua Wei" is in one column, "Loon" is in the next column.

我期待通过文本文件并找到名称之间没有管,我无法找到一个解决的办法。

I have look through the text file and find no pipes in between the name, and I couldn't find a solution to it.

下面是我的code的功能:

Below is my code for the function:

protected void SaveAsExcelBtn_Click(object sender, EventArgs e)
    {


        //string strExcelOutputFilename = "C:/Documents and Settings/rhlim/My Documents/" + DateTime.Now.ToString("yyyyMMddHHmmss") + xlExtension;

        // Before attempting to import the file, verify 
        // that the FileUpload control contains a file. 
        if (TextFile.HasFile)
        {
            // Get the name of the Excel spreadsheet. 
            string strFileName = Server.HtmlEncode(TextFile.FileName);

            // Get the extension of the text. 
            string strExtension = Path.GetExtension(strFileName);

            // Validate the file extension. 
            if (strExtension != ".TXT" && strExtension!=".txt")
            {

                Response.Write("<script>alert('Invalid text file!');</script>");
                return;
            }

            // Generate the file name to save the text file.
            //string strUploadFileName = "C:/Documents and Settings/rhlim/My Documents/Visual Studio 2005/WebSites/SoD/UploadFiles/" + DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension;

            if (DEMUserRoleRB.Checked)
            {
                string strExcelOutputFilename = "C:/" + "userrolelist" + xlExtension;


                using (StreamWriter outputWriter = new StreamWriter(File.Create(strExcelOutputFilename)))
                {
                    StreamReader inputReader = new StreamReader(TextFile.FileContent);
                    string fileContent = inputReader.ReadToEnd();
                    fileContent = fileContent.Replace('|', ',');
                    outputWriter.Write(fileContent);
                    //TextFile.SaveAs(strExcelOutputFilename);
                    inputReader.Close();
                    UploadStatusLabel.Text = "Conversion successful. File is stored at directory C:/";
                }

                //string strExcelOutputFilename = "C:/Documents and Settings/rhlim/My Documents/" + DateTime.Now.ToString("yyyyMMddHHmmss")+xlExtension;
                // Save the Excel spreadsheet on server. 
                //TextFile.SaveAs (strExcelOutputFilename);

            }
        }
        else Response.Write("<script>alert('Please select a file');</script>");

.csv文件的示例输出(有错误行高亮显示):

A sample output of the .csv file (rows with errors are highlighted):

我已经意识到我的错误,这是由于其内部造成的名字拆分变量commans。

I have realized my mistake, it is due to having commans inside variables which caused the name to split.

是否有任何建议,使我仍然可以将其转换为.csv文件,尽管变量之间具有commans?

Are there any suggestions so that I could still convert them to .csv files despite having commans in between variables?

推荐答案

你检查[蔡卫龙]究竟是不是真正[蔡卫,龙]?你得到的数据去掉逗号第一?

did you check whether [Chua Wei Loon] is not actually [Chua Wei, Loon] ? Did you get rid of the commas in the data first?

这篇关于行转换管分隔的文本文件为.csv的Excel文件时搞砸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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