C# OFFICE WORD EXCEL操作
2014-08-01 20:00
441 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Office;
using MSWord = Microsoft.Office.Interop.Word;
using System.IO;
using System.Reflection;
namespace word
{
public partial class Form1 : Form
{
public Form1( )
{
InitializeComponent ();
}
[DllImport ("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(
IntPtr parentHandle,
IntPtr childAfter,
string className,
string windowTitle);
[DllImport ("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(
string lpClassName,
string lpWindowName
);
[DllImport ("user32.dll", EntryPoint = "GetWindowText")]
public static extern int GetWindowText(
IntPtr hwnd,
StringBuilder lpString,
int cch
);
[DllImport ("USER32.DLL", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SendMessage(
IntPtr hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
[MarshalAs (UnmanagedType.LPTStr)]StringBuilder lParam // second message parameter
);
const int WM_GETTEXT = 0x000D;
private void button1_Click(object sender, EventArgs e)
{
IntPtr hwnd = FindWindow ("TF_FpShow", "发票显示");
IntPtr c = FindWindowEx (hwnd, IntPtr.Zero, "TScrollBox", null);
IntPtr c1 = FindWindowEx (c, IntPtr.Zero, "TGroupBox", null);
IntPtr a = FindWindowEx (c1, IntPtr.Zero, null, null);
IntPtr temp = a;
int count=0;
string[] table=new string[100];
for (int i = 0; i < 100; i++)
{
IntPtr b = FindWindowEx (c1, a, "TEdit", null);
StringBuilder ret = new StringBuilder (256);
SendMessage (a, WM_GETTEXT, 100, ret);
table[i] = ret.ToString ();
a = b;
count++;
if (b == temp)
{
count--;
break;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
object path; //文件路径变量
string strContent; //文本内容变量
MSWord.Application wordApp; //Word应用程序变量
MSWord.Document wordDoc;
path = @"C:\MyWord.doc"; //路径
wordApp = new MSWord.ApplicationClass(); //初始化
//如果已存在,则删除
if (File.Exists((string)path))
{
File.Delete((string)path);
}
Object Nothing = Missing.Value;
wordDoc = wordApp.Documents.Add (ref Nothing, ref Nothing, ref Nothing, ref Nothing); //WdSaveFormat为Word文档的保存格式
//图片文件的路径
string filename = @"C:\BackgroundImage.jpg";
//要向Word文档中插入图片的位置
Object range = wordDoc.Paragraphs.Last.Range;
//定义该插入的图片是否为外部链接
Object linkToFile = false; //默认
//定义要插入的图片是否随Word 文档一起保存
Object saveWithDocument = true; //默认
//使用InlineShapes.AddPicture方法插入图片
wordDoc.InlineShapes.AddPicture(filename, ref linkToFile, ref saveWithDocument, ref range);
object format = MSWord.WdSaveFormat.wdFormatDocument; //将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.Close (ref Nothing, ref Nothing, ref Nothing); //关闭wordApp组件对象
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
}
}
需要下载几个DLL文件
然后才能引用
具体方案可以去百度
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Office;
using MSWord = Microsoft.Office.Interop.Word;
using System.IO;
using System.Reflection;
namespace word
{
public partial class Form1 : Form
{
public Form1( )
{
InitializeComponent ();
}
[DllImport ("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(
IntPtr parentHandle,
IntPtr childAfter,
string className,
string windowTitle);
[DllImport ("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(
string lpClassName,
string lpWindowName
);
[DllImport ("user32.dll", EntryPoint = "GetWindowText")]
public static extern int GetWindowText(
IntPtr hwnd,
StringBuilder lpString,
int cch
);
[DllImport ("USER32.DLL", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SendMessage(
IntPtr hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
[MarshalAs (UnmanagedType.LPTStr)]StringBuilder lParam // second message parameter
);
const int WM_GETTEXT = 0x000D;
private void button1_Click(object sender, EventArgs e)
{
IntPtr hwnd = FindWindow ("TF_FpShow", "发票显示");
IntPtr c = FindWindowEx (hwnd, IntPtr.Zero, "TScrollBox", null);
IntPtr c1 = FindWindowEx (c, IntPtr.Zero, "TGroupBox", null);
IntPtr a = FindWindowEx (c1, IntPtr.Zero, null, null);
IntPtr temp = a;
int count=0;
string[] table=new string[100];
for (int i = 0; i < 100; i++)
{
IntPtr b = FindWindowEx (c1, a, "TEdit", null);
StringBuilder ret = new StringBuilder (256);
SendMessage (a, WM_GETTEXT, 100, ret);
table[i] = ret.ToString ();
a = b;
count++;
if (b == temp)
{
count--;
break;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
object path; //文件路径变量
string strContent; //文本内容变量
MSWord.Application wordApp; //Word应用程序变量
MSWord.Document wordDoc;
path = @"C:\MyWord.doc"; //路径
wordApp = new MSWord.ApplicationClass(); //初始化
//如果已存在,则删除
if (File.Exists((string)path))
{
File.Delete((string)path);
}
Object Nothing = Missing.Value;
wordDoc = wordApp.Documents.Add (ref Nothing, ref Nothing, ref Nothing, ref Nothing); //WdSaveFormat为Word文档的保存格式
//图片文件的路径
string filename = @"C:\BackgroundImage.jpg";
//要向Word文档中插入图片的位置
Object range = wordDoc.Paragraphs.Last.Range;
//定义该插入的图片是否为外部链接
Object linkToFile = false; //默认
//定义要插入的图片是否随Word 文档一起保存
Object saveWithDocument = true; //默认
//使用InlineShapes.AddPicture方法插入图片
wordDoc.InlineShapes.AddPicture(filename, ref linkToFile, ref saveWithDocument, ref range);
object format = MSWord.WdSaveFormat.wdFormatDocument; //将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.Close (ref Nothing, ref Nothing, ref Nothing); //关闭wordApp组件对象
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
}
}
需要下载几个DLL文件
然后才能引用
具体方案可以去百度
相关文章推荐
- C# 操作office(word、excel)com异常问题
- office操作笔记:c#与excel之helloword
- C#的office操作[word&Excel]
- C#操作office方面的总结 Excel和Word
- C#操作office方面的总结 Excel和Word
- C#的office操作[word&amp;Excel]
- C#的office操作[word&Excel]
- C#操作office方面的总结 Excel和Word
- C#操作office进行Excel图表创建,保存本地,word获取
- C#的Office操作(Word&Excel)
- C#操作office组件(excel、word、ppt)报错问题!
- C#操作Excel,调用ApplicationClass.Quit()关闭Excel时,发生异常:Microsoft Office Word 遇到问题需要关闭
- C#操作office进行Excel图表创建,保存本地,word获取
- C#的office操作[word&Excel]
- C#操作office方面的总结 Excel和Word
- 在应用程序上实现OFFICE相关的WORD EXCEL导入导出操作
- Web服务中调用office pia 操作WORD,EXCEL时需做的设置
- 关于C#操作EXCEL,生成图表的全面应用之一(利用Microsoft.Office.Interop.Excel)
- C#操作Word文档(Office 2007)
- C#操作Word/excel文档