導航:首頁 > 文件教程 > vsto讀取word

vsto讀取word

發布時間:2023-03-06 14:14:35

Ⅰ 急求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";

閱讀全文

與vsto讀取word相關的資料

熱點內容
maya粒子表達式教程 瀏覽:84
抖音小視頻如何掛app 瀏覽:283
cad怎麼設置替補文件 瀏覽:790
win10啟動文件是空的 瀏覽:397
jk網站有哪些 瀏覽:134
學編程和3d哪個更好 瀏覽:932
win10移動硬碟文件無法打開 瀏覽:385
文件名是亂碼還刪不掉 瀏覽:643
蘋果鍵盤怎麼打開任務管理器 瀏覽:437
手機桌面文件名字大全 瀏覽:334
tplink默認無線密碼是多少 瀏覽:33
ipaddgm文件 瀏覽:99
lua語言編程用哪個平台 瀏覽:272
政采雲如何導出pdf投標文件 瀏覽:529
php獲取postjson數據 瀏覽:551
javatimetask 瀏覽:16
編程的話要什麼證件 瀏覽:94
錢脈通微信多開 瀏覽:878
中學生學編程哪個培訓機構好 瀏覽:852
榮耀路由TV設置文件共享錯誤 瀏覽:525

友情鏈接