本站首页    管理页面    写新日志    退出

The Neurotic Fishbowl

[.NET-Winform]数据库数据导出到txt文本(通过测试)
麦林 发表于 2007/8/31 15:06:22

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using Word;using System.IO;using System.Data.SqlClient; namespace word{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private const int DATADISTANCE = 5;        private const int TABDISTANCE = 8;        private void button1_Click(object sender, EventArgs e)        {            Form1 one = new Form1();            DataSet ds = one.getData();            ExportToWord(ds);        }         public DataSet getData()        {            try            {                string connString = "server = .; database = Northwind;uid = sa ; pwd = 123";                SqlConnection con = new SqlConnection(connString);                con.Open();                SqlDataAdapter da = new SqlDataAdapter("select * from Orders", con);                DataSet ds = new DataSet();                da.Fill(ds);                return ds;            }            catch (Exception ex)            {                throw new Exception(ex.Message);            }        }         public void ExportToWord(DataSet ds)        {            if (ds.Tables.Count != 0)            {                string tempFileName = null;                tempFileName = DateTime.Now.ToString("yyyyMMddhhmmssfff");                 //创建一个.txt文件,文件名用系统时间生成精确到毫秒                FileInfo file = new FileInfo(tempFileName + ".txt");                StreamWriter textFile = null;                try                {                    textFile = file.CreateText();                }                catch                {                    System.Windows.Forms.MessageBox.Show("系统找不到指定目录下的文件");                    return;                }                 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();                System.Windows.Forms.MessageBox.Show("数据文件已保存到" + "   " + file.FullName);            }            else            {                System.Windows.Forms.MessageBox.Show("No Data");            }        }    }}

阅读全文(2322) | 回复(0) | 编辑 | 精华

 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)
 

The Neurotic Fishbowl

.: 公告

fighting for the work!


Bloginess

«September 2025»
123456
78910111213
14151617181920
21222324252627
282930

.: 我的分类(专题)

首页(449)
文字感悟(103)
学习公共库(61)
生活体会(100)
资源收集(14)
.NET-Winform(27)
音乐文字(15)
心情小记(119)
SQL问题解决(10)


In the Bowl

.: 最新日志

C#打印代码
你看到的我是蓝色的
即将逝去的3月
这个假期
两个月过去
表名存在其他表时获取数据
条码打印
Remoting的用法
INI文件用法


.: 最新回复

回复:条码打印
回复:条码打印
回复:即将逝去的3月
回复:cookie对象使用
回复:条码打印
回复:JSP学习(字符串比较)
回复:应收应付核销规则及常见问题(续)
回复:两个月过去
回复:两个月过去


The Fishkeeper
blog名称:栗色?蓝色?
日志总数:449
评论数量:201
留言数量:37
访问次数:2259618
建立时间:2006年5月16日



Text Me

.: 留言板

签写新留言

链接完成
回:loseVC
来喽~
转转......
新年啦
对自己说
祝福
回:佑手
WO


Other Fish in the Sea

.: 链接


loseVC's BLOG
Collapsar_feel
布衣加针
C#and.Net
Kinogam Web




站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.266 second(s), page refreshed 144794777 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号