C#操作Word

     在这里面我用的XmlNode作为数据源的,当然使用能用的DataSet也自然没问题,细节就不写了。生成Word可以这样做。 以下是生成的代码,没花时间整理,直接复制过来的,很多参数没有,生成看下效果,体验一下是可以的。

string FileURL = Server.MapPath("Send\\") + Request.Cookies["Bill_name"].Value + DateTime.Now.ToShortDateString()+".doc";//为将创建的文件设置路径,创建文件(路径+文件名)
                    if (File.Exists(FileURL)) System.IO.File.Delete(FileURL);                                  // 判断文件名是否已存在
                    Object Nothing = System.Reflection.Missing.Value;
                    object filename = FileURL; //文件保存路径
                    //创建Word文档
                    Application WordApp = new ApplicationClass();
                    Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                    WordApp.Selection.ParagraphFormat.LineSpacing = 5f;//设置文档的行间距
 
                    //移动焦点并换行
                    object count = 14;
                    object WdLine = WdUnits.wdLine;//换一行;
                    WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
                    WordApp.Selection.TypeParagraph();//插入段落
 
                    WordDoc.Paragraphs.First.Range.Text = mailBody.Replace(" "," ");
                    XmlDataDocument doc = new XmlDataDocument();
                    doc.LoadXml(xml);
 
                    //文档中创建表格
                    Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range,doc.ChildNodes[0].ChildNodes.Count+1,10, ref Nothing, ref Nothing);
                    //设置表格样式
                    newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleDouble;
                    newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
                    newTable.Columns[1].Width = 60f;
                    newTable.Columns[2].Width = 35f;
                    newTable.Columns[3].Width = 55f;
                    newTable.Columns[4].Width = 55f;
                    newTable.Columns[5].Width = 35f;
                    newTable.Columns[6].Width = 35f;
                    newTable.Columns[7].Width = 55f;
                    newTable.Columns[8].Width = 35f;
                    newTable.Columns[9].Width = 55f;
                    newTable.Columns[10].Width = 35f;
                    try
                    {
                        int rowIndex = 1, columnIndex = 1;
                        XmlNode root = doc.FirstChild.FirstChild;
                        foreach (XmlNode xn in root.ChildNodes)
                        {
                            newTable.Cell(rowIndex, columnIndex).Range.Text = xn.Name;
                            if (columnIndex < 10)
                                columnIndex++;
                        }
                        rowIndex++;
                        foreach (XmlNode xn2 in doc.FirstChild.ChildNodes)
                        {
                            columnIndex = 1;
                            foreach (XmlNode xn in xn2.ChildNodes)
                            {
                                newTable.Cell(rowIndex, columnIndex).Range.Text = xn.InnerText;
                                //newTable.Cell(rowIndex, columnIndex).Select();
                                //WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                                if (columnIndex < 10)
                                    columnIndex++;
                            }
                            if (rowIndex <= doc.ChildNodes[0].ChildNodes.Count)
                                rowIndex++;
                        }
                        //文件保存
                        WordDoc.SaveAs(ref filename, 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);
 
                    }
                    catch
                    {
                        GridCheckList.AddScript(clsSystem.fGetExtMsgAlert("系统提示", "导出失败!"));
                    }
                    finally
                    {
                        WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                        WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                    }
 

         在项目中,有批量生成客户对账单的,但是没有用这种方式,主要是不够稳定。另一种方式,利用现有的水晶报表,导出Word,至少不会报错,节约时间。代码下次写吧,因为批量生成,涉及到要使用多线程,后面再写一篇操作多线程生成word的文章。。

posted @ 2009-06-27 10:37  -飛天蟲  阅读(298)  评论(0编辑  收藏  举报