简体   繁体   English

从 C# 读取 Excel 文件

[英]Reading Excel files from C#

Is there a free or open source library to read Excel files (.xls) directly from a C# program?是否有免费或开源库可以直接从 C# 程序读取 Excel 文件 (.xls)?

It does not need to be too fancy, just to select a worksheet and read the data as strings.不需要太花哨,只需将 select 一个工作表读取为字符串即可。 So far, I've been using Export to Unicode text function of Excel, and parsing the resulting (tab-delimited) file, but I'd like to eliminate the manual step.到目前为止,我一直在使用 Export to Unicode text function of Excel,并将生成的(制表符分隔的)文件解析为手动步骤。

var fileName = string.Format("{0}\\fileNameHere", Directory.GetCurrentDirectory());
var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", fileName);

var adapter = new OleDbDataAdapter("SELECT * FROM [workSheetNameHere$]", connectionString);
var ds = new DataSet();

adapter.Fill(ds, "anyNameHere");

DataTable data = ds.Tables["anyNameHere"];

This is what I usually use.这是我通常使用的。 It is a little different because I usually stick a AsEnumerable() at the edit of the tables:这有点不同,因为我通常在编辑表格时使用 AsEnumerable():

var data = ds.Tables["anyNameHere"].AsEnumerable();

as this lets me use LINQ to search and build structs from the fields.因为这让我可以使用 LINQ 从字段中搜索和构建结构。

var query = data.Where(x => x.Field<string>("phoneNumber") != string.Empty).Select(x =>
                new MyContact
                    {
                        firstName= x.Field<string>("First Name"),
                        lastName = x.Field<string>("Last Name"),
                        phoneNumber =x.Field<string>("Phone Number"),
                    });

If it is just simple data contained in the Excel file you can read the data via ADO.NET.如果只是 Excel 文件中包含的简单数据,您可以通过 ADO.NET 读取数据。 See the connection strings listed here:请参阅此处列出的连接字符串:

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

-Ryan -瑞安

Update: then you can just read the worksheet via something like select * from [Sheet1$]更新:然后您可以通过select * from [Sheet1$]类的方式阅读工作表

The ADO.NET approach is quick and easy, but it has a few quirks which you should be aware of, especially regarding how DataTypes are handled. ADO.NET 方法既快速又简单,但它有一些您应该注意的怪癖,尤其是关于如何处理数据类型。

This excellent article will help you avoid some common pitfalls: http://blog.lab49.com/archives/196这篇优秀的文章将帮助您避免一些常见的陷阱: http : //blog.lab49.com/archives/196

This is what I used for Excel 2003:这是我在 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);
    }
}

How about Excel Data Reader? Excel数据阅读器怎么样?

http://exceldatareader.codeplex.com/ http://exceldatareader.codeplex.com/

I've used in it anger, in a production environment, to pull large amounts of data from a variety of Excel files into SQL Server Compact.我曾经在生产环境中使用它从各种 Excel 文件中提取大量数据到 SQL Server Compact 中。 It works very well and it's rather robust.它工作得很好,而且相当健壮。

Here's some code I wrote in C# using .NET 1.1 a few years ago.这是我几年前使用 .NET 1.1 用 C# 编写的一些代码。 Not sure if this would be exactly what you need (and may not be my best code :)).不确定这是否正是您所需要的(并且可能不是我最好的代码:))。

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;
        }
    }
}

Koogra是一个用 C# 编写的开源组件,用于读取和写入 Excel 文件。

