一个gridview中包含图片,用以下方法导出到word时文字能显示,图片都是红叉:private void Export()
{
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("试卷.doc",Encoding.UTF8).ToString());
Response.ContentType = "application/ms-word";
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
this.GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}求高人帮忙。
{
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("试卷.doc",Encoding.UTF8).ToString());
Response.ContentType = "application/ms-word";
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
this.GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}求高人帮忙。
顶
具体实现的网上很多的,我们的代码我现在也找不到了!不过大体的思路就是这么做的
具体实现的网上很多的,我们的代码我现在也找不到了!不过大体的思路就是这么做的
如何使用C#在Word文档中插入图片以及读取Word文档中的图片呢,看下面:
网上只有相应的文本读取写入介绍.
如下:
this.OpenFile(@"c:\1.doc",false);
//读取并将图片保存到指定目录中 int i = 0;
foreach (Word.InlineShape ish in MyDoc.InlineShapes)
{
if ((ish.Type == Word.WdInlineShapeType.wdInlineShapeLinkedPicture) || (ish.Type==Word.WdInlineShapeType.wdInlineShapePicture))
{
ish.Select();
MyWordApp.Selection.Copy();
Image image = Clipboard.GetImage(); Bitmap bitmap = new Bitmap(image);
bitmap.Save("c:\\pic" + i.ToString() + ".jpg");
i++;
}
}
于是从网上找了另外一个方法,修改下试验通过,和大家分享下:using word; public void InsertImage()
{
string path = @"e:\2.gif";
object missing = System.Reflection.Missing.Value;
Word.ApplicationClass oWordApplic = new ApplicationClass();
oWordApplic.Visible = true; object LinkToFile = false;
object SaveWithDocument = true;
Word.Document WordDoc = oWordApplic.Documents.Add(ref missing, ref missing, ref missing, ref missing);
oWordApplic.Selection.InlineShapes.AddPicture(path, ref LinkToFile, ref SaveWithDocument, ref missing);
}但是这种方法只能单纯插入图片,如何确定图片的位置和输出GridView1中的文字保持一致还未能解决,求高手帮忙。
当然这个方法并不好,因为如果无法访问网络的话,就看不到图片了。所以说是一个临时地方法。
方法1、在服务器端,用C#导出GridView中的数据,并把服务器端的图片,用word的API,插入进去。
方法2、使用Web Service或者HTTP代理类,模拟客户端,访问要导出页面,此时在程序里,得到静态页面比如“导出.html”,这个页面,然后,再把这个“导出.html”转换为word,此过程类拟,我们在IE中的,另存为word,这样也可以有图片。
可以考虑用js脚本解决导出的问题,强烈反对用服务器端的application word.exe 或是 excel.exe方法,因为稍有不慎就回产生n多的线程影响服务器的性能。