/// <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. BIP_BI Publisher Administrator设定Configuration/Font/Currencies(案例)

    2014-08-08 Created By BaoXinjian

  2. android tween动画效果

    anim文件夹下 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android=&quo ...

  3. [UIView beginAnimations:context:]与[UIView animateWithDuration:animations:]值得注意的一个区别

    原文链接:http://longtimenoc.com/archives/uiview-beginanimationscontext%E4%B8%8Euiview-animatewithduratio ...

  4. C++学习46 getline()函数读入一行字符 一些与输入有关的istream类成员函数

    getline函数的作用是从输入流中读取一行字符,其用法与带3个参数的get函数类似.即    cin.getline(字符数组(或字符指针), 字符个数n, 终止标志字符) [例13.7] 用get ...

  5. gc 日志格式

    对于 Parallel Scavenge + Parallel Old - 新生代:PSYoungGen - 老年代:ParOldGen - 永久代:PSPermGen 后面方括号内部的 " ...

  6. CRM plugin 激活 停用 事件

    需要注册 SetState 和 SetStateDynamecEntity

  7. SQL Server中GO的使用方法(转)

    GO不是标准SQL语句,甚至不是T-SQL语句.它只是SQL Server管理器(SSMS)中用来提交T-SQL语句的一个标志.你可以在SSMS中任意指定这个提交标志.SSMS->工具-> ...

  8. How to get the date N days ago in Python

    from datetime import datetime, timedelta N = 2 date_N_days_ago = datetime.now() - timedelta(days=N) ...

  9. 立体匹配:关于理解middlebury提供的立体匹配代码后的精减

    Middlebury立体匹配源码总结 优化方法 图像可否预处理 代价计算可否采用BT方式 可选代价计算方法 可否代价聚合 可否MinFilter优化原始代价 WTA-Box 可以 可以 AD/SD 可 ...

  10. Regional Changchun Online--Elven Postman(裸排序二叉树)

    Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tot ...