C# 操作Word


          Word.Application WordApp = new Word.ApplicationClass();
          // 调用Word打开文档
          string CurDir = @"C:\chy\";

          if (!Directory.Exists(CurDir))
              Directory.CreateDirectory(CurDir);
          string FileName = @"C:\chy\ysContractModelA4.doc";
          //FileStream fs = new FileStream(FileName, FileMode.Create, FileAccess.ReadWrite);
          //fs.Close();
          object fileName = FileName;
          object t1 = "t1", t2 = "t2", t3 = "t3", t4 = "t4", t5 = "t5", t6 = "t6", t7 = "t7", t8 = "t8", t9 = "t9", t10 = "t10", t11 = "t11", t12 = "t12", t13 = "t13", t14 = "t14", t15 = "t15", t16 = "t16", t17 = "t17", t18 = "t18", t19 = "t19", t20 = "t20", t21 = "t21", t22 = "t22", t23 = "t23", t24 = "t24";
          object t25 = "t25", t26 = "t26", t27 = "t27", t28 = "t28", t29 = "t29", t30 = "t30", t31 = "t31", t32 = "t32", t33 = "t33", t34 = "t34", t35 = "t35", t36 = "t36", t37 = "t37", t38 = "t38", t39 = "t39", t40 = "t40", t41 = "t41", t42 = "t42", t43 = "t43", t44 = "t44", t45 = "t45", ts1 = "ts1", ts4 = "ts4";

//t1 = "t1",     表示是 word    中 插入的标签 t1
          object missing = System.Reflection.Missing.Value;
          object readOnly = false;
          object isVisible = true;

          try
          {
              WordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
          }
          catch
          {
              //popInfoBox.windowAlert(this.Page, "打开文档错误!");
              //return;

          }
          WordApp.Selection.GoTo(ref missing, ref missing, ref missing, ref t1); //给标签t1附值
          if (txtsolder.Text.Trim() != "")    //txtsolder.Text.Trimtxtsolder.Text.Trim() 给标签t1附值
              WordApp.Selection.TypeText("cccccccccccchhhhhhhhhyyyyyyyyyy");
          else
              WordApp.Selection.TypeText(" ");
              int totalMoney = 0;
              int totalNum = 0;
              int wordrow=0;
              object bRow = null;   //操作表格
              bRow = WordApp.ActiveDocument.Tables[1].Rows[4];    //在第四行上面增加五行
              for(wordrow=0;wordrow<=4;wordrow++)
              {
                 WordApp.ActiveDocument.Tables[1].Rows.Add(ref bRow);
              }
           
              for (wordrow = 0; wordrow <= 6; wordrow++)
              {
               
                  WordApp.ActiveDocument.Tables[1].Cell(wordrow + 2, 1).Range.Text = wordrow.ToString();
                  WordApp.ActiveDocument.Tables[1].Cell(wordrow + 2, 2).Range.Text = wordrow.ToString();
                  WordApp.ActiveDocument.Tables[1].Cell(wordrow + 2, 3).Range.Text = wordrow.ToString();
                  WordApp.ActiveDocument.Tables[1].Cell(wordrow + 2, 4).Range.Text = wordrow.ToString();
                  WordApp.ActiveDocument.Tables[1].Cell(wordrow + 2, 5).Range.Text = wordrow.ToString();
                  WordApp.ActiveDocument.Tables[1].Cell(wordrow + 2, 6).Range.Text = wordrow.ToString();
                  WordApp.ActiveDocument.Tables[1].Cell(wordrow + 2, 7).Range.Text = wordrow.ToString();
                  WordApp.ActiveDocument.Tables[1].Cell(wordrow + 2, 8).Range.Text = wordrow.ToString();
               
                  totalMoney += 5;
                  totalNum += 10;
              }
              WordApp.ActiveDocument.Tables[1].Cell(wordrow+2, 7).Range.Text = 100+"";
              WordApp.ActiveDocument.Tables[1].Cell(wordrow+3, 1).Range.Text = "全程含税包干总价为:";
              WordApp.ActiveDocument.Tables[1].Cell(wordrow+4, 1).Range.Text = "合计人民币金额(大写):";

          WordApp.Documents.Save(ref missing, ref missing);
          WordApp.Quit(ref missing, ref missing, ref missing);

             

               FileInfo DownWordDoc = new FileInfo(FileName); //打开文档
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer = false;
                Response.ContentType = "application/ms-Word";
                Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownWordDoc.FullName, System.Text.Encoding.UTF8));
                Response.AppendHeader("Content-Length", DownWordDoc.Length.ToString());
                Response.WriteFile(DownWordDoc.FullName);
                Response.Flush();
                Response.End();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值