.Net将数据导出Word(转)
1,首先要导入Com文件Microsoft Word 11.0 Object Library.
2.声明using System.Text.RegularExpressions;
3,要添加Interop.Word.dll文件
https://files.cnblogs.com/ghostljj/Interop.Word.rar
-----------------------------------------------------------------
4.执行下面步骤
Object Nothing = System.Reflection.Missing.Value;
//取得Word文件保存路径
object filename = System.Web.HttpRuntime.AppDomainAppPath + "file://xmlfiles//EduceWordFiles//" + this.Context.User.Identity.Name + ".doc";
//创建一个名为WordApp的组件对象
Word.Application WordApp = new Word.ApplicationClass();
//创建一个名为WordDoc的文档对象
Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//增加一表格
//Word.Table table = WordDoc.Tables.Add(WordApp.Selection.Range, 1, 1, ref Nothing, ref Nothing);
//在表格第一单元格中添加自定义的文字内容
//table.Cell(1, 1).Range.Text = "在表格第一单元格中添加自定义的文字内容";
//在文档空白地方添加文字内容
//WordDoc.Paragraphs.Last.Range.Bold = 72;
//WordApp.Visible = true;
//WordDoc.Activate();
#region 标题
WordApp.Selection.Font.Size = 15;
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; // 居中
WordApp.Selection.Font.Bold = 1; // 黑体
WordApp.Selection.TypeText(SaveShowInfo.Title);
#endregion
#region 时间和来源
WordApp.Selection.TypeParagraph();
WordApp.Selection.Font.Size = 10;
WordApp.Selection.Font.Bold = 0; // 取消黑体
WordApp.Selection.TypeText("发布时间:" + SaveShowInfo.SaveTime + " 来源:" + SaveShowInfo.WebSiteName);
#endregion
#region 摘要
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft; // 居左
WordApp.Selection.TypeText("摘要:");
WordApp.Selection.TypeParagraph();
//WordApp.Selection.ParagraphFormat.CharacterUnitFirstLineIndent = 2.0f; //首行缩进2个字符
WordApp.Selection.TypeText(" " + SaveShowInfo.Summary);
#endregion
#region 内容
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeText("内容:");
string strPageContent = SaveShowInfo.Content.ToString();
//将一个
变成两个
//strPageContent = Regex.Replace(strPageContent, "(
[\\s]*)+", "
");
//将所有标签去掉,只剩下\r\n
strPageContent = Regex.Replace(strPageContent, @"<[^>]+/?>|]+>", "", RegexOptions.IgnoreCase);
string[] strContents = strPageContent.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string strContent in strContents)
{
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeText(" " + strContent);
}
#endregion
#region 图片导出
string[] strPictureUrls = SaveShowInfo.PictureUrl.Split(new string[] { "
" }, StringSplitOptions.RemoveEmptyEntries);
if (strPictureUrls.Length != 0 && strPictureUrls[0] != "")
{
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeText("图片:");
WordApp.Selection.TypeParagraph();
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; // 居中
foreach (string strPictureUrl in strPictureUrls)
{
if (strPictureUrl.Length > 10)
{
string strUrl = getPictureOnlyUrl(strPictureUrl);
WordApp.Selection.InlineShapes.AddPicture(strUrl, ref Nothing, ref Nothing, ref Nothing);
WordApp.Selection.TypeParagraph();
}
}
}
#endregion
//WordDoc.Paragraphs.Last.Range.Text += SaveShowInfo.PictureUrl;
//将WordDoc文档对象的内容保存为DOC文档
WordDoc.SaveAs(ref filename, 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, ref Nothing);
//关闭WordDoc文档对象
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭WordApp组件对象
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
//返回结果
//lblMsg.Text = "文档路径:c:\\111.doc(点击链接查看)
生成结果:成功!";
//使导出文件清除特殊符号
string outFileName = si.Title.Replace("/", " ");
outFileName = outFileName.Replace("\\", " ");
outFileName = outFileName.Replace(":", " ");
outFileName = outFileName.Replace("*", " ");
outFileName = outFileName.Replace("?", " ");
outFileName = outFileName.Replace("\"", " ");
outFileName = outFileName.Replace("<", " ");
outFileName = outFileName.Replace(">", " ");
outFileName = outFileName.Replace("|", " ");
//这个是从服务器中下载文件,(请参考我另外一个文章)
//参考网址http://www.cnblogs.com/ghostljj/archive/2007/01/24/629293.html
ResponseFile(Page.Request, Page.Response, outFileName + ".doc"
, System.Web.HttpRuntime.AppDomainAppPath + "file://xmlfiles//EduceWordFiles//" + this.Context.User.Identity.Name + ".doc", 1024000);
//--------------------------------------------------------
5.如果是放在IIS中,现在是不能到出的,还要配置一下
方案一:在Web.config中添加
方案二:
(1)在运行->dcomcnfg打开组件服务
(2) 在 控制台根目录->组件服务->计算机->我的电脑->DCOM配置->Microsoft Word 文档->属性->安全
(3)启动和激活权限->使用自定义->添加一个ASPNET用户,还有打开本地启动和本地激活
访问权限->使用自定义->添加一个ASPNET用户,还有打开本地访问和远程访问