虽然您确实特别要求 .xls,暗示较旧的文件格式,但对于 OpenXML 格式(例如 xlsx),我强烈推荐 OpenXML SDK( http://msdn.microsoft.com/en-us/library/bb448854.aspx

I did a lot of reading from Excel files in C# a while ago, and we used two approaches:前段时间我在 C# 中阅读了大量 Excel 文件,我们使用了两种方法:

  • The COM API, where you access Excel's objects directly and manipulate them through methods and properties COM API,您可以在其中直接访问 Excel 的对象并通过方法和属性对其进行操作
  • The ODBC driver that allows to use Excel like a database.允许像使用数据库一样使用 Excel 的 ODBC 驱动程序。

The latter approach was much faster: reading a big table with 20 columns and 200 lines would take 30 seconds via COM, and half a second via ODBC.后一种方法快得多:通过 COM 读取一个包含 20 列和 200 行的大表需要 30 秒,通过 ODBC 需要半秒。 So I would recommend the database approach if all you need is the data.因此,如果您只需要数据,我会推荐数据库方法。

Cheers,干杯,

Carl卡尔

ExcelMapper is an open source tool ( http://code.google.com/p/excelmapper/ ) that can be used to read Excel worksheets as Strongly Typed Objects. ExcelMapper 是一个开源工具 ( http://code.google.com/p/excelmapper/ ),可用于将 Excel 工作表作为强类型对象读取。 It supports both xls and xlsx formats.它支持 xls 和 xlsx 格式。

I want to show a simple method to read xls/xlsx file with .NET.我想展示一种使用 .NET 读取 xls/xlsx 文件的简单方法。 I hope that the following will be helpful for you.我希望以下内容对您有所帮助。

private DataTable ReadExcelToTable(string path)    
 {

     //Connection String

     string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";  
     //the same name 
     //string connstring = Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + path + //";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; 

     using(OleDbConnection conn = new OleDbConnection(connstring))
     {
        conn.Open();
        //Get All Sheets Name
        DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[]{null,null,null,"Table"});  

        //Get the First Sheet Name
        string firstSheetName = sheetsName.Rows[0][2].ToString(); 

        //Query String 
        string sql = string.Format("SELECT * FROM [{0}]",firstSheetName); 
        OleDbDataAdapter ada =new OleDbDataAdapter(sql,connstring);
        DataSet set = new DataSet();
        ada.Fill(set);
        return set.Tables[0];   
   }
 }

Code is from article: http://www.c-sharpcorner.com/uploadfile/d2dcfc/read-excel-file-with-net/ .代码来自文章: http : //www.c-sharpcorner.com/uploadfile/d2dcfc/read-excel-file-with-net/ You can get more details from it.您可以从中获得更多详细信息。

Not free, but with the latest Office there's a very nice automation .Net API.不是免费的,但是最新的 Office 有一个非常好的自动化 .Net API。 (there has been an API for a long while but was nasty COM) You can do everything you want / need in code all while the Office app remains a hidden background process. (已经有一个 API 很长一段时间了,但是很讨厌 COM)你可以在代码中做你想要/需要的一切,而 Office 应用程序仍然是一个隐藏的后台进程。

SmartXLS是另一个excel电子表格组件,支持excel图表、公式引擎的大部分功能,可以读写excel2007 openxml格式。

如果我不在基地,请原谅我,但这不是办公室 PIA 的用途吗?

最近,部分是为了在 LINQ 方面变得更好......我一直在使用 Excel 的自动化 API 将文件保存为 XML 电子表格,然后使用 LINQ to XML 处理该文件。

SpreadsheetGear for .NET is an Excel compatible spreadsheet component for .NET. SpreadsheetGear for .NET适用于 .NET的 Excel 兼容电子表格组件。 You can see what our customers say about performance on the right hand side of our product page .您可以在我们产品页面的右侧看到我们的客户对性能的评价。 You can try it yourself with the free, fully-functional evaluation .您可以通过免费的、功能齐全的评估自行试用

The .NET component Excel Reader .NET may satisfy your requirement. .NET 组件 Excel Reader .NET 可以满足您的要求。 It's good enought for reading XLSX and XLS files.它足以读取 XLSX 和 XLS 文件。 So try it from:所以尝试从:

http://www.devtriogroup.com/ExcelReader http://www.devtriogroup.com/ExcelReader

聚会迟到了,但我是LinqToExcel的粉丝

You can try using this open source solution that makes dealing with Excel a lot more cleaner.您可以尝试使用这个开源解决方案,它可以让处理 Excel 更加清晰。

http://excelwrapperdotnet.codeplex.com/ http://excelwrapperdotnet.codeplex.com/

SpreadsheetGear is awesome. SpreadsheetGear 很棒。 Yes it's an expense, but compared to twiddling with these other solutions, it's worth the cost.是的,这是一笔费用,但与使用这些其他解决方案相比,这是值得的。 It is fast, reliable, very comprehensive, and I have to say after using this product in my fulltime software job for over a year and a half, their customer support is fantastic!它快速、可靠、非常全面,我不得不说在我的全职软件工作中使用这个产品一年半后,他们的客户支持非常棒!

I recommend the FileHelpers Library which is a free and easy to use .NET library to import/export data from EXCEL, fixed length or delimited records in files, strings or streams + More.我推荐 FileHelpers 库,它是一个免费且易于使用的 .NET 库,用于从 EXCEL 导入/导出数据、文件、字符串或流中的固定长度或分隔记录等。

The Excel Data Link Documentation Section http://filehelpers.sourceforge.net/example_exceldatalink.html Excel 数据链接文档部分http://filehelpers.sourceforge.net/example_exceldatalink.html

The solution that we used, needed to:我们使用的解决方案需要:

  • Allow Reading/Writing of Excel produced files允许读取/写入Excel 生成的文件
  • Be Fast in performance (not like using COMs)性能要(不像使用 COM)
  • Be MS Office Independent (needed to be usable without clients having MS Office installed)独立于 MS Office(需要在没有安装 MS Office 的客户端的情况下可用)
  • Be Free or Open Source (but actively developed)免费开源(但积极开发)

There are several choices, but we found NPoi (.NET port of Java's long existing Poi open source project) to be the best: http://npoi.codeplex.com/有多种选择,但我们发现NPoi (Java 长期存在的Poi开源项目的 .NET 端口)是最好的: http : //npoi.codeplex.com/

It also allows working with .doc and .ppt file formats它还允许使用 .doc 和 .ppt 文件格式

If it's just tabular data.如果只是表格数据。 I would recommend file data helpers by Marcos Melli which can be downloaded here .我会推荐 Marcos Melli 的文件数据助手,可以在此处下载。

We use ClosedXML in rather large systems.我们在相当大的系统中使用ClosedXML

  • Free自由
  • Easy to install易于安装
  • Straight forward coding直接编码
  • Very responsive support非常敏感的支持
  • Developer team is extremly open to new suggestions.开发团队非常乐于接受新的建议。 Often new features and bug fixes are implemented within the same week通常在同一周内实施新功能和错误修复

Excel Package is an open-source (GPL) component for reading/writing Excel 2007 files. Excel 包是一个开源 (GPL) 组件,用于读取/写入 Excel 2007 文件。 I used it on a small project, and the API is straightforward.我在一个小项目中使用了它,API 很简单。 Works with XLSX only (Excel 200&), not with XLS.仅适用于 XLSX (Excel 200&),不适用于 XLS。

The source code also seems well-organized and easy to get around (if you need to expand functionality or fix minor issues as I did).源代码似乎也井井有条且易于使用(如果您需要像我一样扩展功能或修复小问题)。

At first, I tried the ADO.Net (Excel connection string) approach, but it was fraught with nasty hacks -- for instance if second row contains a number, it will return ints for all fields in the column below and quietly drop any data that doesn't fit.起初,我尝试了 ADO.Net(Excel 连接字符串)方法,但它充满了令人讨厌的技巧——例如,如果第二行包含一个数字,它将返回下面列中所有字段的整数,并悄悄地删除任何数据那不合适。

you could write an excel spreadsheet that loads a given excel spreadsheet and saves it as csv (rather than doing it manually).您可以编写一个 excel 电子表格来加载给定的 excel 电子表格并将其保存为 csv(而不是手动执行)。

then you could automate that from c#.那么你可以从 c# 自动化。

and once its in csv, the c# program can grok that.一旦它在 csv 中,c# 程序就可以理解了。

(also, if someone asks you to program in excel, it's best to pretend you don't know how) (另外,如果有人让你用excel编程,最好假装你不知道怎么做)

(edit: ah yes, rob and ryan are both right) (编辑:啊,是的,rob 和 ryan 都是对的)

I know that people have been making an Excel "extension" for this purpose.我知道人们为此目的一直在制作 Excel“扩展”。
You more or less make a button in Excel that says "Export to Program X", and then export and send off the data in a format the program can read.您或多或少在 Excel 中创建一个按钮,上面写着“导出到程序 X”,然后以程序可以读取的格式导出和发送数据。

http://msdn.microsoft.com/en-us/library/ms186213.aspx should be a good place to start. http://msdn.microsoft.com/en-us/library/ms186213.aspx应该是一个不错的起点。

Good luck祝你好运

Just did a quick demo project that required managing some excel files.刚刚做了一个需要管理一些excel文件的快速演示项目。 The .NET component from GemBox software was adequate for my needs. GemBox 软件中的 .NET 组件足以满足我的需求。 It has a free version with a few limitations.它有一个有一些限制的免费版本。

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

Take.io Spreadsheet will do this work for you, and at no charge. Take.io Spreadsheet 将为您完成这项工作,并且不收取任何费用。 Just take a look at this .看看这个

I just used ExcelLibrary to load an .xls spreadsheet into a DataSet.我只是使用ExcelLibrary将 .xls 电子表格加载到 DataSet 中。 Worked great for me.对我很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM