ReadExcelFiles
//讀文件源代碼;
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;
namespace CC
{
public class ReadExcel
{
private string FileName;
private string FilsPath;
/// <summary>
/// 構造函數文件所在路徑
/// </summary>
/// <param name="Path">文件路徑(Execl文件為完整的路徑)</param>
public ReadExcel(string Path)//構造
{
FileName = Path;
//FilsPath = Files;
}
/// <summary>
/// 構造函數:文件所在路徑
/// </summary>
/// <param name="Path">文件路徑(為Csv文件所在的文件夾路徑)</param>
/// <param name="Files">讀Csv文件時的文件名</param>
public ReadExcel(string Path, string Files)//構造
{
FileName = Path;
FilsPath = Files;
}
OleDbConnection OleCon = new OleDbConnection();
OleDbCommand OleCmd = new OleDbCommand();
OleDbDataAdapter OleDa = new OleDbDataAdapter();
#region 讀取Execl文件返回DataSet
/// <summary>
/// 讀取Execl文件返回DataSet
/// </summary>
/// <returns>返回結果集</returns>
public DataSet Execl()
{
DataSet Myds = new DataSet();
OleCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+FileName+";Extended Properties=Excel 8.0;";
OleCon.Open();
OleCmd.CommandText="select * from [sheet1$]";
OleCmd.Connection = OleCon;
OleDa.SelectCommand = OleCmd;
try
{
OleDa.Fill(Myds, "Execl");
return Myds;
}
catch
{
return Myds;
}
finally
{
OleCon.Close();
OleCmd.Dispose();
OleDa.Dispose();
OleCon.Dispose();
}
}
#endregion
#region 讀取Csv文件返回DataSet
/// <summary>
/// 讀取Csv文件返回DataSet
/// </summary>
/// <returns>Csv內容</returns>
public DataSet Csv()
{
DataSet CsvData = new DataSet();
OleCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=Text;FMT=Delimited;HDR=YES;";
OleCon.Open();
OleCmd.Connection = OleCon;
OleCmd.CommandText = "select * From " + FilsPath ;
OleDa.SelectCommand=OleCmd;
try
{
OleDa.Fill(CsvData, "Csv");
return CsvData;
}
catch
{
return CsvData;
}
finally
{
OleCon.Close();
OleCmd.Dispose();
OleDa.Dispose();
&