/// <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. 初步理解MVC

    一.Asp.net WebForms 与Asp.net MVC 概念 Asp.net是创建WEB应用的框架,MVC是能够用更好的方法来组织并管理代码的一种更高级架构体系. 我们可将原来的Asp.net ...

  2. Android Gradle实用技巧——多渠道打包

    友盟有很多不错的功能,例如渠道统计等. 想要做渠道统计,有一个要求就是要在manifest文件中添加各个渠道的配置.只有一两个渠道还好说,但是渠道多了的话,手动修改然后打包简直是噩梦. 幸好现在And ...

  3. PLSQL_查询SQL的执行次数和频率(案例)

    2014-12-25 Created By BaoXinjian

  4. poj 3048 Max Factor(素数筛)

    这题就是先写个素数筛,存到prime里,之后遍历就好,取余,看是否等于0,如果等于0就更新,感觉自己说的不明白,引用下别人的话吧: 素数打表,找出20000之前的所有素数,存入prime数组,对于每个 ...

  5. 非spring组件servlet、filter、interceptor中注入spring bean

    问题:在filter和interceptor中经常需要调用Spring的bean,filter也是配置在web.xml中的,请问一下这样调用的话,filter中调用Spring的某个bean,这个be ...

  6. 配置jetty 远程调试

    该调试不支持hessian 接口调用 1.配置远程jetty 服务器的 bin/jetty.sh JAVA_OPTIONS+=("-Xdebug -Xrunjdwp:server=y,tra ...

  7. Mac下搭建cocos2d 和cocos2d -x 环境

    来源:http://blog.csdn.net/duxinfeng2010/article/details/7985024 cocos2d是一个开源框架,用于构建2D游戏.演示程序和其他图形界面交互应 ...

  8. IDEA激活服務器

    IDEA: http://www.iteblog.com/idea/key.php webstorm11:http://15.idea.lanyus.com/

  9. [ActionScript] AS3利用SWFObject与JS通信

    首先介绍SWFObject的用法: swfobject.embedSWF(swfUrl, id, width, height, version, expressInstallSwfurl, flash ...

  10. arcgis 栅格计算器(Spatial Analyst/Raster Calculator)

    栅格计算器中用得到$$相关函数 $$NROWS: the number of rows in the analysis window (行数)$$NCOLS: the number of column ...