我有一个客户需求,他们需要固定长度格式的数据我需要知道有没有办法从c#导出到固定长度格式的文本文件。这就是我写的,但它仍然与空格混淆。
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 ClaimantAddress3 " +
" , left([Claimant Address7] +' ',29) as ClaimantAddress7 " +
", space(59)" +
" , left(t.t_reference + ' ' ,23 ) as ClaimantReference " +
" , left([Claim Number] +' ',7) " +
" , " + comboBox4.Text + " as CourtCode" +
" ,left(ta_title +' ',29) as Title " +
" ,left(ta_surname +' ',29) as Surname " +
" ,left(ta_othername +' ',29 ) as Othername " +
",left(ta_house_number +' ',29) as Address1 " +
" ,left(ta_address_1 +' ',29) as Address2 " +
" ,left(ta_address_2 + ' ',29) as Address3 " +
",left(ta_address_3 +' ',29) as Address4 " +
//",left(ta_address_4 + ' ',29) as Address5 " +
",left(ta_post_code +' ',7) as Address6 " +
", space(398)," +
"RIGHT('00'+CAST(CAST(bat.PCN_Charge* 100.00 AS INT) AS VARCHAR(5)),8) as CLAIMAMT," +
"RIGHT('0'+CAST(CAST([CFee] * 100 AS INT) AS VARCHAR(5)),5) as CFEE," +
"RIGHT('00000'+CAST(CAST([Solictors Fees] AS INT) AS VARCHAR(5)),5)," +
"RIGHT('000'+CAST(CAST(bat.PCN_Charge + [CFee] AS INT) AS VARCHAR(5)),9) as TotalAMT " +
",left(ISNULL([POC1], '')+' ',44)" +
",left(ISNULL([POC2], '')+' ',44)" +
",left(ISNULL([POC3], '')+' ',44)" +
",Left(ISNULL([t_vrm], '')+' ',7) " +
",left(ISNULL([POC4], '')+' ',44)" +
",left(ISNULL([POC5], '')+' ',44)" +
",left(ISNULL([t_zone_name],'')+' ',14) " +
",left(ISNULL([POC6], '')+' ',44)" +
",left(ISNULL([t_date_time_issued],'')+' ',14) " +
",left(ISNULL([POC7], '')+' ',44)" +
",left(ISNULL([POC8], '')+' ',44)" +
",left(ISNULL([POC9], '')+' ',44)" +
",left(ISNULL([POC10], '')+' ',44)" +
",left(ISNULL([POC11], '')+' ',44)" +
",left(ISNULL([POC12], '')+' ',44)" +
",left(ISNULL([POC13], '')+' ',44)" +
",left(ISNULL([POC14], '')+' ',44)" +
",left(ISNULL([POC15], '')+' ',44)" +
",left(ISNULL([POC16], '')+' ',44)" +
",left(ISNULL([POC17], '')+' ',44)" +
",left(ISNULL([POC18], '')+' ',44)" +
",left(ISNULL([POC19], '')+' ',44)" +
",left(ISNULL([POC20], '')+' ',44)" +
",left(ISNULL([POC21], '')+' ',44)" +
",left(ISNULL([POC22], '')+' ',44)" +
",left(ISNULL([POC23], '')+' ',50)" +
" FROM tickets t " +
" LEFT OUTER JOIN " +
"( " +
" SELECT ticket_addresses.ta_system_ref, ta_title, ta_othername, ta_surname, ta_house_number, ta_address_1, ta_address_2, " +
" ta_address_3, ta_address_4, ta_post_code, ta_telephone, ta_organisation " +
" FROM ticket_addresses " +
" INNER JOIN " +
" ( " +
" SELECT ticket_addresses.ta_system_ref, MAX(ta_address_code) AS ta_address_code " +
" FROM ticket_addresses " +
" GROUP BY ta_system_ref " +
" ) ad " +
" ON (ticket_addresses.ta_system_ref=ad.ta_system_ref AND ticket_addresses.ta_address_code=ad.ta_address_code) " +
")ta " +
"ON (t.t_number=ta.ta_system_ref) " +
" " +
" Inner JOIN " +
" ticket_hold_record b " +
" ON ( t.t_number = b.thr_system_ref) " +
+
" and t.t_reference IN {where} ";
//Generate list of Ticket IDS for SQL Where Clause
string whereClause = "";
string[] tempArray = new string[this.txt.Lines.Length];
tempArray = this.txt.Lines;
if (this.txt.Lines.Length == 0)
{
return;
}
for (int counter = 0; counter <= tempArray.Length - 1; counter++)
{
if (tempArray[counter].Trim().Length > 0)
{
whereClause = whereClause + "'" + tempArray[counter] + "'" + ", ";
}
}
whereClause = whereClause.TrimEnd(' ', ',');
whereClause = "(" + whereClause + ")";
stringSql = stringSql.Replace("{where}", whereClause);
myDataset = new DataSet("SQL");
SqlConnection myConn = new SqlConnection();
SqlCommand myCommand = new SqlCommand();
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = stringSql;
myCommand.Connection = myConn;
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.SelectCommand = myCommand;
myAdapter.Fill(myDataset);
this.dataGridView1.DataSource = myDataset.Tables[0];
for (int counter = 0; counter < myDataset.Tables[0].Columns.Count; counter++)
{
this.dataGridView1.Columns[counter].SortMode = DataGridViewColumnSortMode.NotSortable;
}
this.dataGridView1.Refresh();
myConn.Close();
this.btnGetData.Enabled = true;
this.btnSave.Enabled = true;
Application.DoEvents();
}