/// <summary>
/// Export.cs的摘要说明。
/// 导出到txt文档。
/// </summary>
public class Export
{
protected static string path()
{
string str3 = Directory.GetCurrentDirectory();
string[] sArr = str3.Split('b');
return sArr[0] + "temptxt\\";
}
private const int TXTRecords = 10;//默认txt文档中记录条数。
private const string TXTPOSTFIX = ".txt";
private const int DATADISTANCE = 5;
private const int TABDISTANCE = 8;
public Export()
{
//删除目标文件夹下的所有txt文件
DeleteFile(path());
}
public static void DeleteFile(string dirRoot)
{
try
{
string[] rootDirs = Directory.GetDirectories(dirRoot); //当前目录的子目录:
string[] rootFiles = Directory.GetFiles(dirRoot); //当前目录下的文件:
foreach (string s2 in rootFiles)
{
File.Delete(s2); //删除文件
}
foreach (string s1 in rootDirs)
{
DeleteFile(s1);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
}
//获取数据源
public DataSet GetData()
{
try
{
DBHelper db = new DBHelper();
//现在到昨天所有数据库中的内容
string strQuery = "select * from Store where SimulationTime between @YesterdayTime and @CurrentTime";
SqlCommand cmd = db.GetSqlStringCommond(strQuery);
string Millisecond = DateTime.Now.Millisecond.ToString();
db.AddInParameter(cmd, "@YesterdayTime", DbType.DateTime, Convert.ToDateTime(DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss") + "." + Millisecond));
db.AddInParameter(cmd, "@CurrentTime", DbType.DateTime, Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")));
return db.ExecuteDataSet(cmd);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
//将数据源导入到多个txt文件中,规定每个txt文件存放记录条数。
public void ExportToTxt(DataSet ds)
{
if (ds.Tables.Count != 0)
{
//统计dataset中当前表的行数
int row = ds.Tables[0].Rows.Count;
//统计dataset中当前表的列数
int column = ds.Tables[0].Columns.Count;
//用于统计当前表中每列记录中字符数最长的字符串的长度之和
int totalLength = 0;
int[] columnLength = new int[column];
int txtCounts = 0;//定义txt文档的数量
if (row % TXTRecords == 0)
{
txtCounts = row / TXTRecords;
}
else
{
txtCounts = row / TXTRecords + 1;
}
for (int x = 1; x < txtCounts+1; x++)
{
string tempFileName = null;
Comon c = new Comon();
tempFileName = c.Ipcid + "_" + DateTime.Now.ToString("yyyyMMddhhmmss")+"_"+x;
FileInfo file = new FileInfo(path() + tempFileName + TXTPOSTFIX);
StreamWriter textFile = null;
try
{
textFile = file.CreateText();
}
catch
{
MessageBox.Show("系统找不到指定目录下的文件: " + path() + tempFileName + TXTPOSTFIX);
return;
}
for (int i = 0; i < column; i++)
{
columnLength[i] = ds.Tables[0].Columns[i].ColumnName.ToString().Length;
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
if (ds.Tables[0].Rows[i][j].ToString().Length > columnLength[j])
{
columnLength[j] = ds.Tables[0].Rows[i][j].ToString().Length;
}
}
}
for (int i = 0; i < column; i++)
{
textFile.Write(ds.Tables[0].Columns[i].ColumnName.ToString());//列名
for (int k = 0; k < columnLength[i] - ds.Tables[0].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文件中
int Count = 0;
if (x * TXTRecords > row)
{
Count = row;
}
else
{
Count = x * TXTRecords;
}
for (int i = (x-1)*TXTRecords; i < Count; i++)
{
for (int j = 0; j < column; j++)
{
textFile.Write(ds.Tables[0].Rows[i][j].ToString());
for (int k = 0; k < columnLength[j] - ds.Tables[0].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();
//关闭当前的StreamWriter流
textFile.Close();
}
}
else
{
System.Windows.Forms.MessageBox.Show("No Data");
}
}
}
本文介绍了一种从数据集导出数据到多个TXT文件的方法,包括如何按记录数量拆分文件、处理文件路径及名称、确保文件内容的正确布局等关键技术细节。
141

被折叠的 条评论
为什么被折叠?



