我有datagridview
有一些记录。我必须以.txt文件格式导出此记录。当它导出到.txt文件时,记录的格式不正确,如图所示
我已经编写了一些导出到.txt文件的代码
string FileName = CurrentUserContext.CurrentEnterprise.FSaveReportTo+"\\" + fExpFile.strFileName + ".txt";
bool IncludeColHeaders = fExpFile.includeHeaders;
StringBuilder sb = new StringBuilder();
if (Company != null)
{
sb.AppendLine("Company: " + Company.FName);
}
sb.AppendLine("Bank: " + Bank.FBankName + "\t" + Bank.FBankNo);
sb.AppendLine("Account: " + Bank.FAccountName + "\t" + Bank.FAccountNo);
sb.AppendLine("\n");
string strColumnHeaders = null;
int ColumnCount = this.dgvCheckdetails.ColumnCount;
if (IncludeColHeaders)
{
for (int j = 0; j <= ColumnCount - 2; j++)
{
if (j == 3 || j == 5)
{
strColumnHeaders += "\t\t";
}
if (j != 4)
{
strColumnHeaders += dgvCheckdetails.Columns[j].HeaderText + "\t";
}
}
sb.AppendLine(strColumnHeaders);
}
int rowcount = dgvCheckdetails.Rows.Count;
string strRow;
for (int i = 0; i <= rowcount - 1; i++)
{
strRow = null;
for (int j = 0; j <= ColumnCount - 2; j++)
{
if (j != 4)
strRow += Convert.ToString(dgvCheckdetails.Rows[i].Cells[j].Value) + "\t";
}
sb.AppendLine(strRow);
}
File.WriteAllText(FileName, sb.ToString());
MessageBox.Show(CultureManager.GetMessageString("Export_Data_Successfully_Exported"));
如何对齐从datagridview
?
答案 0 :(得分:1)
问题在于您的代码使用“\ t”来编写行以尝试对齐列。执行此操作的正确方法是使用填充代替选项卡。选项卡将导致事物根据数据长度发生意外变化,而填充将考虑到这一点并相应地进行缩放。
查看这篇类似的SO文章,解释一些关于填充的内容:
Pad left or right with string.format
或此MSDN链接: