关于Word文件的导出

导出word文档,首先数据源不用说啦,(没数据说个毛(dan)线(dan))

记下来导出的路径

string tempWordPath=XElement.Load(Server.MapPath(("~/ExcelPath.xml"))).Element("EXPORT").Value+"\\"+"XXX";

其中ExcelPath.xml文件内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<!--注意,请不要更改该xml的结构及位置!-->
<PATH>
  <EXPORT>d:\DOC\ExcelFiles</EXPORT>
  <IMPORT>d:\UPLOAD\ExcelFiles</IMPORT>
</PATH>

如果该文件夹下有旧文件,可以删除(非必须)

if(System.IO.Directory.Exists(TempWordPath))

{

  string[] files=System.IO.Directory.GetFiles(TempWordPath);

  foreach(string file in files)

  {

    if(System.IO.Path.GetExtension(file)==".docx"){System.IO.File.Delete(file);}

  }

}

string tempFileName="word1.docx";

然后就是将上面的一些参数传给下面这个导出方法以供使用

object Nothing=System.Reflection.Missing.Value;

object objWordFullPath=tempWordPath+"\\"+tempFileName;

Microsoft.Office.Interop.Word.ApplicationClass wordApp=new ApplicationClass();

Microsoft.Office.Interop.Word.Document wordDoc=wordApp.Document.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);

object oEndOfDoc = @"\EndOfDoc";
                #region 标题
                Word.Paragraph oPara2;
                object oRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                oPara2 = WordDoc.Content.Paragraphs.Add(ref oRng);
                oPara2.Range.Text = "主标题名";
                oPara2.Range.Font.Name = "黑体";
                oPara2.Range.Font.Size = 14;
                oPara2.Range.Font.Bold = 1;
                oPara2.Format.SpaceAfter = (float)0;
                oPara2.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                oPara2.Range.InsertParagraphAfter();

#region 副标题
                Word.Paragraph oPara3;
                oRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                oPara3 = WordDoc.Content.Paragraphs.Add(ref oRng);
                oPara3.Range.Text = "副标题编号"
                oPara3.Range.Font.Name = "仿宋_GB2312";
                oPara3.Range.Font.Bold = 0;
                oPara3.Range.Font.Size = (float)11;
                oPara3.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                oPara3.Range.InsertParagraphAfter();
                //副标题后最好加间隔,待增加
                #endregion

#region 表格基本设置
                Word.Table table_basic = WordDoc.Tables.Add(wrdRng, 13, 5, ref Nothing, ref Nothing);
                table_basic.Range.Font.Size = (float)11;
                table_basic.Range.Font.Name = "仿宋_GB2312";
                table_basic.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
                table_basic.Rows.Alignment = Word.WdRowAlignment.wdAlignRowCenter;
                table_basic.Columns.DistributeWidth();
                table_basic.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                table_basic.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                table_basic.LeftPadding = 5;
                table_basic.RightPadding = 5;
                table_basic.TopPadding = 5;
                #endregion
                #region 表格宽度、高度
                table_basic.Columns[1].Width = 40;
                table_basic.Columns[2].Width = 100;
                table_basic.Columns[3].Width = 110;
                table_basic.Columns[4].Width = 100;
                table_basic.Columns[5].Width = 110;
                table_basic.Rows[1].Height = 23;
                table_basic.Rows[2].Height = 50;
                table_basic.Rows[3].Height = 23;
                table_basic.Rows[4].Height = 23;
                table_basic.Rows[5].Height = 23;
                table_basic.Rows[6].Height = 23;
                table_basic.Rows[7].Height = 23;
                table_basic.Rows[8].Height = 100;
                table_basic.Rows[9].Height = 200;
                table_basic.Rows[10].Height = 200;
                table_basic.Rows[11].Height = 100;
                table_basic.Rows[12].Height = 100;
                table_basic.Rows[13].Height = 100;
                #endregion
                #region 单元格合并
                for (int i = 1; i <= 8; i++)
                {
                    table_basic.Cell(i, 1).Merge(table_basic.Cell(i, 2));
                }
                table_basic.Cell(1, 2).Merge(table_basic.Cell(1, 4));
                table_basic.Cell(2, 2).Merge(table_basic.Cell(2, 4));
                table_basic.Cell(3, 2).Merge(table_basic.Cell(3, 4));
                table_basic.Cell(6, 2).Merge(table_basic.Cell(6, 4));
                table_basic.Cell(8, 2).Merge(table_basic.Cell(8, 4));
                for (int i = 9; i <= 13; i++)
                {
                    table_basic.Cell(i, 2).Merge(table_basic.Cell(i, 5));
                    table_basic.Cell(i, 1).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                }
                #endregion

