项目需要导入Microsoft.Office.Interop.Word.dll
//图片绝对路径
string picUrl = "d://mypic.jpeg";
string outStr = "测试文本";
string spath = Server.MapPath("../") + "/UserFiles/" + "/results.doc";
try
{
//路径检测
string strDirectory = Server.MapPath("../") + "/UserFiles/";
if(!System.IO.Directory.Exists(strDirectory))
System.IO.Directory.CreateDirectory(strDirectory);
using(StreamWriter sw =new StreamWriter(spath, false, System.Text.Encoding.GetEncoding("GB2312")))
{
sw.WriteLine(outStr);
sw.Flush();
sw.Close();
}
}
catch{}
finally
{
// sw.Close();
}
//word插入图片
object Nothing=System.Reflection.Missing.Value;
object filename=Server.MapPath("../") + "/UserFiles/" + "/test.doc";
object objt=true;
Microsoft.Office.Interop.Word.Application wordApp=new Microsoft.Office.Interop.Word.ApplicationClass();
object format=Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
Microsoft.Office.Interop.Word.Document wordDoc = null;
try
{
// 打开word
Microsoft.Office.Interop.Word.Application thisApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
Object ConfirmConversions = false;
Object ReadOnly = true;
Object AddToRecentFiles = false;
Object PasswordDocument = System.Type.Missing;
Object PasswordTemplate = System.Type.Missing;
Object Revert = System.Type.Missing;
Object WritePasswordDocument = System.Type.Missing;
Object WritePasswordTemplate = System.Type.Missing;
Object Format = System.Type.Missing;
Object Encoding = System.Type.Missing;
Object Visible = System.Type.Missing;
Object OpenAndRepair = System.Type.Missing;
Object DocumentDirection = System.Type.Missing;
Object NoEncodingDialog = System.Type.Missing;
Object XMLTransform = System.Type.Missing;
//Microsoft.Office.Interop.Word.Document wordDoc =
wordDoc = thisApplication.Documents.Open(ref filename, ref ConfirmConversions,
ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
ref Revert,ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
ref NoEncodingDialog, ref XMLTransform );
//插入图片
Microsoft.Office.Interop.Word.InlineShape oShape= wordDoc.Words.Last.InlineShapes.AddPicture(picUrl,ref Nothing,ref objt,ref Nothing);
//oShape.WrapFormat.Type =Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
//将htm文件save as成doc文件
filename = Server.MapPath("../") + "/UserFiles/" + "/testfile.doc";
//wordDoc.Save();
//另存为
wordDoc.SaveAs(ref filename,ref format, 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);
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
try
{
//读取文件,并写入到客户端响应
FileStream fs = new FileStream(filename.ToString(),FileMode.Open,FileAccess.Read);
byte[] b = new Byte[fs.Length];
fs.Read(b, 0, b.Length);
fs.Flush();
fs.Close();
File.Delete(filename.ToString());
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";//ContentType;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("result.doc", System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length",b.Length.ToString());
Response.OutputStream.Write(b, 0, b.Length);
Response.Flush();
Response.End();
}
catch(Exception em)
{
Response.Write(em.ToString());
}
}
finally
{
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}