是否有免费或开源的库可以直接从c#程序中读取Excel文件(.xls) ?

它不需要太花哨,只需选择一个工作表并将数据作为字符串读取即可。到目前为止,我一直在使用Excel的Export to Unicode文本功能,并解析生成的(以制表符分隔的)文件,但我想消除手动步骤。


当前回答

如果只是包含在Excel文件中的简单数据,则可以通过ADO.NET读取数据。请看下面列出的连接字符串:

http://www.connectionstrings.com/?carrier=excel2007 或 http://www.connectionstrings.com/?carrier=excel

更新:然后你可以读取工作表通过选择*从[Sheet1$]

其他回答

刚刚做了一个需要管理一些excel文件的快速演示项目。GemBox软件的. net组件足以满足我的需求。它有一个免费版本,但有一些限制。

http://www.gemboxsoftware.com/GBSpreadsheet.htm

我们在相当大的系统中使用ClosedXML。

免费的 安装方便 直接编码 非常灵敏的支持 开发团队对新的建议非常开放。通常情况下,新特性和bug修复会在同一周内实现

Koogra是一个用c#编写的开源组件,可以读写Excel文件。

SpreadsheetGear for . net是一个Excel兼容的电子表格组件。您可以在我们产品页面的右侧看到我们的客户对性能的评价。您可以自己尝试使用免费的、功能齐全的评估。

这是我在Excel 2003中使用的:

Dictionary<string, string> props = new Dictionary<string, string>();
props["Provider"] = "Microsoft.Jet.OLEDB.4.0";
props["Data Source"] = repFile;
props["Extended Properties"] = "Excel 8.0";

StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<string, string> prop in props)
{
    sb.Append(prop.Key);
    sb.Append('=');
    sb.Append(prop.Value);
    sb.Append(';');
}
string properties = sb.ToString();

using (OleDbConnection conn = new OleDbConnection(properties))
{
    conn.Open();
    DataSet ds = new DataSet();
    string columns = String.Join(",", columnNames.ToArray());
    using (OleDbDataAdapter da = new OleDbDataAdapter(
        "SELECT " + columns + " FROM [" + worksheet + "$]", conn))
    {
        DataTable dt = new DataTable(tableName);
        da.Fill(dt);
        ds.Tables.Add(dt);
    }
}