C# 创建Word

object Missing = System.Reflection.Missing.Value;
                ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                wordApp.Visible = true;

                _Document doc = wordApp.Documents.Add(ref Missing, ref Missing, ref Missing, ref Missing);
                doc.Activate();

                //设置页面总体格式
                doc.PageSetup.PageHeight = 1040.0F;
                doc.PageSetup.PageWidth = 734.286F;
                doc.PageSetup.TopMargin = 37.142F;
                doc.PageSetup.BottomMargin = 37.142F;
                doc.PageSetup.LeftMargin = 40;
                doc.PageSetup.RightMargin = 40;
                // 打印出标题
                wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
                wordApp.Selection.Font.Bold = (int)WdConstants.wdToggle;
                wordApp.Selection.Font.Size = 18;
                wordApp.Selection.Font.Name = "宋体";
                wordApp.Selection.TypeText("XXXXXX");
                wordApp.Selection.TypeParagraph();
                //打印出(A)卷     08/09学年    第二学期       班级:         姓名:           学号:     
                wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
                wordApp.Selection.Font.Bold = 0;
                wordApp.Selection.Font.Size = 12;
                wordApp.Selection.TypeText("(A)卷     08/09学年    第二学期       班级:         姓名:           学号:     ");
                wordApp.Selection.TypeParagraph();
                //打印出…………………………………装……………………………订…………………………线……………………………………………
                wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
                wordApp.Selection.Font.Bold = 0;
                wordApp.Selection.Font.Size = 12;
                wordApp.Selection.TypeText("…………………………………装……………………………订…………………………线……………………………………………");
                wordApp.Selection.TypeParagraph();

                //插入表格
                Range range = doc.Content;
                range.Select();
                object lastPositioin = doc.Characters.Count - 1;
                range = doc.Range(ref lastPositioin, ref lastPositioin);
                doc.Tables.Add(range, 1, 8, ref Missing, ref Missing);
                //向表格中添加内容
                Cell cell = doc.Tables[1].Cell(1, 1);
                cell.Range.Text = "适用专业:";
                cell = doc.Tables[1].Cell(1, 3);
                cell.Range.Text = "考核性质:";
                cell = doc.Tables[1].Cell(1, 5);
                cell.Range.Text = "命题教师";
                cell = doc.Tables[1].Cell(1, 7);
                cell.Range.Text = "考试时间";
                //再添加一个表格
                lastPositioin = doc.Characters.Count - 1;
                range = doc.Range(ref lastPositioin, ref lastPositioin);
                doc.Tables.Add(range, 5, 10, ref Missing, ref Missing);
                //在表格中添加内容
                cell = doc.Tables[1].Cell(2, 1);
                cell.Range.Text = "题号";
                cell = doc.Tables[1].Cell(3, 1);
                cell.Range.Text = "得分";
                cell = doc.Tables[1].Cell(4, 1);
                cell.Range.Text = "评阅人";
                cell = doc.Tables[1].Cell(5, 1);
                cell.Range.Text = "复核人";
                //保存和退出
                object path = @"D:/database/123.doc";
                doc.SaveAs(ref path, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing);
                doc.Close(ref Missing, ref Missing, ref Missing);
                wordApp.Quit(ref Missing, ref Missing, ref Missing);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值