您的位置:首页 > 编程语言 > C#

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文件
然后才能引用

具体方案可以去百度
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: