62,046
社区成员
发帖
与我相关
我的任务
分享
protected string CreatWord(string id)
{
Sendinformation sendinfo = SendinformationLogic.Get(id);
string name = sendinfo.Caption.ToString();
object path;
string strContent;
string picName = "http://localhost/SendInfo/Files/20140710/党政内网修改3.05.files/image002.jpg";
MSWord.Application wordApp;
MSWord.Document wordDoc;
string dates = System.DateTime.Now.ToString("yyyyMMdd");
string phyPath = Server.MapPath("~/") + "Files\\报送信息导出\\" + dates + "\\";
if (!Directory.Exists(phyPath))
Directory.CreateDirectory(phyPath); //创建文件所在目录
path = phyPath + @"\" + name + ".doc";
wordApp = new MSWord.ApplicationClass();
if (File.Exists((string)path))
File.Delete((string)path);
Object Nothing = Missing.Value;
wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//插入文档图片
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = wordDoc.Application.Selection.Range;
wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(picName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
wordDoc.Application.ActiveDocument.InlineShapes[1].Width = 553f;
wordDoc.Application.ActiveDocument.InlineShapes[1].Height = 211f;
MSWord.Shape s = wordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
s.WrapFormat.Type = MSWord.WdWrapType.wdWrapSquare;
wordApp.Selection.TypeParagraph();//插入段落
//插入文档内容
strContent = Server.HtmlDecode(sendinfo.Content);
wordDoc.Paragraphs.Last.Range.Text = strContent;
object format = MSWord.WdSaveFormat.wdFormatDocument;
wordDoc.SaveAs(ref path, 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);
MSWord._Document doc = wordDoc as MSWord._Document;
doc.Close(ref Nothing, ref Nothing, ref Nothing);
MSWord._Application app = wordApp as MSWord._Application;
app.Quit(ref Nothing, ref Nothing, ref Nothing);
return path.ToString();
}