C#读取Excel中的数据的类
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.IO;
using System.Text;
using WirelessPlatform.Models;
namespace WirelessPlatform.Utility
{
public class ExcelExtension
{
//private static readonly string _Conn = "Provider=Microsoft.ACE.OleDb.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=YES'"; //Office 2007
private static readonly string _Conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'"; //Office 2003
private static readonly string _Sql_Select_SaleInfo = "Select * From [接收短信记录$]";
public static IList<XlsEntitySaleInfo> GetSaleInfoEntitys(string xlsFile)
{
IList<XlsEntitySaleInfo> list = new List<XlsEntitySaleInfo>();
using (OleDbConnection conn = new OleDbConnection(string.Format(_Conn, Path.GetFullPath(xlsFile))))
{
OleDbCommand cmd = new OleDbCommand(_Sql_Select_SaleInfo, conn);
conn.Open();
OleDbDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
//将rdr中的数据写入数据库或显示出来
}
}
return list;
}
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步