• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

wayde


博客园    首页    新随笔    联系   管理    订阅  订阅
记录 excel 处理.
Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using System.Reflection;
 5 using System.Threading;
 6 using Microsoft.Office.Interop.Excel;
 7 
 8 namespace excelApp {
 9     class Program {
10         //private object missing = Missing.Value;
11         static void Main(string[] args) {
12             Type _excelAppType = null;
13             object _excel = null;
14             object _documents = null;
15             object books = null;
16             object book = null;
17             try {
18                 _excelAppType = Type.GetTypeFromProgID("Excel.Application",true); 
19                 _excel = Activator.CreateInstance(_excelAppType, false);
20                 //_documents = _excelAppType.InvokeMember("Workbooks",
21                 //   BindingFlags.GetProperty, null, _excel, null);
22 
23                 books = _excelAppType.InvokeMember("Workbooks",
24                     BindingFlags.GetProperty, null, _excel, null);
25 
26                 //book = books.GetType().InvokeMember("Add",BindingFlags.GetProperty, null, books, null);
27 
28                 //_documents = book.GetType().InvokeMember("Worksheets", BindingFlags.GetProperty, null, book, null);
29             } catch (Exception er) {
30                 string ss = er.Message;
31             }
32 
33             object result = null;//_excelAppType
34 
35             object[] argsForOpen = new object[3];
36             argsForOpen[0] = @"D:\temp\3012201701100072008052920080529.xls";
37             argsForOpen[1] = true;
38             argsForOpen[2] = true;
39 
40             //result = books.GetType().InvokeMember("Open",
41             //    System.Reflection.BindingFlags.InvokeMethod, null, books, new Object[] { @"D:\temp\EA000120081010.xml", true, true });
42             result = books.GetType().InvokeMember("Open",
43                 System.Reflection.BindingFlags.InvokeMethod, null, books, argsForOpen);
44 
45 
46             _documents = result.GetType().InvokeMember("Worksheets", BindingFlags.GetProperty, null, result, null);
47 
48             object objSheet;
49             objSheet = _documents.GetType().InvokeMember("Item", BindingFlags.GetProperty, null, _documents, new object[1] { 1 });
50 
51             objSheet.GetType().InvokeMember("Protect", BindingFlags.InvokeMethod, null, objSheet, new object[1] { Guid.NewGuid().ToString() });
52 
53             //
54             _excel.GetType().InvokeMember("DisplayAlerts", BindingFlags.SetProperty, null, _excel, new object[1] { false });
55 
56             //
57             result.GetType().InvokeMember("SaveCopyAs", BindingFlags.InvokeMethod, null, result, new Object[1] { @"D:\temp\EA0001888.xls" });
58 
59             //
60             _excel.GetType().InvokeMember("Quit", BindingFlags.InvokeMethod, null, _excel, null);
61 
62 
63             //test();
64             Console.WriteLine("ok");
65             Console.Read();
66 
67         }
68     }
69 }
posted on 2008-10-23 16:39  wayde  阅读(243)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3