登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

秒大刀 博客

好好学习 天天向上

 
 
 

日志

 
 
 
 

Use Excel2007 as DB  

2008-12-11 12:10:17|  分类: C# |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

    虽然Office2007的文件格式是表转的zip+xml,但手工写解析仍然是件非常痛苦的事情。因暂时还没有找到好用的Office2007的解析器,就仿照Excel2003将Excel当做OLE-DB来使用。

    平台:VS2008 Excel2007

    操作代码如下:

using System;

namespace CsConsole
{
class Program
{
const string ConnectionStringFormat = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES\"";
const string FileName = @"C:\Documents and Settings\Administrator\桌面\test.xlsx";
static void Main(string[] args)
{
System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(string.Format(ConnectionStringFormat, FileName));
connection.Open();
System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand("select count(*) from [Sheet1$]", connection);
object ret = command.ExecuteScalar();
connection.Close();
Console.WriteLine(ret);
}
}
}

    链接字符串从http://www.connectionstrings.com/excel-2007获取,强烈推荐该站点!

    如果哪位朋友有好用的Office2007的解析组件,请分享一下,谢谢!


推荐阅读:
2011-12-21
以Linq方式访问Excel:


2011-12-23
    Lightweight and fast library written in C# for reading Microsoft Excel files ('97-2007).
    Cross-platform:
  • Windows with .Net Framework 2
  • Windows Mobile with Compact Framework
  • Linux, OS X, BSD with Mono 2+


2011-12-23
    若要同时处理Excel97-2003以及Excel2007的各种形式,推荐使用如下的连接字符串(C#):

/// <summary>得到Excel的数据库连接字符串,适用于所有Excel表格格式</summary>
/// <remarks> ref: http://www.connectionstrings.com/excel-2007 </remarks>
static string GetDbConnectionString(string fileName)
{
return string.Format(
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"{0}\";" +
"Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"", fileName);
}

    从Excel中读取数据的SQL拼接方式为:

static string GetSqlSelect(string sheetName, IEnumerable<string> columnNames)
{
return
"SELECT " + string.Join(", ", from c in columnNames select '[' + c + ']') +
" FROM [" + sheetName + "$]"; // Excel以OleDb方式打开时,每个表单名会以字符'$'结尾
}

    从Excel中得到所有表单名的方法为:

/// <summary>得到Excel表中所有的表单名</summary>
/// <remarks> ref: http://www.cnblogs.com/dachie/archive/2010/05/17/1737497.html </remarks>
static IEnumerable<string> GetSheetNames(OleDbConnection connection)
{
var schema = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new object[] { null, null, null, "Table" });
return
from DataRow item in schema.Rows
let name = item["TABLE_NAME"].ToString()
select name.Remove(name.Length - 1); // 移除表单名后面的'$'
}

    从Excel中得到给定表单中所有列名的方法为:

/// <summary>得到Excel指定表单中所有的列名称</summary>
/// <remarks> ref: http://www.cnblogs.com/dachie/archive/2010/05/17/1737497.html </remarks>
static IEnumerable<string> GetColumnNames(OleDbConnection connection, string sheetName)
{
var schema = connection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Columns,
new object[] { null, null, sheetName + '$', null });
return
from DataRow item in schema.Rows
let name = item["COLUMN_NAME"].ToString()
select name;
}


2011-12-23
    当某格数据长度超过255字符时,可能会出现读出阶段问题,具体参见Jet Engine - 255 character truncation。可以按照数据被截断为 255 个字符与 Excel ODBC 驱动程序(MSDN)所给方案对注册表TypeGuessRows字段进行修改。

参考:
C#操作Excel文件(读取Excel,写入Excel)
2013-04-13
    所有采用VSTO的项目,需确保强制采用x86的CPU而非AnyCPUUse Excel2007 as DB - 秒大刀 - 秒大刀 博客。否则在64位操作系统上会不稳定,因为操作系统上安装的Office是32位的。

2013-5-23
    Open XML SDK 也可以用来进行Office2007+文件的读写
  评论这张
 
阅读(1392)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018