博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#后期绑定操作Excel
阅读量:6174 次
发布时间:2019-06-21

本文共 3725 字,大约阅读时间需要 12 分钟。

后期绑定的好处是:对于不同版本的软件都能支持。如果机器A上安装的Office 2003,机器B上安装Office 2007,没问题,下面代码都能正常运行。但是如果采用前期绑定就不行了。关于“前期绑定和后期绑定”,“Excel专业开发”一书的3.3.3节(43页)说的非常清楚。


InBlock.gif

                
private 
void Test()

InBlock.gif                {

InBlock.gif                        DataTable table = 
new DataTable();

InBlock.gif                        table.Columns.Add(
"ID", System.Type.GetType(
"System.Int32"));

InBlock.gif                        table.Columns.Add(
"Name", System.Type.GetType(
"System.String"));

InBlock.gif                        
for (
int i = 0; i < 22; i++)

InBlock.gif                        {

InBlock.gif                                DataRow row = table.NewRow();

InBlock.gif                                row[
"ID"] = i;

InBlock.gif                                row[
"Name"] = 
"name" + i;

InBlock.gif                                table.Rows.Add(row);

InBlock.gif                        }

InBlock.gif                        
try

InBlock.gif                        {

InBlock.gif                                
this.Cursor = Cursors.AppStarting;

InBlock.gif                                
//例如在中文系统下安装的是英文的Office,就需要指定CultureInfo为en-US

InBlock.gif                                ExportToExcel(table, 
new System.Globalization.CultureInfo(
"en-US"));

InBlock.gif                                
this.Cursor = Cursors.Default;

InBlock.gif                        }

InBlock.gif                        
catch (Exception ex)

InBlock.gif                        {

InBlock.gif                                MessageBox.Show(
"Exception \n" + ex.Message + 
"\nStack Trace: \n" + ex.StackTrace.ToString(), 
"信息提示",

InBlock.gif                                        MessageBoxButtons.OK, MessageBoxIcon.Information);

InBlock.gif                        }

InBlock.gif                }

InBlock.gif

                
public 
void ExportToExcel(System.Data.DataTable table)

InBlock.gif                {

InBlock.gif                        ExportToExcel(table, System.Globalization.CultureInfo.CurrentCulture);

InBlock.gif                }

InBlock.gif

                
public 
void ExportToExcel(System.Data.DataTable table, System.Globalization.CultureInfo cultureInfoOfOffice)

