//统计当前表中每列记录中字符数最长的字符串的长度之和 for(int i = 0;i<column;i++) { totalLength = totalLength+columnLength[i]+DATADISTANCE; } totalLength = totalLength+2*TABDISTANCE-DATADISTANCE;
//统计标题的长度(dataset中的当前表名的length+"表的数据如下"的length) titleLength = ds.Tables[totaltable].TableName.ToString().Length+"表的数据如下".Length*2; //把标题写入.txt文件中 for(int i = 0;i<(int)((totalLength-titleLength)/2);i++) { textFile.Write(' '); } textFile.Write(ds.Tables[totaltable].TableName+"表的数据如下"); textFile.WriteLine(); for(int i = 0;i<totalLength;i++) { textFile.Write('*'); } textFile.WriteLine(); textFile.Write("\t"); //把dataset中当前表的字段名写入.txt文件中 for(int i = 0;i<column;i++) { textFile.Write(ds.Tables[totaltable].Columns[i].ColumnName.ToString()); for(int k = 0;k<columnLength[i]-ds.Tables[totaltable].Columns[i].ColumnName.ToString().Length+DATADISTANCE;k++) { textFile.Write(' '); } } textFile.WriteLine(); for(int i = 0;i<totalLength;i++) { textFile.Write('-'); } textFile.WriteLine(); textFile.Write("\t"); //把dataset中当前表的数据写入.txt文件中 for(int i = 0;i<row;i++) { for(int j = 0;j<column;j++) { textFile.Write(ds.Tables[totaltable].Rows[i][j].ToString()); for(int k = 0;k<columnLength[j]-ds.Tables[totaltable].Rows[i][j].ToString().Length+DATADISTANCE;k++) { textFile.Write(' '); } } textFile.WriteLine(); textFile.Write("\t"); } textFile.WriteLine(); for(int i = 0;i<totalLength;i++) { textFile.Write('-'); } textFile.WriteLine(); textFile.WriteLine(); textFile.WriteLine(); } |