zoukankan      html  css  js  c++  java
  • 导出(winform) word

     1 string FileURL = Server.MapPath("Send\\") + Request.Cookies["Bill_name"].Value + DateTime.Now.ToShortDateString()+".doc";//为将创建的文件设置路径,创建文件(路径+文件名)
    2   if (File.Exists(FileURL)) System.IO.File.Delete(FileURL); // 判断文件名是否已存在
    3 Object Nothing = System.Reflection.Missing.Value;
    4 object filename = FileURL; //文件保存路径
    5 //创建Word文档
    6 Application WordApp = new ApplicationClass();
    7 Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
    8 WordApp.Selection.ParagraphFormat.LineSpacing = 5f;//设置文档的行间距
    9
    10 //移动焦点并换行
    11 object count = 14;
    12 object WdLine = WdUnits.wdLine;//换一行;
    13 WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
    14 WordApp.Selection.TypeParagraph();//插入段落
    15
    16 WordDoc.Paragraphs.First.Range.Text = mailBody.Replace(" "," ");
    17 XmlDataDocument doc = new XmlDataDocument();
    18 doc.LoadXml(xml);
    19
    20 //文档中创建表格
    21 Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range,doc.ChildNodes[0].ChildNodes.Count+1,10, ref Nothing, ref Nothing);
    22 //设置表格样式
    23 newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleDouble;
    24 newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
    25 newTable.Columns[1].Width = 60f;
    26 newTable.Columns[2].Width = 35f;
    27 newTable.Columns[3].Width = 55f;
    28 newTable.Columns[4].Width = 55f;
    29 newTable.Columns[5].Width = 35f;
    30 newTable.Columns[6].Width = 35f;
    31 newTable.Columns[7].Width = 55f;
    32 newTable.Columns[8].Width = 35f;
    33 newTable.Columns[9].Width = 55f;
    34 newTable.Columns[10].Width = 35f;
    35 try
    36 {
    37 int rowIndex = 1, columnIndex = 1;
    38 XmlNode root = doc.FirstChild.FirstChild;
    39 foreach (XmlNode xn in root.ChildNodes)
    40 {
    41 newTable.Cell(rowIndex, columnIndex).Range.Text = xn.Name;
    42 if (columnIndex < 10)
    43 columnIndex++;
    44 }
    45 rowIndex++;
    46 foreach (XmlNode xn2 in doc.FirstChild.ChildNodes)
    47 {
    48 columnIndex = 1;
    49 foreach (XmlNode xn in xn2.ChildNodes)
    50 {
    51 newTable.Cell(rowIndex, columnIndex).Range.Text = xn.InnerText;
    52 //newTable.Cell(rowIndex, columnIndex).Select();
    53 //WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
    54 if (columnIndex < 10)
    55 columnIndex++;
    56 }
    57 if (rowIndex <= doc.ChildNodes[0].ChildNodes.Count)
    58 rowIndex++;
    59 }
    60 //文件保存
    61 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);
    62
    63 }
    64 catch
    65 {
    66 GridCheckList.AddScript(clsSystem.fGetExtMsgAlert("系统提示", "导出失败!"));
    67 }
    68 finally
    69 {
    70 WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
    71 WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
    72 }

  • 相关阅读:
    Java学习日记之框架SSM
    Java学习日记之Maven
    Java学习日记之Redis
    文件夹浏览控件(.NET)
    扩展ComboBox(.Net)
    公交车路线查询系统后台数据库设计查询算法
    一个用于动态生成静态页面的类——TextTemplate
    远程控制程序
    公交车路线查询系统后台数据库设计关联地名和站点
    Ext上传文件
  • 原文地址:https://www.cnblogs.com/lingyuan/p/1943440.html
Copyright © 2011-2022 走看看