当前位置:编程学习 > C#/ASP.NET >>

关于c#操作word文档的问题

我使用c#操作word文档进行打印表格,我希望我打开的word文档打开的页面是横向的而非纵向的,但是我不知道该使用什么属性或者方法才可以,特此请教各位大虾
 private string CreateWordFile()
        {
            string message = "";
            try
            {
                Object Nothing = System.Reflection.Missing.Value;
                Directory.CreateDirectory("D:/单据"); //创建文件所在目录
                string name = this.txtPjoName.Text + this.txtNo.Text + ".doc";
                object filename = "D://单据//" + name; //文件保存路径
                //创建Word文档
                Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

                


                WordApp.Selection.ParagraphFormat.LineSpacing = 10f;//设置文档的行间距
                

                //移动焦点并换行
                object count = 14;
                object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
                WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
                WordApp.Selection.TypeParagraph();//插入段落


                ////创建标题
                object w1 = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
                WordApp.Selection.MoveDown(ref w1, ref count, ref Nothing);//移动焦点
                WordApp.Selection.TypeParagraph();//插入段落
                WordDoc.Paragraphs.First.Range.Text = this.lblBiaoTi.Text+"\n      " + this.lblBianHao.Text + this.txtNo.Text +"                    " + this.lblRiQi.Text + this.dateTimePicker1.Text+"                 ";
                WordDoc.Paragraphs.First.Range.Font.Size = 15;
                WordDoc.Paragraphs.First.Range.Bold = 2;
                WordDoc.Paragraphs.First.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
               


                //文档中创建表格
                Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 7, 10, ref Nothing, ref Nothing);
                //设置表格样式
                newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
                newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
                newTable.Columns[1].Width = 80f;
                newTable.Columns[2].Width = 55f;
                newTable.Columns[3].Width = 140f;
                newTable.Columns[4].Width = 25f;
                newTable.Columns[5].Width = 25f;
                newTable.Columns[6].Width = 25f;
                newTable.Columns[7].Width = 25f;
                newTable.Columns[8].Width = 30f;
                newTable.Columns[9].Width = 30f;
                newTable.Columns[10].Width = 70f;
               



                //填充表格内容

                //第一行
                newTable.Cell(1, 1).Range.Text = this.lblPjoName.Text;
                newTable.Cell(1, 1).Range.Font.Size = 10;
                newTable.Cell(1,2).Merge(newTable.Cell(1,10));
                newTable.Cell(1, 2).Range.Text = this.txtPjoName.Text;

                
                //第二行
                newTable.Cell(2, 1).Range.Text = this.lblDevOrg.Text;
                newTable.Cell(2, 2).Merge(newTable.Cell(2,3));
                newTable.Cell(2, 2).Range.Text = this.txtDevOrg.Text;
                newTable.Cell(2, 3).Merge(newTable.Cell(2, 4));
                newTable.Cell(2, 3).Range.Text = this.lblDevLinkman.Text;
                newTable.Cell(2, 4).Merge(newTable.Cell(2, 5));
                newTable.Cell(2, 4).Range.Text = this.txtDevLinkman.Text;
                newTable.Cell(2, 5).Merge(newTable.Cell(2, 6));
                newTable.Cell(2, 5).Range.Text = this.lblDevLinkmanTel.Text;
                newTable.Cell(2, 6).Range.Text = this.txtDevLinkmanTel.Text;


                ////第三行
                newTable.Cell(3, 1).Range.Text = this.lblWinningUnit.Text;
                newTable.Cell(3, 2).Merge(newTable.Cell(3, 3));
                newTable.Cell(3, 2).Range.Text = this.txtWinningUnit.Text;
                newTable.Cell(3, 3).Merge(newTable.Cell(3, 4));
                newTable.Cell(3, 3).Range.Text = this.lblWinLinkman.Text;
                newTable.Cell(3, 4).Merge(newTable.Cell(3, 5));
                newTable.Cell(3, 4).Range.Text = this.txtWinLinkman.Text;
                newTable.Cell(3, 5).Merge(newTable.Cell(3, 6));
                newTable.Cell(3, 5).Range.Text = this.lblWinLinkmanTel.Text;
                newTable.Cell(3, 6).Range.Text = this.txtWinLinkmanTel.Text;


                //第四行
                newTable.Cell(4, 1).Range.Text = this.lblChargeName.Text;
                newTable.Cell(4, 2).Range.Text = this.lblPrice.Text;
                newTable.Cell(4, 3).Range.Text = "       "+this.lblBiaoZhun.Text;
                newTable.Cell(4, 4).Merge(newTable.Cell(4, 6));
                newTable.Cell(4, 4).Range.Text = this.lblJinE.Text;
                newTable.Cell(4, 5).Merge(newTable.Cell(4, 7));
                newTable.Cell(4, 5).Range.Text = this.lblJianShe.Text;
                newTable.Cell(4, 6).Range.Text = this.lblZhongBiao.Text;
                WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中


                //第五行
                newTable.Cell(5, 1).Range.Text = "\n\n" + this.label1.Text;
                newTable.Cell(5, 2).Range.Text = "\n\n" + this.txtPrice.Text;
                newTable.Cell(5, 3).Range.Text = this.txtBiaozhun.Text;
                newTable.Cell(5, 3).Range.Font.Size = 7;
                newTable.Cell(5, 4).Merge(newTable.Cell(5, 6));
                newTable.Cell(5, 4).Range.Text = "\n\n" + this.label2.Text;
                newTable.Cell(5, 5).Merge(newTable.Cell(5, 7));
                newTable.Cell(5, 5).Range.Text = "\n\n" + this.label3.Text;
                newTable.Cell(5, 6).Range.Text = "\n\n" + this.label4.Text;
                WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
            

                //第六行
                newTable.Cell(6, 1).Range.Text = this.lblJineEDaXie.Text;
                newTable.Cell(6, 2).Merge(newTable.Cell(6, 10));
                newTable.Cell(6, 2).Range.Text = this.label5.Text;

                //第七行
                newTable.Cell(7, 1).Merge(newTable.Cell(7, 10));
                newTable.Cell(7, 1).Range.Text = this.lblZhuShi.Text;

                WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;//水平居左


                //添加页眉
                //WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
                //WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
                //WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]");
                //WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;// 设置右对齐
                //WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置


                WordDoc.Paragraphs.Last.Range.Text = "      " + this.lblYeWu.Text + "                    " + this.lblShenPi.Text + "                  " + this.lblShouKuan.Text + "                 ";

                //文件保存
                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);
                WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                message = name + "文档生成成功,以保存到D:单据下";
                
            }
            catch (Exception e)
            {
                message = e.Message;
            }
            return message;
        } --------------------编程问答-------------------- 在哪本书上看到的,很容易啊。  --------------------编程问答-------------------- 先用宏录下操作 然后把vba的代码翻译下,我一般都这么干 --------------------编程问答-------------------- 跟Excel一样,页面、打印预览等都可以设置的,都是一些属性 --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 已经搞定了  谢谢各位大虾了
补充:.NET技术 ,  .NET Framework
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,