手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Asp.Net编程>列表

从数据库导出数据到word、excel、.txt

来源:互联网 作者:west263.com 时间:2008-02-22
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

转自http://martinyang2010.bokee.com/2598999.html
using System;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using Excel;
using Word;
using System.IO;
namespace Common
{
/// <summary>
/// 把数据导入到.doc、.txt、.xls文件中
/// </summary>
public class Export
{
private const string DATAWORDPATH = @"C:\folder\doc\datadoc\";
private const string IMAGEWORDPATH = @"C:\folder\doc\imagedoc\";
private const string IMAGEPATH = @"C:\folder\image\";
private const string EXCELPATH = @"C:\folder\excel\";
private const string TXTPATH = @"C:\folder\txt\";
private const string IMAGEPOSTFIX = ".bmp";
private const string WORDPOSTFIX = ".doc";
private const string EXCELPOSTFIX = ".xls";
private const string TXTPOSTFIX = ".txt";
private const int DATADISTANCE = 5;
private const int TABDISTANCE = 8;

public Export()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

/// <summary>
/// 获得数据集Dataset--------------------------------用于调试
/// </summary>
/// <returns>Dataset</returns>
public DataSet GetData()
{
try
{
string sConnectionString;
sConnectionString = "workstation id=GUOFU;packet size=4096;user id=sa;data source=GUOFU;persist security info=True;initial catalog=YC;password=sc";
SqlConnection objConn = new SqlConnection(sConnectionString);
objConn.Open();
SqlDataAdapter daPoint = new SqlDataAdapter("Select * From Point", objConn);
DataSet dsYC = new DataSet("YC");
daPoint.FillSchema(dsYC,SchemaType.Mapped, "Point");
daPoint.Fill(dsYC,"Point");
daPoint = new SqlDataAdapter("Select * From Employee", objConn);
daPoint.FillSchema(dsYC,SchemaType.Mapped, "Employee");
daPoint.Fill(dsYC,"Employee");
return dsYC;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}

}

/// <summary>
/// 把数据文件导入到.xls文件
/// </summary>
/// <param name="ds"></param>
public void ExportToExcel(DataSet ds)
{

if(ds.Tables.Count!=0)
{
//生成.xls文件完整路径名
string tempFileName = GetTempFileName();
object filename = EXCELPATH tempFileName EXCELPOSTFIX;
object Nothing = System.Reflection.Missing.Value;

//创建excel文件,文件名用系统时间生成精确到毫秒
Excel.Application myExcel = new Excel.ApplicationClass();
myExcel.Application.Workbooks.Add(Nothing);

try
{
//把Dataset中的数据插入excel文件中
int totalCount = 0;
for(int k =0;k<ds.Tables.Count;k )
{
int row = ds.Tables[k].Rows.Count;
int column = ds.Tables[k].Columns.Count;

for(int i = 0;i<column;i )
{
myExcel.Cells[totalCount 2,1 i] = ds.Tables[k].Columns[i].ColumnName;
}

for(int i = 0;i<row;i )
{
for(int j =0;j<column;j )
{
myExcel.Cells[totalCount 3 i,1 j] = "'" ds.Tables[k].Rows[i][j].ToString();
}
}
totalCount = totalCount row 4;
}

try
{
//保存excel文件到指定的目录下,文件名用系统时间生成精确到毫秒
myExcel.ActiveWorkbook._SaveAs(filename,Nothing,Nothing,Nothing,Nothing,Nothing,XlSaveAsAccessMode.xlExclusive,Nothing,Nothing,Nothing,Nothing);
}
catch
{
System.Windows.Forms.MessageBox.Show("系统找不到指定目录下的文件: " EXCELPATH tempFileName EXCELPOSTFIX);
return;
}
//让生成的excel文件可见
myExcel.Visible = true;
}
catch(Exception e)
{
System.Windows.Forms.MessageBox.Show("向excel文件中写入数据出错: " e.Message);
}
}
else
{
System.Windows.Forms.MessageBox.Show("No Data");
}
}


/// <summary>
/// 把数据导入到.doc文件
/// </summary>
/// <param name="ds"></param>
public void ExportToWord(DataSet ds)
{
if(ds.Tables.Count!=0)
{
string tempFileName = null;
object filename = null;

object tableBehavior = Word.WdDefaultTableBehavior.wdWord9TableBehavior;
object autoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitFixed;

object unit = Word.WdUnits.wdStory;
object extend = System.Reflection.Missing.Value;
object breakType = (int)Word.WdBreakType.wdSectionBreakNextPage;

object count = 1;
object character = Word.WdUnits.wdCharacter;

object Nothing = System.Reflection.Missing.Value;

try
{
tempFileName = GetTempFileName();

//生成.doc文件完整路径名
filename = DATAWORDPATH tempFileName WORDPOSTFIX;

//创建一个word文件,文件名用系统时间生成精确到毫秒

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!