C#将数据库中数据导出到txt文件中
2012-12-06 11:46阅读:
近日做一些用户数据的整合,需要将一些活动的用户数据导出到txt文件中,将页面的代码复制如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using BLL;
public partial class ExportData : System.Web.UI.Page
{
private const string txtPath =
@'D:\data\txt';
private const string txtPostFix =
'.txt';
private const int dataDistance = 5;
private const int tabDistance = 8;
protected void Page_Load(object sender,
EventArgs e)
{
if
(!IsPostBack)
{
if (Request.QueryString['activityId'] !=
null)
{
int
activityId =
Convert.ToInt32(Request.QueryString['activityId'].ToString());
DataSet
dsUsers = UsersManager.GetUsersByActivityID(activityId);
ExportToTxt(dsUsers);
}
}
}
///
/// 把数据文件导入到.txt文件
///
///
public void ExportToTxt(DataSet ds)
{
if
(ds.Tables.Count != 0)
{
string tempFileName = null;
tempFileName = GetTempFileName();
//创建一个.txt文件,文件名用系统时间生成精确到毫秒
FileInfo file = new FileInfo(txtPath +
tempFileName + txtPostFix);
StreamWriter textFile = null;
try
{
textFile =
file.CreateText();
}
catch
{
Response.Write('
return;
}
//把Dataset中的数据写入.txt文件中
for (int totaltable = 0; totaltable <
ds.Tables.Count; totaltable++)
{
//统计dataset中当前表的行数
int row =
ds.Tables[totaltable].Rows.Count;
//统计dataset中当前表的列数
int column
= ds.Tables[totaltable].Columns.Count;
//用于统计当前表中每列记录中字符数最长的字符串的长度之和
int
totalLength = 0;
//用于统计标题的长度(dataset中的表名的length+'表的数据如下'的length)
int
titleLength = 0;
//统计每列记录中字符数最长的字符串的长度
int[]
columnLength = new int[column];
for (int i
= 0; i < column; i++)
{
columnLength[i] =
ds.Tables[totaltable].Columns[i].ColumnName.ToString().Length;
}
for (int i
= 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
if
(ds.Tables[totaltable].Rows[i][j].ToString().Length >
columnLength[j])
{
columnLength[j] =
ds.Tables[totaltable].Rows[i][j].ToString().Length;
}
}
}
//统计当前表中每列记录中字符数最长的字符串的长度之和
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();
}
//关闭当前的StreamWriter流
textFile.Close();
Response.Write('
}
else
{
Response.Write('
}
}
public string GetTempFileName()
{
return
DateTime.Now.ToString('yyyyMMddhhmmssfff');
}
}
参考百度文库:
http://wenku.baidu.com/view/247de6d3360cba1aa811da16.html