Ⅰ 急求C#窗体操作VSTO如何向word模板中的特定文本框添加图文信息
private void button1_Click(object sender, EventArgs e) { FileStream stream = null; SqlConnection conn = null; SqlCommand cmd = null; try { richTextBox1.SaveFile("temp.rtf"); stream = new FileStream("temp.rtf", FileMode.Open, FileAccess.Read); int size = Convert.ToInt32(stream.Length); Byte[] rtf = new Byte[size]; stream.Read(rtf, 0, size); conn = new SqlConnection("Data Source=d30 \\sqlexpress;Initial Catalog=Image;Integrated Security=True;"); conn.Open(); cmd = new SqlCommand("UPDATE pic SET picture=@Photo WHERE ID=1", conn); SqlParameter paramRTF = new SqlParameter("@Photo", SqlDbType.Image, rtf.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, rtf); cmd.Parameters.Add(paramRTF); int rowsUpdated = Convert.ToInt32(cmd.ExecuteNonQuery()); MessageBox.Show(String.Format("{0} rows updated", rowsUpdated)); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (stream != null) stream.Close(); if (cmd != null) cmd.Parameters.Clear(); if (conn != null) conn.Close(); } } private void button2_Click(object sender, EventArgs e) { richTextBox1.Clear(); SqlConnection cn = null; SqlCommand cmd = null; SqlDataReader reader = null; try { cn = new SqlConnection("Data Source=d30 \\sqlexpress;Initial Catalog=Image;Integrated Security=True;"); cn.Open(); cmd = new SqlCommand("SELECT picture FROM pic WHERE ID=1", cn); reader = cmd.ExecuteReader(); reader.Read(); if (reader.HasRows) { if (!reader.IsDBNull(0)) { Byte[] rtf = new Byte[Convert.ToInt32((reader.GetBytes(0, 0, null, 0, Int32.MaxValue)))]; long bytesReceived = reader.GetBytes(0, 0, rtf, 0, rtf.Length); ASCIIEncoding encoding = new ASCIIEncoding(); richTextBox1.Rtf = encoding.GetString(rtf, 0, Convert.ToInt32(bytesReceived)); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (reader != null) reader.Close(); if (cn != null) cn.Close(); } }
Ⅱ 怎么用vs2010实现对word和excel的操作
之前都是用VBA来开发Office解决方案的,后来微软开发出了VSTO这个工具包来创建自定义的应用程序,使得开发Office应用程序更加简单,并且用VSTO来开发office应用程序可以使用Visual studio开发环境中的众多功能和CLR提供的内存管理,垃圾回收等功能。
Office应用程序如Word,Excel和Outlook都是用非托管代码来写的, 而我们创建的VSTO工程使用的是托管代码,这时候就需要使用互操作程序集来与Office应用程序里的非托管COM对象交互,然后主互操作程序集(PIA)指的是官方发布的互操作程序集,如果电脑中安装了PIA,当你添加对类库的引用时,那么Visual Studio会自动加载PIA,微软为Office应用程序提供了PIA,如EXcel PIA就是Microsof.Office.Interop.Excel.dll,其他应用程序也类似。当安装了Office产品后,PIA会自动安装在电脑的GAC目录里,每当创建一个VSTO解决方案, Visual Studio会自动为该解决方案加载合适的Office PIA引用和其他程序集
宿主项是表示Office对象模型入口点的类。应用程序外接程序使用Microsoft.Office.Tools.AddIn类为宿主项,此宿主项提供对宿主应用程序和成员的对象模型的访问,可以通过宿主项添加数据绑定的能力和提供额外的事件来扩展本地Office文档。而创建一个Excel解决方案会创建4个Excel宿主项:Workbook,Sheet1,Sheet2和Sheet3.
Ⅲ winform读取word文件
在Form里加一个button按钮和一个richtextbox,给按钮加一个Click事件
private void button1_Click(object sender, EventArgs e)
{
object filepath= "D:\\Debugtest.doc";
openWord(filepath);
}
//把Word
文档内容取出来放到richtextBox里
private void openWord(object SPath)
{
object file = SPath;
object nullobj = System.Reflection.Missing.Value;
Word.Document doc = myWordApp.Documents.Open(ref file, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,ref nullobj,ref nullobj,ref nullobj,ref nullobj);
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();
this.richTextBox1.Text = data.GetData(DataFormats.Text).ToString();
doc.Close(ref nullobj, ref nullobj, ref nullobj);
}
Ⅳ Delphi中如何调用“接口”,以读取TWordDocument.BuiltInDocumentProperties
要使用 Excel 中的内置属性,请使用以下属性:
在文档级项目中,使用 ThisWorkbook 类的 BuiltinDocumentProperties 属性。
在 VSTO 外接程序项目中,使用 Microsoft.Office.Interop.Excel.Workbook 对象的 BuiltinDocumentProperties 属性。
这些属性将返回 T:Microsoft.Office.Core.DocumentProperties 对象,该对象为 T:Microsoft.Office.Core.DocumentProperty 对象的集合。可以使用集合的 Item 属性,按名称或索引检索该集合中的特定属性。
下面的代码示例演示了如何更改文档级项目中的内置 Revision Number 属性。
若要更改在 Excel 中的修订号属性
将内置文档属性分配给变量。
C#
VB
Microsoft.Office.Core.DocumentProperties properties;
properties = (Microsoft.Office.Core.DocumentProperties)
Globals.ThisWorkbook.BuiltinDocumentProperties;
Microsoft.Office.Core.DocumentProperty prop;
prop = properties["Revision Number"];
以 1 递增 Revision Number 属性。
C#
VB
if (prop.Value == null)
{
prop.Value = 1;
}
else
{
int revision;
if (int.TryParse((string)prop.Value, out revision))
{
prop.Value = revision + 1;
MessageBox.Show("Revision Number = " + revision);
}
else
{
MessageBox.Show("Revision Number = invalid value");
}
}
在 Word 中设置文档属性
若要使用 Word 中的内置属性,请使用以下属性:
在文档级项目中,使用 ThisDocument 类的 BuiltInDocumentProperties 属性。
在 VSTO 外接程序项目中,使用 Microsoft.Office.Interop.Word.Document 对象的P:Microsoft.Office.Interop.Word._Document.BuiltInDocumentProperties 属性。
这些属性将返回 T:Microsoft.Office.Core.DocumentProperties 对象,该对象为 T:Microsoft.Office.Core.DocumentProperty 对象的集合。可以使用集合的 Item 属性,按名称或索引检索该集合中的特定属性。
下面的代码示例演示了如何更改文档级项目中的内置 Subject 属性。
若要更改主题属性
将内置文档属性分配给变量。
C#
VB
Microsoft.Office.Core.DocumentProperties properties;
properties = (Microsoft.Office.Core.DocumentProperties)
Globals.ThisDocument.BuiltInDocumentProperties;
将 Subject 属性更改为“白皮书”。
C#
VB
// Set the Subject property.
properties["Subject"].Value = "Whitepaper";