gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:48:39 25 4
gpt4 key购买 nike

我正在使用 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);
}


}

最佳答案

只是为了弄清楚我认为你在问什么......

你目前为每一行得到这个:

2 vehicle control services ltd brom mal

但是你希望每一行都这样(让我指出这完全没有意义,它看起来不太有用):

2vehicle control services ltdBromMlkit

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

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

用这一行:

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

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

关于c# - 删除导出到文本文件的列之间的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22382393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com