c# 将页面导出到word(含图片及控件)
/// <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(含图片及控件)的更多相关文章
- aspx页面导出为word
aspx页面导出为word代码: System.IO.StringWriter sw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWri ...
- (转)WEB页面导出为Word文档后分页&横向打印的方法
<html> <HEAD> <title>WEB页面导出为Word文档后分页&横向打印的方法 </title> < ...
- Android实现图片滚动控件,含页签功能,让你的应用像淘宝一样炫起来
首先题外话,今天早上起床的时候,手滑一下把我的手机甩了出去,结果陪伴我两年半的摩托罗拉里程碑一代就这么安息了,于是我今天决定怒更一记,纪念我死去的爱机. 如果你是网购达人,你的手机上一定少不了淘宝客户 ...
- C# 使用PictureBox实现图片按钮控件
引言 我们有时候会在程序的文件夹里看见一些图标,而这些图标恰好是作为按钮的背景图片来使用的.鼠标指针在处于不同状态时,有"进入按钮"."按下左键"," ...
- UI-UIImageView的图片填充方式(contentMode)_图片作为控件背景图的拉伸方式(stretch)介绍
常用图片填充方式 这里只介绍三个最常用的图片填充方式 UIViewContentModeScaleToFill模式会导致图片变形.例如: UIViewContentModeScaleAspectFit ...
- 我写的一个 Qt 显示图片的控件
Qt 中没有专门显示图片的控件.通常我们会使用QLabel来显示图片.可是QLabel 显示图片的能力还是有点弱.比方不支持图像的缩放一类的功能.使用起来不是非常方便. 因此我就自己写了个简单的类. ...
- 在asp.net页面上按回车会触发Imagebutton控件的Click事件
原文:在asp.net页面上按回车会触发Imagebutton控件的Click事件 问题: 用asp.net做的aspx页面,无论是否有文本框.下拉框.复选框……获得焦点,只要在当前页面上按一下回车就 ...
- Android开发技巧——定制仿微信图片裁剪控件
拍照--裁剪,或者是选择图片--裁剪,是我们设置头像或上传图片时经常需要的一组操作.上篇讲了Camera的使用,这篇讲一下我对图片裁剪的实现. 背景 下面的需求都来自产品. 裁剪图片要像微信那样,拖动 ...
- MFC入门(三)-- MFC图片/文字控件(循环显示文字和图片的小程序)
惯例附上前几个博客的链接: MFC入门(一)简单配置:http://blog.csdn.net/zmdsjtu/article/details/52311107 MFC入门(二)读取输入字符:http ...
随机推荐
- 【转】图片IMG标记的alt属性和title属性的使用
alt text 替 换文字(alt text)是为了给那些不能看到你文档中图像的浏览者提供文字说明.这包括那些使用本来就不支持图像显示或者图像显示被关闭的浏览器的用户,视觉障碍的用户和使用屏幕阅读器 ...
- ASP.NET ViewState详解
ASP.NET ViewState详解[转载] 作者:Infinities Loop 概述 ViewState是一个被误解很深的动物了.我希望通过此文章来澄清人们对ViewState的一些错误认识.为 ...
- NeHe OpenGL教程 第二十九课:Blt函数
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- 【java】简单的事件总线EventBus
public class EventBus { private static Map<String, EventListener> eventListeners = new HashMap ...
- 冲突--ListView与ScrollView冲突的4种解决方案
众所周知ListView与ScrollView都具有滚动能力,对于这样的View控件,当ScrollView与ListView相互嵌套会成为一种问题: 问题一:ScrollView与ListView嵌 ...
- Apache2 CGI demo
1. 修改 httpd.conf 配置 <IfModule alias_module> ScriptAlias /cgi-bin/ "/usr/local/apache2/cg ...
- GDI+ 中发生一般性错误。
GDI+ 中发生一般性错误. “/wechat”应用程序中的服务器错误. GDI+ 中发生一般性错误. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及 ...
- JAVA 综合布局应用
//布局综合应用 import java.awt.*; import javax.swing.*; public class Jiemian4 extends JFrame{ JPanel mb1,m ...
- 基本 linux命令
rm rf * 递归删除 ln -s 源 目标 |管道命令 把上一个命令的结果交给 | 的后面的命令处理 文件内容查阅 cat 由第一行开始显示文件内容 tac 从最后一行开始显示 nl 显示的时候输 ...
- linux下shell脚本学习
在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活的工具.Shell不仅仅是命令的收集,而且是一门非常棒的编程语言.您可以通过使用shell使大量的任务自动化,shel ...