/// <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. ios5 中文键盘高度变高覆盖现有ui问题的解决方案(获取键盘高度的方法)(转载)

    背景: ios5之前,iphone上的键盘的高度是固定为216.0px高的,中文汉字的选择框是悬浮的,所以不少应用都将此高度来标注键盘的高度(包括米聊也是这么做的). 可是在ios5中,键盘布局变了, ...

  2. 黄聪:WordPress图片插件:Auto Highslide修改版(转)

    一直以来很多人都很喜欢我博客使用的图片插件,因为我用的跟原版是有些不同的,效果比原版的要好,他有白色遮罩层,可以直观的知道上下翻图片和幻灯片放映模式.很多人使用原版之后发现我用的更加帅一些,于是很多人 ...

  3. spring学习笔记2(转)

    1.在Java开发领域,spring相对于EJB来说是一种轻量级的,非侵入性的Java开发框架,曾经有两本很畅销的书<Expert one-on-one J2EE Design and Deve ...

  4. [CSS]如何正确使用ID和Class?

    作者:DarkZone链接:https://www.zhihu.com/question/19550864/answer/23440690来源:知乎 以下摘自<精通CSS:高级Web标准解决方案 ...

  5. win764上vs2010+opencv2.4.11安装配置

    1:准备工作 1)opencv的官网下载你所要版本的opencv库文件,运行安装解压到自定义的一个文件夹里(D:\Program Files). 2)安装vs2010. 二:配置 1.计算机环境变量: ...

  6. [物理学与PDEs]第5章习题参考解答

    [物理学与PDEs]第5章习题1 矩阵的极分解 [物理学与PDEs]第5章习题2 Jacobian 的物质导数 [物理学与PDEs]第5章习题3 第二 Piola 应力张量的对称性 [物理学与PDEs ...

  7. silverlight 报 System.NullReferenceException 未将对象引用设置到对象的实例。

    在 Microsoft.Windows.Design.Platform.SilverlightMetadataContext.SilverlightXamlExtensionImplementatio ...

  8. Installing Redis on Ubuntu

    wget http://download.redis.io/redis-stable.tar.gz tar xvzf redis-stable.tar.gz cd redis-stable sudo ...

  9. struts (一)

    1.jar 2.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app id=&quo ...

  10. I2C协议(转)

    1.I2C协议   2条双向串行线,一条数据线SDA,一条时钟线SCL.   SDA传输数据是大端传输,每次传输8bit,即一字节.   支持多主控(multimastering),任何时间点只能有一 ...