是否有免费或开源的库可以直接从c#程序中读取Excel文件(.xls) ?
它不需要太花哨,只需选择一个工作表并将数据作为字符串读取即可。到目前为止,我一直在使用Excel的Export to Unicode文本功能,并解析生成的(以制表符分隔的)文件,但我想消除手动步骤。
是否有免费或开源的库可以直接从c#程序中读取Excel文件(.xls) ?
它不需要太花哨,只需选择一个工作表并将数据作为字符串读取即可。到目前为止,我一直在使用Excel的Export to Unicode文本功能,并解析生成的(以制表符分隔的)文件,但我想消除手动步骤。
当前回答
SmartXLS是另一个excel电子表格组件,它支持excel图表、公式引擎的大部分功能,并且可以读写excel2007 openxml格式。
其他回答
如果在同一个工作表中有多个表,可以给每个表一个对象名称,并使用OleDb方法读取表,如下所示:http://vbktech.wordpress.com/2011/05/10/c-net-reading-and-writing-to-multiple-tables-in-the-same-microsoft-excel-worksheet/
如果只是包含在Excel文件中的简单数据,则可以通过ADO.NET读取数据。请看下面列出的连接字符串:
http://www.connectionstrings.com/?carrier=excel2007 或 http://www.connectionstrings.com/?carrier=excel
对
更新:然后你可以读取工作表通过选择*从[Sheet1$]
以下是几年前我用。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;
}
}
}
我们使用的解决方案需要:
允许读取/写入Excel生成的文件 性能要快(不像使用com那样) 独立于MS Office(需要在客户端没有安装MS Office的情况下也能使用) 免费或开源(但要积极开发)
有几种选择,但我们发现NPoi(。NET端口的Java长期存在的Poi开源项目)是最好的: http://npoi.codeplex.com/
它还允许使用.doc和.ppt文件格式
SpreadsheetGear for . net是一个Excel兼容的电子表格组件。您可以在我们产品页面的右侧看到我们的客户对性能的评价。您可以自己尝试使用免费的、功能齐全的评估。