以下是把页面中的数据导出word的方法,求页面中的图片怎样导出???public void ExpertControl(System.Web.UI.Control source, DocumentType type)
{
//设置Http的头信息,编码格式
if (type == DocumentType.Excel)
{
//Excel
Response.AppendHeader("Content-Disposition", "attachment;filename=result.xls");
Response.ContentType = "application/ms-excel";
}
else if (type == DocumentType.Word)
{
//Word
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("送货单", System.Text.Encoding.UTF8).ToString() + ".doc");
Response.ContentType = "application/ms-word";
}
Response.Charset = "gb2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
//关闭控件的视图状态
source.Page.EnableViewState = false;
//初始化HtmlWriter
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
source.RenderControl(htmlWriter);
//输出
Response.Write(writer.ToString());
Response.End();
}
//文档类型枚举
public enum DocumentType
{
Word,
Excel
}
{
//设置Http的头信息,编码格式
if (type == DocumentType.Excel)
{
//Excel
Response.AppendHeader("Content-Disposition", "attachment;filename=result.xls");
Response.ContentType = "application/ms-excel";
}
else if (type == DocumentType.Word)
{
//Word
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("送货单", System.Text.Encoding.UTF8).ToString() + ".doc");
Response.ContentType = "application/ms-word";
}
Response.Charset = "gb2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
//关闭控件的视图状态
source.Page.EnableViewState = false;
//初始化HtmlWriter
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
source.RenderControl(htmlWriter);
//输出
Response.Write(writer.ToString());
Response.End();
}
//文档类型枚举
public enum DocumentType
{
Word,
Excel
}
解决方案 »
- ADO.NET entity framework 关联数据添加问题
- 谁能告诉我一个asp程序怎么看
- 请问版主,为什么删我的帖子?????????????
- Response.Redirect后面带查询字符串之后网址出现问题
- 怎样得到刚插入数据库的记录的主键id?
- datalist如何实现多选,并获得选定的行
- XP HOME Edition 下建立IIS的WEB服务器的问题?
- 不知道如何是好呀
- 无法执行web form,请问怎莫办?答对有分挣!
- datagrid中怎样统计各分页数据并在各页页脚显示出来,最后显示出汇总数据?请给出事例
- 一般处理程序上传文件的问题..
- 用户控件中Input控件onserverclick事件的使用
用xml方式保存成WORD,把图片内容读出来变成base64编码放到XML你先熟悉下WORD转出XML结构的样子
//如果此人有照片的话,导入到word
if (ds.Tables[0].Rows[0]["照片"].ToString() !="")
{
//将数据库中的照片保存到本地
byte[] img_bytes = (byte[])ds.Tables[0].Rows[0]["照片"]; //图片字段
string img_path = System.Windows.Forms.Application.StartupPath + "\\img.jpg";
System.IO.FileStream fs = new System.IO.FileStream(img_path, FileMode.CreateNew);
fs.Write(img_bytes, 0, img_bytes.Length);
fs.Flush();
fs.Close();
//添加照片到word
object range = newTable.Cell(2, 5).Range;
object linkToFile = false;
object saveWithDocument = true;
wordDoc.InlineShapes.AddPicture(img_path, ref linkToFile, ref saveWithDocument, ref range); wordDoc.Application.ActiveDocument.InlineShapes[1].Width = 97f;//图片宽度
wordDoc.Application.ActiveDocument.InlineShapes[1].Height = 139f;//图片高度
}
这里的ds是我自己定义的dataset,也就是通过sql查出的数据
if (ds.Tables[0].Rows[0]["照片"].ToString() !="")
{
//将数据库中的照片保存到本地
byte[] img_bytes = (byte[])ds.Tables[0].Rows[0]["照片"]; //图片字段
string img_path = System.Windows.Forms.Application.StartupPath + "\\img.jpg";
System.IO.FileStream fs = new System.IO.FileStream(img_path, FileMode.CreateNew);
fs.Write(img_bytes, 0, img_bytes.Length);
fs.Flush();
fs.Close();
//添加照片到word
object range = newTable.Cell(2, 5).Range;
object linkToFile = false;
object saveWithDocument = true;
wordDoc.InlineShapes.AddPicture(img_path, ref linkToFile, ref saveWithDocument, ref range); wordDoc.Application.ActiveDocument.InlineShapes[1].Width = 97f;//图片宽度
wordDoc.Application.ActiveDocument.InlineShapes[1].Height = 139f;//图片高度
}
下面是关于worDoc的初始化: object Nothing = System.Reflection.Missing.Value;
Word.Application wordApp = new Word.ApplicationClass();
wordApp.Visible = false;
Word.Document wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);