删除列之间的空格导出到文本文件

时间:2014-03-13 14:51:42

标签: c#

我正在使用c#windows应用程序从数据库中获取数据并在datagridview上显示并导出到文本文件 我想删除4列以下的空格。

2  
vehicle control services ltd 
Brom
Malkit

我得到2 vehicle control services ltd brom mal,但我希望它像2vehicle control services ltdBromMlkit

这是我的代码。

string stringSql = " SELECT distinct  " +
                "'" + comboBox6.Text + "' as RecordType" +
               " , left([Claimant Name] +'                              ',30) " +
                " , left([Claimant Address1] +'                             ',30)  " +
                " , left([Claimant Address2] +'                             ',30) as ClaimantAddress2 " +
                " , left([Claimant Address3] +'                             ',30) as

导出到文本文件代码

 if (obj == null || obj == Convert.DBNull)
                return "";

            // if string has no ','
            if (obj.ToString().IndexOf(",") == -1)
                return obj.ToString();

            // remove backslahes
            return "\"" + obj.ToString() + "\"";
        }

        private void ExportDatatviewToCsv(string iFilename, DataView dv)
        {
            // Open output stream
            StreamWriter swFile = new StreamWriter(iFilename);


            // Rows of Data
            foreach (DataRowView rowData in dv)
            {
                string[] colData = new string[dv.Table.Columns.Count];
                for (int i = 0; i < dv.Table.Columns.Count; i++)
                {
                    object obj = rowData[i];
                    colData[i] = GetWriteableValueForCsv(obj);
                }

                // Write data in row
                swFile.WriteLine(string.Join(" ", colData));
            }

            // Close output stream
            swFile.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (myDataset == null)
            {
                return;
            }

            if (myDataset.Tables[0].Rows.Count == 0)
            {
                return;
            }
            DataView vwExport = new DataView(myDataset.Tables[0]);
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "TXT file|*.txt";
            sfd.FileName = "ee "  + ".txt";

            if (sfd.ShowDialog() == DialogResult.OK)
            {

                if (sfd.FileName != "")
                {
                    ExportDatatviewToCsv(sfd.FileName, vwExport);
                    MessageBox.Show("File has been saved as: " + Environment.NewLine + sfd.FileName + Environment.NewLine + Environment.NewLine + "NB: This dataset has been ordered by t_reference in ascending order. If being combined with an existing dataset - that dataset will also need to be sorted in this way.", "Operation complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }


  }

1 个答案:

答案 0 :(得分:2)

只是为了说明我认为你在问什么......

您目前为每一行获得此内容:

  

2车辆控制服务有限公司

但你想要每条线都这样(这让我指出完全没有意义,它看起来不太可用):

  

2车辆控制服务有限公司BrromMlkit

如果是这种情况,那么在您的代码中只需替换此行:

swFile.WriteLine(string.Join(" ", colData));

这一行:

swFile.WriteLine(string.Join("", colData));

请注意,它现在将使用空字符串连接字符串,而不是加入您不想要的单个空格。