2014-03-13 49 views
2

我使用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

該代碼不是原因,你的SQL部分工作正常。你用什麼代碼從datagridview導出到文本文件? – musefan

回答

2

只是爲了說清楚什麼,我認爲你是問...

您目前得到這個每行:

2車輛控制服務有限公司維奇發作

但你希望它像這樣的每一行(這讓我指出使得完全沒有任何意義,它看起來並不十分有用):

2vehicle控制服務ltdBromMlkit

這條線

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

:如果是這樣的話,那麼在你的代碼只需更換這行

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

注意,它現在將一個空字符串連接字符串,而不是你不希望單一空間的加盟。

+0

感謝Musefan那樣的魅力...... – smrithi