熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#中導入導出Excel的操作

2013-11-13 10:15:45  來源: .NET編程 

/// <summary>
/// 讀取Excel文檔
/// </summary>
/// <param name=Path>文件名稱</param>
/// <returns>返回一個數據集</returns>
public DataSet ExcelToDS(string Path)
{
string strConn = Provider=MicrosoftJetOLEDB; +Data Source=+ Path +;+Extended Properties=Excel ;;
OleDbConnection conn = new OleDbConnection(strConn);
connOpen();
string strExcel = ;
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel=select * from [sheet$];
myCommand = new OleDbDataAdapter(strExcel strConn);
ds = new DataSet();
myCommandFill(dstable);
return ds;
}

/// <summary>
/// 寫入Excel文檔
/// </summary>
/// <param name=Path>文件名稱</param>
public bool SaveFPtoExcel(string Path)
{
try
{
string strConn = Provider=MicrosoftJetOLEDB; +Data Source=+ Path +;+Extended Properties=Excel ;;
OleDbConnection conn = new OleDbConnection(strConn);
connOpen();
SystemDataOleDbOleDbCommand cmd=new OleDbCommand ();
cmdConnection =conn;
//cmdCommandText =UPDATE [sheet$] SET 姓名= WHERE 工號=日期;
//cmdExecuteNonQuery ();
for(int i=;i<fpSheets []RowCount ;i++)
{
if(fpSheets []Cells[i]Text!=)
{
cmdCommandText =INSERT INTO [sheet$] (工號姓名部門職務日期時間) VALUES(+fpSheets []Cells[i]Text+ +
fpSheets []Cells[i]Text++fpSheets []Cells[i]Text++fpSheets []Cells[i]Text+
+fpSheets []Cells[i]Text++fpSheets []Cells[i]Text+);
cmdExecuteNonQuery ();
}
}
connClose ();
return true;
}
catch(SystemDataOleDbOleDbException ex)
{
SystemDiagnosticsDebugWriteLine (寫入Excel發生錯誤+exMessage );
}
return false;
}
From:http://tw.wingwit.com/Article/program/net/201311/13097.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.