1,979
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 加载Excel
/// </summary>
/// <param name="filePath">Excel路径</param>
/// <returns></returns>
public static DataTable LoadDataZongBiaoExcel(string tableName)
{
OleDbConnection OleConn = null;
String sql = string.Empty;
DataSet OleDsExcle = new DataSet();
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + tableName + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
try
{
OleConn = new OleDbConnection(strConn);
sql = string.Format("SELECT * FROM {0}",tableName);//可是更改Sheet名称,比如sheet2,等等
OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
OleDaExcel.Fill(OleDsExcle, "Sheet1");// Sheet1 表名,可以随便取一个 return OleDsExcle.Tables[0];
}
catch (Exception ex)
{
//Log.Error(ex.StackTrace + ex.Message);
}
return null;
}
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection ;
Excel.Application xApp = new Excel.ApplicationClass();
xApp.Visible = true;
Excel.Workbook xBook = xApp.Workbooks._Open(this.txtfilepath.Text ,
Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//xBook=xApp.Workbooks.Add(Missing.Value);//新建文件的代码
Excel.Worksheet xSheet = (Excel.Worksheet)xBook.Sheets["SheetA"];
//读取数据,通过Range对象
int CurRow = 5 ;
If( xSheet.get_Range("A" + CurRow.ToString() , Type.Missing).Value2 != null )
{
string unit = xSheet.get_Range("A5" , Type.Missing).Value2.ToString() ;
CurRow ++
}