从数据库中读出的数据怎样能转换成word,求助!!!

解决方案 »

  1.   

    你把数据读取到dataset 或者 datatable 里面
       /// <summary>
        /// dataset数据导入Word
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="FileName"></param>
        /// <param name="Titlename"></param>
        public void DataSetToWord(DataSet ds, string FileName, string Titlename)
        {        
            string ExportFileName = null;
            if (FileName == null || FileName == "")
            {
                ExportFileName = "DFSOFT";
            }
            else
            {
                ExportFileName = FileName;
            }
            if (Titlename == "" || Titlename == null)
            {
                Titlename = "添加标题处";
            }        System.Data.DataTable dt = ds.Tables[0];
            DataRow[] myRow = dt.Select();
            int i = 0;
            int cl = dt.Columns.Count;
            //Web页面定义
            HttpResponse resp;   
            resp=HttpContext.Current.Response;
            resp.Clear();
            resp.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");
            resp.AppendHeader("Content-disposition","attachment;filename="+ExportFileName+".doc");
            resp.ContentType="application/vnd.ms-word"; 
            string BeginTab="<table border='0' cellpadding='0' cellspacing='0'style='border-right:#000000 0.1pt solid;border-top:#000000 0.1pt solid;'>";
            string EndTab="</table>";
            string FileIO=null;
            string MainIO=null;
            string TitleTab = "<tr><td style='font-size:30px;'align='center'><b>" + Titlename + "</b></td></tr><tr><td align='right' style='font-size:15px;'>" + DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString() + "月" + DateTime.Now.Day.ToString() + "日&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>";
            string BeginTr="<tr>";
            string EndTr="</tr>";
               for(i=0;i<cl;i++)
               {
                FileIO+="<td style='border-left:#000000 0.1pt solid; border-bottom:#000000 1.0pt solid; font-size:15px;' align='center'><b>"+dt.Columns[i].Caption.ToString()+"</b></td>";           }
               FileIO=BeginTr.ToString()+FileIO.ToString()+EndTr.ToString();           //逐行处理数据
               foreach(DataRow row in myRow)
               {
                string OutIO=null;
                //当前数据写入
                for(i=0;i<cl;i++)
                {
                 OutIO+="<td style='border-left:#000000 0.1pt solid;border-bottom:#000000 1.0pt solid; font-size:15px;' align='center'>"+row[i].ToString()+"</td>";            }
                MainIO+=BeginTr.ToString()+OutIO.ToString()+EndTr.ToString();    
               }           FileIO="<center><table>"+TitleTab.ToString()+"<tr>"+BeginTab.ToString()+FileIO.ToString()+MainIO.ToString()+EndTab.ToString()+"</tr></table></center>";
               resp.Write(FileIO.ToString());
               resp.End();
            //Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
            //Microsoft.Office.Interop.Word.Document doc;
            //object nothing = System.Reflection.Missing.Value;
            //doc = word.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);
            //doc.Paragraphs.Last.Range.Copy = FileIO;
            //object myfilename = FileName;
            //doc.Save();
            ////doc.SaveAs(ref myfilename, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing);
            //doc.Close(ref nothing, ref nothing, ref nothing);
            //word.Quit(ref nothing, ref nothing, ref nothing);
    你试试这个!