📄 word2.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class word2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
StreamReader sr = new StreamReader(Server.MapPath("word2.aspx"));
string ss = sr.ReadToEnd();
Response.Write(ss.ToCharArray());
SaveAsWord(@"d:\test",ss);
}
public void SaveAsWord(string FileName, string text)
{
if (text == "")
return;
if (FileName == "")
return;
//string FileName=this.saveFileDialog1.FileName;
if (FileName.Length < 1)
return;
FileName += ".doc";
try
{
Word.ApplicationClass MyWord = new Word.ApplicationClass();
Word.Document MyDoc;
Object Nothing = System.Reflection.Missing.Value;
MyDoc = MyWord.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//Word.Table tb;
//Word.Row row = Word.Row;
//row.Range.Text = "aaaaa";
//object o = (object)row;
//tb.Rows.Add(ref o );
//o = (object)tb;
//object index = (object)1;
//MyDoc.Sections.Add(ref o,ref index);
MyDoc.Paragraphs.Last.Range.Text = text;
object MyFileName = FileName;
//将WordDoc文档对象的内容保存为DOC文档
MyDoc.SaveAs(ref MyFileName, 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文档对象
MyDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭WordApp组件对象
MyWord.Quit(ref Nothing, ref Nothing, ref Nothing);
// MessageBox.Show("WORD文件保存成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception Err)
{
// MessageBox.Show("WORD文件保存操作失败!" + Err.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -