//从控件中导出输出到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;
}