[.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"); } } }}
|
|
|

.: 公告
|
« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
|
.: 我的分类(专题)
|

.: 最新日志
.: 最新回复
|

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

.: 留言板
|

.: 链接
|

|