//从控件中导出输出到html上的内容
private void ExpertControl(System.Web.UI.Control repeater, DocumentType type)
{
//关闭控件的视图状态
repeater.Page.EnableViewState = false;
//初始化HtmlWriter
System.IO.StringWriter writer = new System.IO.StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
repeater.RenderControl(htmlWriter);
object path; //文件路径变量
string strContent = StripHTML(htmlWriter.InnerWriter.ToString()); //对html文本处理
Microsoft.Office.Interop.Word.Application wordApp; //Word应用程序变量
Microsoft.Office.Interop.Word.Document wordDoc; //Word文档变量
path = @"D:\政策标引文本.doc"; //路径
wordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); //初始化
//如果已存在,则删除
if (File.Exists((string)path))
{
File.Delete((string)path);
}
//由于使用的是COM库,因此有许多变量需要用Missing.Value代替
Object Nothing = Missing.Value;
wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
string[] temp = strContent.Split('&');
//for (int i = 0; i < temp.Length - 1; i++)
//{
// if (i % 2 == 0)
// {
// wordDoc.Paragraphs.Last.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
// wordDoc.Paragraphs.Last.Range.Text = temp[i];
// }
// if (i % 2 == 1)
// {
// wordDoc.Paragraphs.Last.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
// wordDoc.Paragraphs.Last.Range.Text = temp[i];
// }
//}
//定义一个Word中的表格对象
Microsoft.Office.Interop.Word.Table table = wordDoc.Tables.Add(wordApp.Selection.Range, temp.Length - 1, 1, ref Nothing, ref Nothing);
//默认创建的表格没有边框,这里修改其属性,使得创建的表格带有边框
table.Borders.Enable = 1;
// wordDoc.Paragraphs.Last.Range.Font.Size = 15;
table.Range.Font.Size = 10;
table.Cell(1, 1).Range.Text = "政策标引内容\r";
table.Cell(1, 1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
//循环填充表格的内容
for (int i = 0; i <= temp.Length - 1; i++)
{
if (i % 2 == 0)
{
table.Cell(i + 2, 1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
table.Cell(i+2, 1).Range.Text = temp[i].TrimStart(' ').TrimEnd(' ');
}
if (i % 2 == 1)
{
table.Cell(i + 2, 1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
table.Cell(i + 2, 1).Range.Text = temp[i].TrimStart(' ').TrimEnd(' ');
}
}
//wordDoc.Paragraphs.Last.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
//wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
//wordDoc.Paragraphs.Last.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
//wordDoc.Paragraphs.Last.Range.Text = strContent;
//WdSaveFormat为Word 2003文档的保存格式
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
//WdSaveFormat为Word 2007文档的保存格式
//object format =Microsoft.Office.Interop.WdSaveFormat.wdFormatDocumentDefault;
//将wordDoc文档对象的内容保存为DOC文档
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);
//关闭wordDoc文档对象
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭wordApp组件对象
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
if (File.Exists(Convert.ToString(path)))
{
FileInfo file = new FileInfo(Convert.ToString(path));
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
Response.AddHeader("Content-length", file.Length.ToString());
Response.ContentType = "appliction/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}
//输出
//Response.Write(writer.ToString());
//Response.End();
}
//文档类型
public enum DocumentType
{
Word,
Excel
}
#endregion
#region -BtnWord_Click 执行导出word
protected void BtnWord_Click(object sender, EventArgs e)
{
ExpertControl(rptIndexData, DocumentType.Word);
}
#endregion
private string StripHTML(string strHtml)
{
string[] aryReg = {
@"</p>",
@"<.*?>",
@"(\r\n)+",
@"&(nbsp|#160);",
@" "
};
string[] aryRep = {
"&",
"",
"",
"",
""
};
string newReg = aryReg[0];
string strOutput = strHtml;
for (int i = 0; i < aryReg.Length; i++)
{
Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
strOutput = regex.Replace(strOutput, aryRep[i]);
}
return strOutput;
}
分享到:
相关推荐
Microsoft.Office.Interop.Word.dll Microsoft.Office.Interop.Excel.dll 二合一 详细代码请看博文:http://blog.csdn.net/panfuy/article/details/6386590
Microsoft.Office.Interop.Word.dll下载,里面包含各种版本的更新
Microsoft.Office.Interop下所有的dll文件 Microsoft Office 2013 下的COM组件类库 Microsoft.Office.Interop.Access ...Microsoft.Office.Interop.Word Microsoft.Vbe.Interop Microsoft.Office.Core
主要用于解决在使用office的dll操作后台word文件时候,在本机运行可以,但是在IIS服务器上会报一个 Retrieving the ...的问题
一、如果在运行某软件或编译程序时提示缺少、找不到microsoft.office.interop.word.dll等类似提示 下载来的microsoft.office.interop.word.dll拷贝到指定目录即可(一般是system系统目录或放到软件同级目录里面),...
/// 解决WORD文字写入时255个字符限制 /// /// <param name="wordApp">WORD文档 /// 查找的标记 /// 替换的文字 public void FindAndReplaceLong( Application wordApp, object findText, object ...
Microsoft.Office.Core.dll和Microsoft.Office.Interop.Excel.dll Microsoft.Office.Core.dll和Microsoft.Office.Interop.Excel.dll
Microsoft.Office.Interop.Word.dll,Microsoft.Office.Interop.Excel.dll,Microsoft.Office.Interop.Powerpoint.dll各个版本的DLL
Microsoft.Office.Interop.Word Microsoft Word 16.0 Object. Library C#操作word
Microsoft Office 2003 .NET 互操作DLL 含 Microsoft.Office.Interop.Access Microsoft.Office.Interop.Access.Dao ...Microsoft.Office.Interop.Word Microsoft.Vbe.Interop Microsoft.Office.Core
C# 操作Excel时往往需要引入Microsoft.Office.Interop.Excel.dll 和Interop.Microsoft.Office.Core.dll这两个DLL文件 这里提供两个文件的打包下载!!!!
Microsoft.Office.Interop.Word.dll.reg Microsoft.Vbe.Interop.dll Microsoft.Vbe.Interop.dll.reg mscomctl.dll mscomctl.dll.reg msdatasrc.dll msdatasrc.dll.reg mstag.dll mstag.dll.reg office.dll office....
最近接到一个公司开发需求需要提取Word的目录以及根据点击目录跳转到对应内容搜索众多资料得出一些经验分享 提取出来的属性可以用于树形展示 目录
microsoft.office.interop.excel
microsoft.office.interop.word word转pdf用到的哦。。。
各处收集的Microsoft.Office.Interop.Word打印各版本DLL。。。。。。。。。。。。