■请问■ 如何将图片输出到word中? SQL Server / C# / WORD如何将数据库中的图片型字段的图片,输出到word文件中?搞定就给分! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://community.csdn.net/Expert/topic/3520/3520416.xml?temp=.988003前车之鉴! 我不知道你的WORD文件是怎样的。我用WORD2003做了个试验,生成简化XML文件如下:<?xml version="1.0" encoding="UTF-8" standalone="yes"?><?mso-application progid="Word.Document"?><w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no" xml:space="preserve"><w:body><w:pict><w:binData w:name="wordml://01000001.gif">R0lGODlh9AH0AfcAAP//////AP8A//8AAAD//wD/AAAA/wAAAGZmZv/ix/+8oW05FlQjAhQcrwgkmwYTiZBOUv+XnUYgJP+Hk/7Kz3I4P/9yiP+vu2IMHKQWLnUSJLgdOv/09oQJIUsPGzcLFP9WecaXoP/v8v/3+f/7/P/8/dHO0KqIpPv5/GVZjnx2mFVQqEhFjQAAAouMphEaqREaphMbrQ8Vgw4TeAgLRjk8agcPaQ0YnA8ZohAYjhcisQUReAcUiwYSgAkVjwoWlS4yWgABCAIINwIEEhottgIDCyU3uEVKbGdrigQQWCk+vTdS6DVO2zlT6jJJzR0qdSQyjQQNPQIHI....(省略,此处为图片编码,你应该可以查到是用什么方式生成的。)</w:binData><v:shape id="_x0000_i1025" type="#_x0000_t75" style="width:375pt;height:375pt"><v:imagedata src="wordml://01000001.gif" o:title="3_oJYvYzrGpaeK"/></v:shape></w:pict></w:body></w:wordDocument>你试试看直接输出图片,然后生成这样一个文档。Happy program! to 大象:我的意思是:将数据库中的图片,输出到一个已经存在的word文件中(如果没有,即时新建一个word文档也可),你提出的解决办法我不是很了解。 object o = Type.Missing;WordTab.Cell(3,3).Range.Text = "";WordTab.Cell(3,3).Range.InlineShapes.AddPicture(ImgPath,ref o,ref o,ref o);WordTab.Cell(3,3).Range.InlineShapes[0].Width = 255;WordTab.Cell(3,3).Range.InlineShapes[0].Height = 152;ImgPath:图片路径这是往Word里的表格中插入图片的代码WordTab.Cell(3,3)是一个表格的单元格如果想往Word其他地方插入把WordTab.Cell(3,3).Range改一下改成你要插入地方的Range具体可以看 http://community.csdn.net/Expert/topic/3913/3913839.xml?temp=.5681116 从数据库先读出来写到本机上!前面的应该不难我给你一段 我用的插入图片的方法!先创建WordwordDoc = wordApp.Documents.Open(System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, ref WritePasswordTemplate, System.Type.Missing, System.Type.Missing, System.Type.Missing);然后在你原来摸版的位置插入图片private void FillImageToWord(string imageName){ IEnumerator enumerator1 = inlineShapes.GetEnumerator(); enumerator1.Reset(); enumerator1.MoveNext(); InlineShape inlineShape = enumerator1.Current as InlineShape; Shape curShape = inlineShape.ConvertToShape(); enumerator1 = null; if (curShape == null) return; object left = curShape.Left; object top = curShape.Top; object width = curShape.Width; object height = curShape.Height; object anchor = curShape.Anchor; object missing = Type.Missing; Word.Shape newShape = shapes.AddPicture(imageName, ref missing, ref missing, ref left, ref top, ref width, ref height, ref anchor); curShape.Delete();}希望你有用! 我不很明白你想做的是什么。但是用WORD2003的XML支持在.NET中可以比容易操作。大体的做法是将DOC文件直接SAVE AS为XML临时文件,然后找到要插入的位置(即:XML标记)把上面<w:pict/>段插入后,再SAVE为DOC文件即可。如果用户使用WORD97的,那就只有通过VBA来做了。这种做法,国内现在没人用,我是看到国外的文献上的例子,具体我也没尝试过,但就从文本操作的角度来将是不应该有难度的。关键是那段乱码是用什么编码方式取得的:(。BTY,要是你实现了,也给我共享一下:)以下是大概的代码:从数据库获取图片及图片编码部分就不写了 /// <summary> /// 将编码后的图片数据插入WORD文档 /// </summary> /// <param name="filepath">WORD文档全路径</param> /// <param name="picCode">编码后的图片数据</param> /// <param name="picWidth">显示图片的宽(单位:pt)</param> /// <param name="picheight">显示图片的高(单位:pt)</param> private void InsertWordPic(string filepath,string picCode,string picWidth,string picheight) { StreamWriter sw; string tmpfile=Path.GetTempPath+"//new123.xml"; bool isNewFlag=false; //检查文件是否存在,否,则创建一个 if(!File.Exists(filepath)) { File.Create(tmpfile).Close(); isNewFlag=true; } else { File.Copy(filepath,Path.GetTempPath+"//new123.xml",true); } string context=""; //如果是新文件 if(isNewFlag) { //写入WORD xml文件头 context="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><?mso-application progid=\"Word.Document\"?><w:wordDocument xmlns:w=\"http://schemas.microsoft.com/office/word/2003/wordml\" mlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:w10=\"urn:schemas-microsoft-com:office:word\" xmlns:sl=\"http://emas.microsoft.com/schemaLibrary/2003/core\" xmlns:aml=\"http://schemas.microsoft.com/aml/2001/core\" xmlns:wx=\"http://schemas.microsoft.com/office/word/2003/auxHint\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" ns:dt=\"uuid:C2F41010-65B3-11d1-A29F-00AA00C14882\" w:macrosPresent=\"no\" w:embeddedObjPresent=\"no\" w:xPresent=\"no\" xml:space=\"preserve\"><w:body>"; //写入图片数据编码,类似XML数据岛 context=string.Concat(context,"<w:pict><w:binData w:name=\"wordml://01000001.gif\">",picCode,"</w:binData>"); //写入图片显示定义 string PicName=(new FileInfo(filepath)).Name; context=string.Concat(context,"<v:shape id=\"_x0000_i1025\" type=\"#_x0000_t75\" style=\"width:"+picWidth+";height:"+picheight+"\"><v:imagedata src=\"wordml://01000001.gif\" o:title=\""+PicName.Substring(0,PicName.LastIndexOf("."))+"\"/></v:shape></w:pict>"); //关闭文档结构 context=string.Concat(context,"</w:wordDocument>"); //打开文件,准备写入 sw=new StreamWriter(tmpfile); sw.Write(context); sw.Flush(); sw.Close(); } else { //准备 //写入图片数据编码,类似XML数据岛。w:binData可以在文档的如何地方,w:name是KEY context=string.Concat(context,"<w:binData w:name=\"wordml://01000001.gif\">",picCode,"</w:binData>"); //打开临时文件,插入图片数据 XmlDocument doc=new XmlDocument(tmpfile); XmlDocumentFragment df=doc.CreateDocumentFragment(); df.InnerXml=context; XmlNode node=doc.DocumentElement; node.InsertAfter(df,node.LastChild); //写入图片显示定义 string PicName=(new FileInfo(filepath)).Name; context=string.Concat("<w:pict><v:shape id=\"_x0000_i1025\" type=\"#_x0000_t75\" style=\"width:"+picWidth+";height:"+picheight+"\"><v:imagedata src=\"wordml://01000001.gif\" o:title=\""+PicName.Substring(0,PicName.LastIndexOf("."))+"\"/></v:shape></w:pict>"); df.InnerXml=context; //查找要显示图片的节点 插入 大概的写一下肯定有错误,要用XPATH定义精确比较快! foreach(XmlNode nd in (node.SelectNodes("w:pict"))) { if(nd.InnerText=="这里插入图片") { nd.InsertAfter(df,node.FirstChild); } } doc.Save(); } //返回文档 File.Copy(tmpfile,filepath,true); } 补充依据,DOC文档是编译过的,你要保存成WORD的XML文件格式。 补充一句,DOC文档是编译过的,不能直接转为XML,你要先保存成WORD的XML文件格式。待图片插入后直接将XML报存为DOC文档 哦,是这样啊... 谢谢!word2000可以吗? .net 中怎样调用当前机器的用户名 C# C/S结构文件夹相互传送 C# 监控键盘 如何实现dialog的效果 为防止SQL注入,查询的存储过程式应该怎样写? 字符串占位的问题 哪里有.net framework sdk 中文版本下载? IsValueType 的BUG 在逐句调试程序时,按键盘哪个键执行下一句。 这是什么错误信息? 从txt文件导入到文本框中,再存储到数据库中 如何判断进程是否关闭 如果进程关闭的话 再次调用他
前车之鉴!
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core"
xmlns:aml="http://schemas.microsoft.com/aml/2001/core"
xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no" xml:space="preserve">
<w:body>
<w:pict>
<w:binData w:name="wordml://01000001.gif">
R0lGODlh9AH0AfcAAP//////AP8A//8AAAD//wD/AAAA/wAAAGZmZv/ix/+8oW05FlQjAhQcrwgk
mwYTiZBOUv+XnUYgJP+Hk/7Kz3I4P/9yiP+vu2IMHKQWLnUSJLgdOv/09oQJIUsPGzcLFP9WecaX
oP/v8v/3+f/7/P/8/dHO0KqIpPv5/GVZjnx2mFVQqEhFjQAAAouMphEaqREaphMbrQ8Vgw4TeAgL
Rjk8agcPaQ0YnA8ZohAYjhcisQUReAcUiwYSgAkVjwoWlS4yWgABCAIINwIEEhottgIDCyU3uEVK
bGdrigQQWCk+vTdS6DVO2zlT6jJJzR0qdSQyjQQNPQIHI....(省略,此处为图片编码,你应该可以查到是用什么方式生成的。)
</w:binData>
<v:shape id="_x0000_i1025" type="#_x0000_t75" style="width:375pt;height:375pt">
<v:imagedata src="wordml://01000001.gif" o:title="3_oJYvYzrGpaeK"/>
</v:shape>
</w:pict>
</w:body>
</w:wordDocument>
你试试看直接输出图片,然后生成这样一个文档。Happy program!
将数据库中的图片,输出到一个已经存在的word文件中(如果没有,即时新建一个word文档也可),你提出的解决办法我不是很了解。
WordTab.Cell(3,3).Range.Text = "";
WordTab.Cell(3,3).Range.InlineShapes.AddPicture(ImgPath,ref o,ref o,ref o);
WordTab.Cell(3,3).Range.InlineShapes[0].Width = 255;
WordTab.Cell(3,3).Range.InlineShapes[0].Height = 152;ImgPath:图片路径
这是往Word里的表格中插入图片的代码
WordTab.Cell(3,3)是一个表格的单元格
如果想往Word其他地方插入
把WordTab.Cell(3,3).Range改一下改成你要插入地方的Range具体可以看 http://community.csdn.net/Expert/topic/3913/3913839.xml?temp=.5681116
前面的应该不难我给你一段 我用的插入图片的方法!
先创建Word
wordDoc = wordApp.Documents.Open(System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, ref WritePasswordTemplate, System.Type.Missing,
System.Type.Missing, System.Type.Missing);然后在你原来摸版的位置插入图片
private void FillImageToWord(string imageName)
{
IEnumerator enumerator1 = inlineShapes.GetEnumerator();
enumerator1.Reset();
enumerator1.MoveNext();
InlineShape inlineShape = enumerator1.Current as InlineShape;
Shape curShape = inlineShape.ConvertToShape();
enumerator1 = null;
if (curShape == null) return;
object left = curShape.Left;
object top = curShape.Top;
object width = curShape.Width;
object height = curShape.Height;
object anchor = curShape.Anchor;
object missing = Type.Missing;
Word.Shape newShape = shapes.AddPicture(imageName, ref missing, ref missing,
ref left, ref top, ref width, ref height, ref anchor);
curShape.Delete();
}
希望你有用!
如果用户使用WORD97的,那就只有通过VBA来做了。
这种做法,国内现在没人用,我是看到国外的文献上的例子,具体我也没尝试过,但就从文本操作的角度来将是不应该有难度的。关键是那段乱码是用什么编码方式取得的:(。BTY,要是你实现了,也给我共享一下:)以下是大概的代码:从数据库获取图片及图片编码部分就不写了
/// <summary>
/// 将编码后的图片数据插入WORD文档
/// </summary>
/// <param name="filepath">WORD文档全路径</param>
/// <param name="picCode">编码后的图片数据</param>
/// <param name="picWidth">显示图片的宽(单位:pt)</param>
/// <param name="picheight">显示图片的高(单位:pt)</param>
private void InsertWordPic(string filepath,string picCode,string picWidth,string picheight)
{
StreamWriter sw;
string tmpfile=Path.GetTempPath+"//new123.xml";
bool isNewFlag=false;
//检查文件是否存在,否,则创建一个
if(!File.Exists(filepath))
{
File.Create(tmpfile).Close();
isNewFlag=true;
}
else
{
File.Copy(filepath,Path.GetTempPath+"//new123.xml",true);
}
string context="";
//如果是新文件
if(isNewFlag)
{
//写入WORD xml文件头
context="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><?mso-application progid=\"Word.Document\"?><w:wordDocument xmlns:w=\"http://schemas.microsoft.com/office/word/2003/wordml\" mlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:w10=\"urn:schemas-microsoft-com:office:word\" xmlns:sl=\"http://emas.microsoft.com/schemaLibrary/2003/core\" xmlns:aml=\"http://schemas.microsoft.com/aml/2001/core\" xmlns:wx=\"http://schemas.microsoft.com/office/word/2003/auxHint\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" ns:dt=\"uuid:C2F41010-65B3-11d1-A29F-00AA00C14882\" w:macrosPresent=\"no\" w:embeddedObjPresent=\"no\" w:xPresent=\"no\" xml:space=\"preserve\"><w:body>";
//写入图片数据编码,类似XML数据岛
context=string.Concat(context,"<w:pict><w:binData w:name=\"wordml://01000001.gif\">",picCode,"</w:binData>");
//写入图片显示定义
string PicName=(new FileInfo(filepath)).Name;
context=string.Concat(context,"<v:shape id=\"_x0000_i1025\" type=\"#_x0000_t75\" style=\"width:"+picWidth+";height:"+picheight+"\"><v:imagedata src=\"wordml://01000001.gif\" o:title=\""+PicName.Substring(0,PicName.LastIndexOf("."))+"\"/></v:shape></w:pict>");
//关闭文档结构
context=string.Concat(context,"</w:wordDocument>");
//打开文件,准备写入
sw=new StreamWriter(tmpfile);
sw.Write(context);
sw.Flush();
sw.Close();
}
else
{
//准备
//写入图片数据编码,类似XML数据岛。w:binData可以在文档的如何地方,w:name是KEY
context=string.Concat(context,"<w:binData w:name=\"wordml://01000001.gif\">",picCode,"</w:binData>");
//打开临时文件,插入图片数据
XmlDocument doc=new XmlDocument(tmpfile);
XmlDocumentFragment df=doc.CreateDocumentFragment();
df.InnerXml=context;
XmlNode node=doc.DocumentElement;
node.InsertAfter(df,node.LastChild);
//写入图片显示定义
string PicName=(new FileInfo(filepath)).Name;
context=string.Concat("<w:pict><v:shape id=\"_x0000_i1025\" type=\"#_x0000_t75\" style=\"width:"+picWidth+";height:"+picheight+"\"><v:imagedata src=\"wordml://01000001.gif\" o:title=\""+PicName.Substring(0,PicName.LastIndexOf("."))+"\"/></v:shape></w:pict>");
df.InnerXml=context;
//查找要显示图片的节点 插入 大概的写一下肯定有错误,要用XPATH定义精确比较快!
foreach(XmlNode nd in (node.SelectNodes("w:pict")))
{
if(nd.InnerText=="这里插入图片")
{
nd.InsertAfter(df,node.FirstChild);
}
}
doc.Save();
}
//返回文档
File.Copy(tmpfile,filepath,true);
}