web模式生成word文件,以及插入图片

项目需要导入Microsoft.Office.Interop.Word.dll

   //图片绝对路径
   string picUrl = "d://mypic.jpeg";
      
   string outStr = "测试文本";
   string spath = Server.MapPath("../") + "/UserFiles/" + "/results.doc"; 
   try
   {
    //路径检测
    string strDirectory = Server.MapPath("../") + "/UserFiles/";
    if(!System.IO.Directory.Exists(strDirectory))
     System.IO.Directory.CreateDirectory(strDirectory);
    using(StreamWriter sw =new StreamWriter(spath, false, System.Text.Encoding.GetEncoding("GB2312")))
    {
     sw.WriteLine(outStr);
     sw.Flush();
     sw.Close();
    }
   }
   catch{}
   finally
   {
            //    sw.Close();
   }
   //word插入图片
   object Nothing=System.Reflection.Missing.Value;
   object filename=Server.MapPath("../") + "/UserFiles/" + "/test.doc";
   object objt=true;
   Microsoft.Office.Interop.Word.Application wordApp=new Microsoft.Office.Interop.Word.ApplicationClass();
   object format=Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
   Microsoft.Office.Interop.Word.Document wordDoc = null;
   try
   {
    // 打开word
    Microsoft.Office.Interop.Word.Application thisApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
    Object ConfirmConversions = false;
    Object ReadOnly = true;
    Object AddToRecentFiles = false;
    Object PasswordDocument = System.Type.Missing;
    Object PasswordTemplate = System.Type.Missing;
    Object Revert = System.Type.Missing;
    Object WritePasswordDocument = System.Type.Missing;
    Object WritePasswordTemplate = System.Type.Missing;
    Object Format = System.Type.Missing;
    Object Encoding = System.Type.Missing;
    Object Visible = System.Type.Missing;
    Object OpenAndRepair = System.Type.Missing;
    Object DocumentDirection =  System.Type.Missing;
    Object NoEncodingDialog = System.Type.Missing;
    Object XMLTransform = System.Type.Missing;

    //Microsoft.Office.Interop.Word.Document wordDoc =
    wordDoc = thisApplication.Documents.Open(ref filename, ref ConfirmConversions,
     ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
     ref Revert,ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
     ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
     ref NoEncodingDialog, ref XMLTransform );

    //插入图片
    Microsoft.Office.Interop.Word.InlineShape oShape= wordDoc.Words.Last.InlineShapes.AddPicture(picUrl,ref Nothing,ref objt,ref Nothing);
    //oShape.WrapFormat.Type =Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
    //将htm文件save as成doc文件
    filename = Server.MapPath("../") + "/UserFiles/" + "/testfile.doc"; 
    //wordDoc.Save();
    //另存为
    wordDoc.SaveAs(ref filename,ref format, 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);

    try
    {
    //读取文件,并写入到客户端响应 
     FileStream fs = new FileStream(filename.ToString(),FileMode.Open,FileAccess.Read);    
 
     byte[] b = new Byte[fs.Length];
     fs.Read(b, 0, b.Length);
     fs.Flush();
     fs.Close();
     File.Delete(filename.ToString());
   
     Response.Clear();
     Response.ClearHeaders();
     Response.Buffer = false;
     Response.ContentType = "application/octet-stream";//ContentType;
     Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("result.doc", System.Text.Encoding.UTF8));
     Response.AppendHeader("Content-Length",b.Length.ToString());
     Response.OutputStream.Write(b, 0, b.Length);
     Response.Flush();
     Response.End();
    }
    catch(Exception em)
    {    
     Response.Write(em.ToString());
    }

   }
   finally
   {
    wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
   } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值