.Net将数据导出Word(Office 2003)
2007-01-23 18:03
357 查看
1,首先要导入Com文件Microsoft Word 11.0 Object Library.
2.声明using System.Text.RegularExpressions;
3,要添加Interop.Word.dll文件
http://files.cnblogs.com/ghostljj/Interop.Word.rar
-----------------------------------------------------------------
4.执行下面步骤
Object Nothing = System.Reflection.Missing.Value;
//取得Word文件保存路径
object filename = System.Web.HttpRuntime.AppDomainAppPath + "\\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();
//将一个<br>变成两个<br>
//strPageContent = Regex.Replace(strPageContent, "(<br>[\\s]*)+", "<br /><br />");
//将所有标签去掉,只剩下\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[] { "<br />" }, 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 = "文档路径:<a href='/c:\\111.doc'>c:\\111.doc</a>(点击链接查看)<br>生成结果:成功!";
//使导出文件清除特殊符号
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("|", " ");
//这个是从服务器中下载文件,(请参考我另外一个文章)
//参考网址/article/5871223.html
ResponseFile(Page.Request, Page.Response, outFileName + ".doc"
, System.Web.HttpRuntime.AppDomainAppPath + "\\XMLFiles\\EduceWordFiles\\" + this.Context.User.Identity.Name + ".doc", 1024000);
//--------------------------------------------------------
5.如果是放在IIS中,现在是不能到出的,还要配置一下
方案一:在Web.config中添加
<system.web>
<identity impersonate="true" userName="管理员名" password="密码" />
<system.web>
方案二:
(1)在运行->dcomcnfg打开组件服务
(2) 在 控制台根目录->组件服务->计算机->我的电脑->DCOM配置->Microsoft Word 文档->属性->安全
(3)启动和激活权限->使用自定义->添加一个ASPNET用户,还有打开本地启动和本地激活
访问权限->使用自定义->添加一个ASPNET用户,还有打开本地访问和远程访问
2.声明using System.Text.RegularExpressions;
3,要添加Interop.Word.dll文件
http://files.cnblogs.com/ghostljj/Interop.Word.rar
-----------------------------------------------------------------
4.执行下面步骤
Object Nothing = System.Reflection.Missing.Value;
//取得Word文件保存路径
object filename = System.Web.HttpRuntime.AppDomainAppPath + "\\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();
//将一个<br>变成两个<br>
//strPageContent = Regex.Replace(strPageContent, "(<br>[\\s]*)+", "<br /><br />");
//将所有标签去掉,只剩下\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[] { "<br />" }, 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 = "文档路径:<a href='/c:\\111.doc'>c:\\111.doc</a>(点击链接查看)<br>生成结果:成功!";
//使导出文件清除特殊符号
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("|", " ");
//这个是从服务器中下载文件,(请参考我另外一个文章)
//参考网址/article/5871223.html
ResponseFile(Page.Request, Page.Response, outFileName + ".doc"
, System.Web.HttpRuntime.AppDomainAppPath + "\\XMLFiles\\EduceWordFiles\\" + this.Context.User.Identity.Name + ".doc", 1024000);
//--------------------------------------------------------
5.如果是放在IIS中,现在是不能到出的,还要配置一下
方案一:在Web.config中添加
<system.web>
<identity impersonate="true" userName="管理员名" password="密码" />
<system.web>
方案二:
(1)在运行->dcomcnfg打开组件服务
(2) 在 控制台根目录->组件服务->计算机->我的电脑->DCOM配置->Microsoft Word 文档->属性->安全
(3)启动和激活权限->使用自定义->添加一个ASPNET用户,还有打开本地启动和本地激活
访问权限->使用自定义->添加一个ASPNET用户,还有打开本地访问和远程访问
相关文章推荐
- [转载].Net将数据导出Word
- 在.NET环境下将报表数据导出EXCEL和WORD
- .Net将数据导出Word
- .NET使用Office Open XML导出大量数据到 Excel
- .NET使用Office Open XML导出大量数据到 Excel
- .net导出数据到Excel (安装office excel环境情况)
- Office 2007 打开 C# VS2005 导出的Excel时报错:单元格数据太大 Office 2003打开正常 的解决办法
- 利用OFFICE 2003 OWC 绘图控件在.NET平台下实现数据图表的绘制
- 利用.net替换Word的内容(从数据库中取数据来替换word里面的书签)
- C# 将datagridview 数据导出到Excel、word
- Excel vba从excel中导出表格数据和图表到新word文档
- 将DBGrid的数据导出到word和excel中
- .net 导出数据到excel
- 将ASP.NET页面内的数据导出到Excel 或 Word中
- 使用phpoffice/phpexcel导出数据不全或者文件有错误
- .net数据导出到excel【Update20111009】
- Word 2003 进阶培训教程(文员OFFICE救命技巧)
- 导出Word 里面用替换符替换数据 并导出到PDF
- 【推荐】.NET使用NPOI组件将数据导出Excel
- ASP.NET 利用 Microsoft.Office.Interop.Excel 版本导出Excel数据