using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// DataBaseCommand 的摘要说明
/// </summary>
public class DataBaseCommand
{
 public DataBaseCommand()
 {
  //
  // TODO: 在此处添加构造函数逻辑
  //
 }
    /// <summary>
    /// 将DataSet中的数据导入到Excel中
    /// </summary>
    /// <param name="xlsPath">Excel路径</param>
    /// <param name="txtPath">交接文本路径</param>
    /// <param name="ds">DataSet数据集</param>
    public void PumpToExcel(string xlsPath, string txtPath, DataSet ds)//导Excel
    {

        DataRow[] dr = ds.Tables[0].Select("1=1");
        DataTable dt = ds.Tables[0];
        StreamWriter sw = new StreamWriter(txtPath, false, System.Text.Encoding.Default);//文本流
        string tmp = "";
        foreach (DataColumn dc in dt.Columns)
        {
            tmp += dc.Caption + "\t";
        }
        sw.WriteLine(tmp + "\a");//将DataSet中的头名写进文本流中,用制表符隔开
        int itemCount = dt.Rows.Count;
        int columnCount = dt.Columns.Count;
        for (int j = 2; j < 2 + itemCount; j++)//将DataSet中的每行写进文本流中,用制表符隔开
        {
            tmp = "";
            for (int k = 1; k < columnCount + 1; k++)
            {

                tmp += dr[j - 2][k - 1].ToString() + "\t";
            }
            sw.WriteLine(tmp + "\a");

        }
        sw.Flush();

        File.Copy(txtPath, xlsPath, true);//将Txt保存为Excel
        sw.Close();
    }
}

posted on 2006-04-25 16:28  独蠢无双  阅读(502)  评论(0编辑  收藏  举报