/// <summary>
/// 创建word
/// <param name="filePath">文件路径 </param>
/// </summary>
protected void CreateWordFile(string filePath)
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
using (FileStream fs = File.Create(filePath))
{
fs.Close();
}
}
    /// <summary>
/// 将页面内容输出到Word
/// <param name="filePath">文件路径 </param>
/// </summary>
protected void WritePageContentToWord(string filePath)
{
string pageHtml = string.Empty;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
this.divAll.RenderControl(htw);
pageHtml = sw.ToString();
pageHtml.Replace("../App_Themes/blue/Image/FiveStar.png", Server.MapPath("./App_Themes/blue/Image/FiveStar.png"));
pageHtml.Replace("../App_Themes/blue/Image/FourStar.png", Server.MapPath("./App_Themes/blue/Image/FourStar.png"));
pageHtml.Replace("../App_Themes/blue/Image/ThreeStar.png", Server.MapPath("./App_Themes/blue/Image/ThreeStar.png"));//这里将图片相对路径换成绝对路径 if (File.Exists(filePath))
{ StreamWriter streamW = File.CreateText(filePath);
streamW.Write(pageHtml);
streamW.Close();
}
sw.Close();
htw.Close();
}
protected void InsertPhoto(string filePath)
{
//生成图片
string imagePath = MapPath("/File2/" + Request.QueryString["ID"].ToString() + ".png");
Session["imagePath"] = imagePath;
//插入图片;
StringBuilder reportContent = new StringBuilder(); object Nothing = System.Reflection.Missing.Value;
object filename = filePath;
Microsoft.Office.Interop.Word.Application WordApp2 = new Microsoft.Office.Interop.Word.Application();//.ApplicationClass();
Microsoft.Office.Interop.Word.Document WordDoc2 = WordApp2.Documents.Open(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); //此处打开的这个word实际为网页的文本格式(可用记事本打开看一下),需另存为doc格式,
//才能把图片嵌入到word文档中,否则保存的永远就是链接
WordDoc2.SaveAs(filePath, WdSaveFormat.wdFormatDocument); //循环Word文档中所有插入的图元,查找显示为空的图元,进行替换
//因该Word文档是由HTML生成而来,存放的图片实际为空,类型为wdInlineShapeLinkedPicture,需替换为实际类型
//IEnumerable<InlineShape> shape2 =new IEnumerable WordDoc2.InlineShapes;
foreach (InlineShape shape in WordDoc2.InlineShapes)
         {
//查找插入图形的位置 object oRange = WordDoc2.Paragraphs[].Range;//获取图片插入的位置
object LinkToFile = false;
object SaveWithDocument = true;
//插入图形
WordDoc2.InlineShapes.AddPicture(imagePath, ref LinkToFile, ref SaveWithDocument, ref oRange);
//删除显示为空的图元
shape.Select();
WordApp2.Selection.Delete();
break;
 }
//打开该文档时默认视图为页面视图方式
WordDoc2.ActiveWindow.View.Type = WdViewType.wdPrintView;
//保存插入图片的Word
WordDoc2.SaveAs(filePath, WdSaveFormat.wdFormatDocument); //关闭所有的Word文档
WordApp2.NormalTemplate.Saved = true;
Object saveChanges2 = Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges;
WordApp2.Documents.Close(saveChanges2, Type.Missing, Type.Missing); //退出Word应用程序
try
       {
WordApp2.Application.Quit(Type.Missing, Type.Missing, Type.Missing);
if (WordApp2 != null)
         {
WordApp2 = null;
}
}
catch { }
finally
       {
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
protected void lnkToWord_Click(object sender, EventArgs e)
{
Chart2.SaveImage(Server.MapPath("~/File2/" + Request.QueryString["ID"].ToString() + ".png"), ChartImageFormat.Png);
Label lbName = fv.FindControl("lbName") as Label;
string wordname = lbName.Text.ToString() + Request.QueryString["ID"];
string filePath = MapPath("~/File2/") + wordname + ".doc"; //文件路径
CreateWordFile(filePath);
WritePageContentToWord(filePath);
InsertPhoto(filePath);
string FileName = wordname + ".doc";
Response.Redirect(string.Format("~/File2/{0}", FileName));
string ImagePath = Session["imagePath"].ToString();
}

具体做法是:先将页面写成html,在写成html时将图片相对路径改成绝对路径,保存成一份word到服务器。此页面包含.Net里面的chart控件,所以先将chart控件变成图片存入服务器,最后将图片插入到word

c# 将页面导出到word(含图片及控件)的更多相关文章

  1. aspx页面导出为word

    aspx页面导出为word代码: System.IO.StringWriter sw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWri ...

  2. (转)WEB页面导出为Word文档后分页&横向打印的方法

    <html>    <HEAD>        <title>WEB页面导出为Word文档后分页&横向打印的方法 </title>    < ...

  3. Android实现图片滚动控件,含页签功能,让你的应用像淘宝一样炫起来

    首先题外话,今天早上起床的时候,手滑一下把我的手机甩了出去,结果陪伴我两年半的摩托罗拉里程碑一代就这么安息了,于是我今天决定怒更一记,纪念我死去的爱机. 如果你是网购达人,你的手机上一定少不了淘宝客户 ...

  4. C# 使用PictureBox实现图片按钮控件

    引言 我们有时候会在程序的文件夹里看见一些图标,而这些图标恰好是作为按钮的背景图片来使用的.鼠标指针在处于不同状态时,有"进入按钮"."按下左键"," ...

  5. UI-UIImageView的图片填充方式(contentMode)_图片作为控件背景图的拉伸方式(stretch)介绍

    常用图片填充方式 这里只介绍三个最常用的图片填充方式 UIViewContentModeScaleToFill模式会导致图片变形.例如: UIViewContentModeScaleAspectFit ...

  6. 我写的一个 Qt 显示图片的控件

    Qt 中没有专门显示图片的控件.通常我们会使用QLabel来显示图片.可是QLabel 显示图片的能力还是有点弱.比方不支持图像的缩放一类的功能.使用起来不是非常方便. 因此我就自己写了个简单的类. ...

  7. 在asp.net页面上按回车会触发Imagebutton控件的Click事件

    原文:在asp.net页面上按回车会触发Imagebutton控件的Click事件 问题: 用asp.net做的aspx页面,无论是否有文本框.下拉框.复选框……获得焦点,只要在当前页面上按一下回车就 ...

  8. Android开发技巧——定制仿微信图片裁剪控件

    拍照--裁剪,或者是选择图片--裁剪,是我们设置头像或上传图片时经常需要的一组操作.上篇讲了Camera的使用,这篇讲一下我对图片裁剪的实现. 背景 下面的需求都来自产品. 裁剪图片要像微信那样,拖动 ...

  9. MFC入门(三)-- MFC图片/文字控件(循环显示文字和图片的小程序)

    惯例附上前几个博客的链接: MFC入门(一)简单配置:http://blog.csdn.net/zmdsjtu/article/details/52311107 MFC入门(二)读取输入字符:http ...

随机推荐

  1. 【原】sql 将某列拼成一个字符串

    SQL Server中,写存储过程,时常会碰到这样一个需求:从某个表中取某一列,然后需要将这一列数据以某种形式拼成一个字符串,以供后面使用,下面这种方法能够实现此需求. --取说明书模块枚举,结果格式 ...

  2. 理解MVC模式

    理解一般意义上的MVC模式 MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为以下三个基本部分: 模型(Model):模型用于封装与应用程序的业务逻 ...

  3. DG_Oracle DataGuard作用和概念(概念)

    2014-06-03 Created By BaoXinjian  

  4. C#使用原生的Directx和OpenGL绘图

    原文 混合语言编程-C#使用原生的Directx和OpenGL绘图的方法 由于项目需要做一些图形展示,所以就想到了使用Directx和OpenGL来绘图,但项目准备使用C#来开发(大家比较熟悉C#), ...

  5. 命令行登录mysql报Segmentation fault错误是怎么回事

    ==========解决方法============在源码包里,编辑文件 cmd-line-utils/libedit/terminal.c把terminal_set方法中的 char buf[TC_ ...

  6. android实现 服务器功能

    package com.weijia.tests; import java.io.IOException; import java.net.InetSocketAddress; import java ...

  7. Build常见错误

    1.ant 解决com.sun.tools.javac.Main is not on the classpath的问题 在ant打包时报错: 解决com.sun.tools.javac.Main is ...

  8. setInterval()和setTimeout()的区别

    一.setInterval()函数的语法结构: setInterval(code,interval) setInterval()函数可周期性执行一段代码,也就是说代码会被周期性不断的执行下去. 函数具 ...

  9. Java同步块

    原文:http://ifeve.com/synchronized-blocks/ Java 同步块(synchronized block)用来标记方法或者代码块是同步的.Java同步块用来避免竞争.本 ...

  10. 清除xcode里面的mobileprovision文件

    清除所有的mobileprovision cd ~/Library/MobileDevice/Provisioning\ Profiles/ 然后删除里面所有的mobileprovision文件 rm ...