InBlock.gif                {

InBlock.gif                        
object excel;

InBlock.gif                        
object book;

InBlock.gif                        
object books;

InBlock.gif                        
object sheet;

InBlock.gif                        
object sheets;

InBlock.gif                        
object range;

InBlock.gif                        
object[] parameters;

InBlock.gif                        Type ExcelType;

InBlock.gif

                        parameters = 
new 
object[1];

InBlock.gif                        
//获取Excel类型

InBlock.gif                        ExcelType = Type.GetTypeFromProgID(
"Excel.Application");

InBlock.gif                        excel = Activator.CreateInstance(ExcelType);

InBlock.gif                        
//获取workbooks集合

InBlock.gif                        books = excel.GetType().InvokeMember(
"Workbooks", BindingFlags.GetProperty, 
null, excel, 
null);

InBlock.gif                        
//新增workbook.

InBlock.gif                        
//BUG:自动化 Excel 时出现“格式太旧或是类型库无效”错误

InBlock.gif                        
//http://support.microsoft.com/kb/320369/zh-cn

InBlock.gif                        
//如果满足以下条件,在调用某个 Excel 方法时会收到此错误:

InBlock.gif                        
//* 该方法需要一个 LCID(区域设置标识符)。

InBlock.gif                        
//* 运行的是英语版本的 Excel。但是,计算机的区域设置是针对非英语语言配置的。

InBlock.gif                        
//如果客户端计算机运行的是英语版本的 Excel 并且当前用户的区域设置配置为英语之外的某个语言,则 Excel 将尝试查找针对所配置语言的语言包。

InBlock.gif                        
//如果没有找到所需语言包,则会报告错误。 

InBlock.gif                        book = books.GetType().InvokeMember(
"Add", BindingFlags.InvokeMethod, 
null, books, 
null, cultureInfoOfOffice);

InBlock.gif                        
//获取worksheets集合

InBlock.gif                        sheets = book.GetType().InvokeMember(
"Worksheets", BindingFlags.GetProperty, 
null, book, 
null);

InBlock.gif                        
//获取第一个 worksheet.

InBlock.gif

                        parameters[0] = 1;

InBlock.gif                        sheet = sheets.GetType().InvokeMember(
"Item", BindingFlags.GetProperty, 
null, sheets, parameters);

InBlock.gif                        
//获取A1单元格所在区域

InBlock.gif                        
object[] header = 
new Object[table.Columns.Count];

InBlock.gif                        parameters = 
new 
object[1];

InBlock.gif                        parameters[0] = 
"A1:" + Convert.ToString(Convert.ToChar(64 + table.Columns.Count)) + 
"1";

InBlock.gif                        range = sheet.GetType().InvokeMember(
"Range", BindingFlags.GetProperty, 
null, sheet, parameters);

InBlock.gif                        
//在A1单元格中填充数据.

InBlock.gif                        parameters = 
new 
object[1];

InBlock.gif                        
for (
int i = 0; i < table.Columns.Count; i++)

InBlock.gif                        {

InBlock.gif                                header[i] = table.Columns[i].ToString();

InBlock.gif                        }

InBlock.gif                        parameters[0] = header;

InBlock.gif                        range.GetType().InvokeMember(
"Value", BindingFlags.SetProperty, 
null, range, parameters, cultureInfoOfOffice);

InBlock.gif                        parameters = 
new 
object[2];

InBlock.gif                        parameters[0] = 
"A2:" + Convert.ToString(Convert.ToChar(64 + table.Columns.Count)) + (table.Rows.Count + 1).ToString().Trim();

InBlock.gif                        parameters[1] = Missing.Value;

InBlock.gif                        range = sheet.GetType().InvokeMember(
"Range", BindingFlags.GetProperty, 
null, sheet, parameters);

InBlock.gif                        parameters = 
new 
object[1];

InBlock.gif                        Object[,] data = 
new Object[table.Rows.Count, table.Columns.Count];

InBlock.gif                        
for (
int i = 0; i < table.Rows.Count; i++)

InBlock.gif                        {

InBlock.gif                                
for (
int j = 0; j < table.Columns.Count; j++)

InBlock.gif                                {

InBlock.gif                                        data[i, j] = table.Rows[i][j].ToString();

InBlock.gif                                }

InBlock.gif                        }

InBlock.gif                        parameters[0] = data;

InBlock.gif                        range.GetType().InvokeMember(
"Value", BindingFlags.SetProperty, 
null, range, parameters, cultureInfoOfOffice);

InBlock.gif                        parameters = 
new 
object[1];

InBlock.gif                        
//启动 Excel

InBlock.gif                        parameters[0] = 
true;

InBlock.gif                        excel.GetType().InvokeMember(
"Visible", BindingFlags.SetProperty, 
null, excel, parameters);

InBlock.gif                        excel.GetType().InvokeMember(
"UserControl", BindingFlags.SetProperty, 
null, excel, parameters);

InBlock.gif                }

DataTable的使用技巧可参考:

http://www.cnblogs.com/dreamof/archive/2008/07/31/1257660.html
本文转自 h2appy  51CTO博客,原文链接:http://blog.51cto.com/h2appy/240830,如需转载请自行联系原作者
你可能感兴趣的文章
一不小心把win10的秘钥卸载了解决方法
查看>>
Linux实现删除撤回的方法。
查看>>
SilverLight之向后台请求数据-WebClient
查看>>
HDU Problem 1260 Tickets 【dp】
查看>>
STL map容器常用API
查看>>
队列的顺序存储---顺序队列
查看>>
Delphi 读取 c# webservice XML的base64编码图片字符串转化图片并显示
查看>>
第三天
查看>>
connector for python
查看>>
等价类划分的应用
查看>>
Web Service(下)
查看>>
trigger()
查看>>
nvm 怎么安装 ?
查看>>
Java VM里的magic
查看>>
[Node.js]Domain模块
查看>>
Linux操作系统文档
查看>>
利用Tensorflow训练自定义数据
查看>>
c++官方文档-枚举-联合体-结构体-typedef-using
查看>>
[题解]UVA11029 Leading and Trailing
查看>>
利用vue-gird-layout 制作可定制桌面 (一)
查看>>