C#如何读写Excell?

2023-05-14 综合 20阅读
using System.Data.OleDb;
/// <summary>
/// 读取数据源,不陪举用COM组件
/// </summary>
/// <param name="filenameurl"></param>
/// <param name="table"></param>
/// <昌乱拍returns></耐羡returns>
public static DataSet ExecleDs(string filenameurl, string table)
{
DataSet ds = new DataSet();

try
{
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
int m=schemaTable.Rows.Count;
string tableName = schemaTable.Rows[0][2].ToString().Trim();
OleDbDataAdapter odda = new OleDbDataAdapter("select * from [" + tableName + "]", conn);
odda.Fill(ds, table);
conn.Close();

}
catch (Exception ex)
{
//Response.Write(ex.ToString());
int i=0;
}
return ds;

}
//table这个参数可以不要,代码中用"[Sheet1$]"代替
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com