#region 赋标题
                table_basic.Cell(1, 1).Range.Text = "xx";
                table_basic.Cell(2, 1).Range.Text = "xx";
                table_basic.Cell(3, 1).Range.Text = "xxx";
                table_basic.Cell(4, 1).Range.Text = "xx";
                table_basic.Cell(5, 1).Range.Text = "xx";
                table_basic.Cell(6, 1).Range.Text = "xx";
                table_basic.Cell(7, 1).Range.Text = "xxx";
                table_basic.Cell(8, 1).Range.Text = "xx";
                table_basic.Cell(4, 3).Range.Text = "xx";
                table_basic.Cell(5, 3).Range.Text = "xx";
                table_basic.Cell(7, 3).Range.Text = "xx";
                table_basic.Cell(8, 1).Range.Text = "xx";
                table_basic.Cell(9, 1).Range.Text = "xx";
                table_basic.Cell(10, 1).Range.Text = "xxx";
                table_basic.Cell(11, 1).Range.Text = "xx";
                table_basic.Cell(12, 1).Range.Text = "xx";
                table_basic.Cell(13, 1).Range.Text = "xx";

#region 表格单元赋值
                table_basic.Cell(1, 2).Range.Text = "";
                table_basic.Cell(2, 2).Range.Text = "";
                table_basic.Cell(3, 2).Range.Text = "";
                table_basic.Cell(4, 2).Range.Text = "";
                table_basic.Cell(5, 2).Range.Text = "";
                table_basic.Cell(6, 2).Range.Text = "";
                table_basic.Cell(7, 2).Range.Text = "";
                table_basic.Cell(8, 2).Range.Text = "";
                table_basic.Cell(9, 2).Range.Text = "";
                table_basic.Cell(10, 2).Range.Text = "";
                table_basic.Cell(11, 2).Range.Text = "";
                table_basic.Cell(12, 2).Range.Text = """;
                table_basic.Cell(13, 2).Range.Text = "";

#region 页面-图片与文字
                WordDoc.Application.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;
                WordDoc.Application.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                WordDoc.Application.Selection.HeaderFooter.Range.Text = "xxx表";
                object left = -10;
                object width = 120;
                object height = 30;
                object top = -20;
                WordDoc.Application.Selection.HeaderFooter.Shapes.AddPicture(”此处为图片途径“, ref Nothing, ref Nothing, ref left, ref top, ref width, ref height, ref Nothing);

#region 页脚-添加页数
                WordDoc.Application.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter;
                WordDoc.Application.Selection.HeaderFooter.Range.Text = "";
                WordDoc.Application.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                WordDoc.Application.Selection.TypeText("第");
                object page = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage;
                WordDoc.Application.Selection.Fields.Add(WordDoc.Application.Selection.Range, ref page, ref Nothing, ref Nothing);
                WordDoc.Application.Selection.TypeText("页/共");
                object pages = Microsoft.Office.Interop.Word.WdFieldType.wdFieldNumPages;
                WordDoc.Application.Selection.Fields.Add(WordDoc.Application.Selection.Range, ref pages, ref Nothing, ref Nothing);
                WordDoc.Application.Selection.TypeText("页");
                #endregion
                string WordPath = System.IO.Path.GetDirectoryName(WordFullPath);
                if (!System.IO.Directory.Exists(WordPath)) { System.IO.Directory.CreateDirectory(WordPath); }
                WordDoc.SaveAs(ref objWordFullPath, 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);
                result = WordFullPath;

 

wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
 wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);

 

 

posted @ 2014-04-03 17:39  2012苦手  阅读(461)  评论(0)    收藏  举报