您的位置:首页 > 其它

[转载].Net将数据导出Word

2010-11-21 10:56 267 查看
protected void Button2_Click(object sender, EventArgs e)
{
Test();
}

private void Test()
{
Object nothing = System.Reflection.Missing.Value;
//获取Word文档保存路径
object fileName = System.Web.HttpRuntime.AppDomainAppPath +"wj.doc";
//创建一个名为WordApp的组件对象
Word.ApplicationClass 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("我是标题");
#endregion
#region 时间和来源
WordApp.Selection.TypeParagraph();
WordApp.Selection.Font.Size = 10;
WordApp.Selection.Font.Bold = 0; // 取消黑体
WordApp.Selection.TypeText("发布时间:" + "我是时间" + " 来源:" + "我是文件来源");
#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(" " + "我是内容摘要");
#endregion
#region 内容
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeText("内容:");
string strPageContent = "我是内容";
//将一个<br>变成两个<br>
//strPageContent = Regex.Replace(strPageContent, "(<br>[/s]*)+", "<br /><br />");
//将所有标签去掉,只剩下rn
strPageContent = Regex.Replace(strPageContent, @"<[^>]+/?>|</[^>]+>", "", RegexOptions.IgnoreCase);

string[] strContents = strPageContent.Split(new string[] { "rn" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string strContent in strContents)
{
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeText(" " + strContent);
}
#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);
//关闭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 ;
//outFileName = outFileName.Replace("", " ");
//outFileName = outFileName.Replace(":", " ");
//outFileName = outFileName.Replace("*", " ");
//outFileName = outFileName.Replace("?", " ");
//outFileName = outFileName.Replace(""", " ");
//outFileName = outFileName.Replace("<", " ");
//outFileName = outFileName.Replace(">", " ");
//outFileName = outFileName.Replace("|", " ");
//Response.WriteFile(outFileName + ".doc",System.Web.HttpRuntime.AppDomainAppPath + "/XMLFiles/EduceWordFiles/" + this.Context.User.Identity.Name + ".doc",1024000);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: