在c#中读取microsoft excel文件 [英] Reading microsoft excel files in c#

查看:129
本文介绍了在c#中读取microsoft excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助阅读

Microsoft excel文件

非常感谢

Please help with reading
Microsoft excel file
Thanks alot

推荐答案

这是你的幸运日!

我只是在我的代码中查看我的页面...

It's your lucky day!
I was just looking at teh page in my code where I do just that...
/// <summary>
/// Load a sheet from an Excel file
/// </summary>
/// <param name="path">Path to excel file</param>
/// <param name="columns">Comma separated list of columns to load. If not specified, loads all</param>
/// <param name="sheet">Sheet name to load. If not specified, loads "Sheet1


< / param >
/// < param name =verify > 如果为true,则检查文件和工作表是否存在,如果不存在则抛出异常。默认为true < / param >
/// < 返回 > 检索到的数据表。< / returns >
public static System.Data.DataTable LoadExcel( string path, string columns = * string sheet = Sheet1
"</param> /// <param name="verify">If true, checks that the file and sheet exist, and throws exceptions if not. Defaults to true</param> /// <returns>Table of data retrieved.</returns> public static System.Data.DataTable LoadExcel(string path, string columns = "*", string sheet = "Sheet1


bool verify = true
{
if (verify)
{
if (!File.Exists(path)) throw new IOException( 输入文件不存在: + path);
if (!GetExcelSheetNames(path).Contains(sheet)) throw new ArgumentException( 请求的工作表不存在: +张);
}
if string .IsNullOrWhiteSpace(columns))columns = *;

System.Data.DataTable dt = new System.Data.DataTable();
使用(OleDbConnection con = new OleDbConnection(GetExcelConnectionString(path)))
{
con.Open();
string cmdStr = string .Format( SELECT {0} FROM [{1}],columns,sheet);
使用(OleDbCommand cmd = new OleDbCommand(cmdStr,con))
{
使用(OleDbDataAdapter da = new OleDbDataAdapter(cmd))
{
da.Fill(dt);
}
}
}
return dt;
}
/// < 摘要 >
/// 返回Excel文件中所有工作表名称的列表。
/// < / summary >
/// < param < span class =code-summarycomment> name =path > < / param >
/// < 返回 > < / returns >
public static List< string> GetExcelSheetNames( string path)
{
List< string> sheetNames = new List< string>();
使用(OleDbConnection con = new OleDbConnection(GetExcelConnectionString(path)))
{
con.Open();
使用(DataTable sheet = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null ))
{
foreach (DataRow表 sheets.Rows)
{
if (工作表[ TABLE_NAME]。ToString()。包含(
", bool verify = true) { if (verify) { if (!File.Exists(path)) throw new IOException("The input file does not exist: " + path); if (!GetExcelSheetNames(path).Contains(sheet)) throw new ArgumentException("The requested sheet does not exist: " + sheet); } if (string.IsNullOrWhiteSpace(columns)) columns = "*"; System.Data.DataTable dt = new System.Data.DataTable(); using (OleDbConnection con = new OleDbConnection(GetExcelConnectionString(path))) { con.Open(); string cmdStr = string.Format("SELECT {0} FROM [{1}]", columns, sheet); using (OleDbCommand cmd = new OleDbCommand(cmdStr, con)) { using (OleDbDataAdapter da = new OleDbDataAdapter(cmd)) { da.Fill(dt); } } } return dt; } /// <summary> /// Returns a list of all sheet names in an Excel File. /// </summary> /// <param name="path"></param> /// <returns></returns> public static List<string> GetExcelSheetNames(string path) { List<string> sheetNames = new List<string>(); using (OleDbConnection con = new OleDbConnection(GetExcelConnectionString(path))) { con.Open(); using (DataTable sheets = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null)) { foreach (DataRow sheet in sheets.Rows) { if (sheet["TABLE_NAME"].ToString().Contains("


这篇关于在c#中读取microsoft excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