在软件开发的过程中,经常用到从excel表格中读取数据作为数据源,以下整理了一个有效的读取excel表格的方法。
DataTable GetDataTable(string tableName,string leftTopCel,string rightbutCel)
{
bool hasTitle = false;
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "Excel(*.xlsx;*.xls)|*.xlsx;*.xls|所有文件(*.*)|*.*";
openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
openFile.Multiselect = false;
if (openFile.ShowDialog() == DialogResult.Cancel) return null;
var filePath = openFile.FileName;
string fileType = System.IO.Path.GetExtension(filePath);
if (string.IsNullOrEmpty(fileType)) return null;
using (DataSet ds = new DataSet())
{
string strCon = string.Format("Provider=Microsoft.Jet.OLEDB.{0}.0;" +