日期:2014-05-18  浏览次数:20848 次

如果把DATA SET里的数据写到word文件里??
如果把DATA SET里的数据写到word文件里??

其他的格式一概不要 只将dataset里的数据写到word里就可以了~~

请教哦~~~

------解决方案--------------------
#region 打开Word文档,并且返回对象wDoc,wDoc

/// <summary>

/// 打开Word文档,并且返回对象wDoc,wDoc

/// </summary>

/// <param name="FileName">完整Word文件路径+名称</param>

/// <param name="wDoc">返回的Word.Document wDoc对象</param>

/// <param name="WApp">返回的Word.Application对象</param>

public static void CreateWordDocument(string FileName,ref Word.Document wDoc,ref Word.Application WApp)

{

if(FileName == "") return;

Word.Document thisDocument = null;

Word.FormFields formFields = null;

Word.Application thisApplication = new Word.ApplicationClass();

thisApplication.Visible = true;

thisApplication.Caption = "";

thisApplication.Options.CheckSpellingAsYouType = false;

thisApplication.Options.CheckGrammarAsYouType = false;



Object filename = FileName;

Object ConfirmConversions = false;

Object ReadOnly = true;

Object AddToRecentFiles = false;



Object PasswordDocument = System.Type.Missing;

Object PasswordTemplate = System.Type.Missing;

Object Revert = System.Type.Missing;

Object WritePasswordDocument = System.Type.Missing;

Object WritePasswordTemplate = System.Type.Missing;

Object Format = System.Type.Missing;

Object Encoding = System.Type.Missing;

Object Visible = System.Type.Missing;

Object OpenAndRepair = System.Type.Missing;

Object DocumentDirection = System.Type.Missing;

Object NoEncodingDialog = System.Type.Missing;

Object XMLTransform = System.Type.Missing;



try

{

Word.Document wordDoc =

thisApplication.Documents.Open(ref filename, ref ConfirmConversions,

ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,

ref Revert,ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,

ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,

ref NoEncodingDialog, ref XMLTransform );



thisDocument = wordDoc;

wDoc = wordDoc;

WApp = thisApplication;

formFields = wordDoc.FormFields;

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

 

}

#endregion

------解决方案--------------------
C# code

//也可以通过自己遍历DataSet,来创建一个Html table的方式,

StringBuilder sb = new StringBuilder();
sb.Append(@"<TABLE>");
//添加表头
foreach (System.Data.DataRow row in DataSet.Tables[0].Rows)
{
    sb.Append(@"<TR>");
    foreach (System.Data.DataColumn column in DataSet.Tables[0].Columns)
    {
        sb.Append(@"<TD");
        sb.Append(row[column.ColumnName]);
        sb.Append(@"</TD");
    }
    sb.Append(@"</TR>");
}
sb.Append(@"<TABLE>");

Response.Clear();
Response.Buffer = true;
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.doc", "test"));
Response.ContentType = "application/ms-word";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.Write(sb.ToString());
Response.Flush();
Response.End();