private void Test()
{
DataTable table =
new DataTable();
table.Columns.Add(
"ID", System.Type.GetType(
"System.Int32"));
table.Columns.Add(
"Name", System.Type.GetType(
"System.String"));
for (
int i = 0; i < 22; i++)
{
DataRow row = table.NewRow();
row[
"ID"] = i;
row[
"Name"] =
"name" + i;
table.Rows.Add(row);
}
try {
this.Cursor = Cursors.AppStarting;
//例如在中文系统下安装的是英文的Office,就需要指定CultureInfo为en-US ExportToExcel(table,
new System.Globalization.CultureInfo(
"en-US"));
this.Cursor = Cursors.Default;
}
catch (Exception ex)
{
MessageBox.Show(
"Exception \n" + ex.Message +
"\nStack Trace: \n" + ex.StackTrace.ToString(),
"信息提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
public void ExportToExcel(System.Data.DataTable table)
{
ExportToExcel(table, System.Globalization.CultureInfo.CurrentCulture);
}
public void ExportToExcel(System.Data.DataTable table, System.Globalization.CultureInfo cultureInfoOfOffice)
{
object excel;
object book;
object books;
object sheet;
object sheets;
object range;
object[] parameters;
Type ExcelType;
parameters =
new object[1];
//获取Excel类型 ExcelType = Type.GetTypeFromProgID(
"Excel.Application");
excel = Activator.CreateInstance(ExcelType);
//获取workbooks集合 books = excel.GetType().InvokeMember(
"Workbooks", BindingFlags.GetProperty,
null, excel,
null);
//新增workbook. //BUG:自动化 Excel 时出现“格式太旧或是类型库无效”错误 //http://support.microsoft.com/kb/320369/zh-cn //如果满足以下条件,在调用某个 Excel 方法时会收到此错误: //* 该方法需要一个 LCID(区域设置标识符)。 //* 运行的是英语版本的 Excel。但是,计算机的区域设置是针对非英语语言配置的。 //如果客户端计算机运行的是英语版本的 Excel 并且当前用户的区域设置配置为英语之外的某个语言,则 Excel 将尝试查找针对所配置语言的语言包。 //如果没有找到所需语言包,则会报告错误。 book = books.GetType().InvokeMember(
"Add", BindingFlags.InvokeMethod,
null, books,
null, cultureInfoOfOffice);
//获取worksheets集合 sheets = book.GetType().InvokeMember(
"Worksheets", BindingFlags.GetProperty,
null, book,
null);
//获取第一个 worksheet. parameters[0] = 1;
sheet = sheets.GetType().InvokeMember(
"Item", BindingFlags.GetProperty,
null, sheets, parameters);
//获取A1单元格所在区域 object[] header =
new Object[table.Columns.Count];
parameters =
new object[1];
parameters[0] =
"A1:" + Convert.ToString(Convert.ToChar(64 + table.Columns.Count)) +
"1";
range = sheet.GetType().InvokeMember(
"Range", BindingFlags.GetProperty,
null, sheet, parameters);
//在A1单元格中填充数据. parameters =
new object[1];
for (
int i = 0; i < table.Columns.Count; i++)
{
header[i] = table.Columns[i].ToString();
}
parameters[0] = header;
range.GetType().InvokeMember(
"Value", BindingFlags.SetProperty,
null, range, parameters, cultureInfoOfOffice);
parameters =
new object[2];
parameters[0] =
"A2:" + Convert.ToString(Convert.ToChar(64 + table.Columns.Count)) + (table.Rows.Count + 1).ToString().Trim();
parameters[1] = Missing.Value;
range = sheet.GetType().InvokeMember(
"Range", BindingFlags.GetProperty,
null, sheet, parameters);
parameters =
new object[1];
Object[,] data =
new Object[table.Rows.Count, table.Columns.Count];
for (
int i = 0; i < table.Rows.Count; i++)
{
for (
int j = 0; j < table.Columns.Count; j++)
{
data[i, j] = table.Rows[i][j].ToString();
}
}
parameters[0] = data;
range.GetType().InvokeMember(
"Value", BindingFlags.SetProperty,
null, range, parameters, cultureInfoOfOffice);
parameters =
new object[1];
//启动 Excel parameters[0] =
true;
excel.GetType().InvokeMember(
"Visible", BindingFlags.SetProperty,
null, excel, parameters);
excel.GetType().InvokeMember(
"UserControl", BindingFlags.SetProperty,
null, excel, parameters);
}