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

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


当前回答

ADO。NET方法是快速和简单的,但它有一些您应该注意的怪癖,特别是关于如何处理DataTypes。

这篇优秀的文章将帮助你避免一些常见的陷阱: http://blog.lab49.com/archives/196

其他回答

虽然迟到了,但我是LinqToExcel的粉丝

我知道人们为了这个目的一直在做一个Excel“扩展”。 你或多或少在Excel中创建一个按钮,上面写着“导出到程序X”,然后以程序可以读取的格式导出并发送数据。

http://msdn.microsoft.com/en-us/library/ms186213.aspx应该是一个很好的开始。

祝你好运

接受。io电子表格将为您做这个工作,并且不收费。看看这个。

Excel软件包是一个开源的(GPL)组件,用于读取/写入Excel 2007文件。我在一个小项目中使用了它,它的API很简单。只能使用XLSX (Excel 200&),不能使用XLS。

源代码似乎组织良好,易于处理(如果您像我一样需要扩展功能或修复小问题)。

起初,我尝试了ADO。Net (Excel连接字符串)方法,但它充满了讨厌的黑客——例如,如果第二行包含一个数字,它将为下面的列中的所有字段返回整数,并悄悄删除任何不匹配的数据。

以下是几年前我用。net 1.1用c#编写的一些代码。不确定这是否正是你所需要的(可能不是我最好的代码:))。

using System;
using System.Data;
using System.Data.OleDb;

namespace ExportExcelToAccess
{
    /// <summary>
    /// Summary description for ExcelHelper.
    /// </summary>
    public sealed class ExcelHelper
    {
        private const string CONNECTION_STRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=<FILENAME>;Extended Properties=\"Excel 8.0;HDR=Yes;\";";

        public static DataTable GetDataTableFromExcelFile(string fullFileName, ref string sheetName)
        {
            OleDbConnection objConnection = new OleDbConnection();
            objConnection = new OleDbConnection(CONNECTION_STRING.Replace("<FILENAME>", fullFileName));
            DataSet dsImport = new DataSet();

            try
            {
                objConnection.Open();

                DataTable dtSchema = objConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

                if( (null == dtSchema) || ( dtSchema.Rows.Count <= 0 ) )
                {
                    //raise exception if needed
                }

                if( (null != sheetName) && (0 != sheetName.Length))
                {
                    if( !CheckIfSheetNameExists(sheetName, dtSchema) )
                    {
                        //raise exception if needed
                    }
                }
                else
                {
                    //Reading the first sheet name from the Excel file.
                    sheetName = dtSchema.Rows[0]["TABLE_NAME"].ToString();
                }

                new OleDbDataAdapter("SELECT * FROM [" + sheetName + "]", objConnection ).Fill(dsImport);
            }
            catch (Exception)
            {
                //raise exception if needed
            }
            finally
            {
                // Clean up.
                if(objConnection != null)
                {
                    objConnection.Close();
                    objConnection.Dispose();
                }
            }


            return dsImport.Tables[0];
            #region Commented code for importing data from CSV file.
            //              string strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source=" + System.IO.Path.GetDirectoryName(fullFileName) +";" +"Extended Properties=\"Text;HDR=YES;FMT=Delimited\"";
            //
            //              System.Data.OleDb.OleDbConnection conText = new System.Data.OleDb.OleDbConnection(strConnectionString);
            //              new System.Data.OleDb.OleDbDataAdapter("SELECT * FROM " + System.IO.Path.GetFileName(fullFileName).Replace(".", "#"), conText).Fill(dsImport);
            //              return dsImport.Tables[0];

            #endregion
        }

        /// <summary>
        /// This method checks if the user entered sheetName exists in the Schema Table
        /// </summary>
        /// <param name="sheetName">Sheet name to be verified</param>
        /// <param name="dtSchema">schema table </param>
        private static bool CheckIfSheetNameExists(string sheetName, DataTable dtSchema)
        {
            foreach(DataRow dataRow in dtSchema.Rows)
            {
                if( sheetName == dataRow["TABLE_NAME"].ToString() )
                {
                    return true;
                }   
            }
            return false;
        }
    }
}