.net html 写入word文档,asp.net 写入word (Microsoft.Office.Interop.Word)经验记录(看不明白问我)...

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

@"

",

@"<.>",

@"(\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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值