http://hi.baidu.com/smalltube/blog/item/58640e771d5caf1fb151b958.html
根據網上的一些資料,自己寫了兩個類實現dataset與excel轉換。
使用時必須先引入excel com控制項。。
using System;
using System.Data;
using Excel ;
namespace DataSetAndExcel
{
/// <summary>
/// 將資料集轉換成excel活頁簿
/// </summary>
public class DataSet2WorkBook
{
private DataSet mDs = new DataSet() ; //存放資料來源
private string mFilePath = "c:\\temp.xls" ; //excel檔案名稱,儲存的路徑
public DataSet2WorkBook(ref DataSet ds , string filePath )
{
//
// TODO: 在此處添加建構函式邏輯
//
this.mDs = ds ;
this.mFilePath = filePath ;
}
/// <summary>
/// 將資料錶轉換成excel活頁簿中的sheet
/// </summary>
/// <param name="tb">要轉換的資料表(參考型別)</param>
/// <param name="xSheet">目標sheet</param>
/// <param name="SheetName">sheet名字</param>
/// <returns></returns>
private bool DataTable2Sheet( ref System.Data.DataTable tb ,ref Excel._Worksheet xSheet ,string SheetName )
{
try
{
int rowIndex=2;
int colIndex=0;
if(SheetName == "")
{
xSheet.Name = tb.TableName ;
}
else
{
xSheet.Name = SheetName ;
}
foreach(DataColumn tempCol in tb.Columns )
{
xSheet.Cells[1,colIndex+1]=tempCol.ColumnName;
rowIndex = 2 ;
foreach(DataRow tempRow in tb.Rows )
{
xSheet.Cells[rowIndex ,colIndex+1] = "'"+tempRow[colIndex].ToString() ;
rowIndex++ ;
}
colIndex++;
}
return true ;
}
catch
{
return false ;
}
}
/// <summary>
/// 將指定資料集裡的錶轉換成活頁簿裡sheet
/// </summary>
/// <param name="starPos">資料表開始位置從0開始計數</param>
/// <param name="Count">要轉換資料表的數目</param>
/// <returns>成功返回true</returns>
public bool Convert(int starPos ,int Count)
{
try
{
System.Data .DataTable tempTable ; //建立暫存資料表
Excel.Application xApp= new Excel.Application();
xApp.Visible = false ;
object objOpt = System.Reflection.Missing.Value;
Excel.Workbook xBook = xApp.Workbooks.Add(true) ;//添加新活頁簿
Excel.Sheets xSheets = xBook.Sheets ;
Excel._Worksheet xSheet = null ;
//
//轉換從指定起始位置以後一定數目的資料集
//
for(int i = starPos , iCount = 1 ; iCount <= Count && i< this.mDs.Tables.Count ; i++ ,iCount++ )
{
tempTable = this.mDs.Tables[i] ;
//
//建立空的sheet
//
xSheet = (Excel._Worksheet)(xBook.Sheets.Add(objOpt,objOpt,objOpt,objOpt)) ;
DataTable2Sheet(ref tempTable ,ref xSheet ,"") ;
}
//
//擷取預設產生的sheet並將其刪除
//
//Excel._Worksheet tempXSheet = (Excel._Worksheet) (xSheets.get_Item(1)) ;
//
Excel._Worksheet tempXSheet = (Excel._Worksheet) (xBook.Worksheets[Count+1]) ;
tempXSheet.Delete() ;
System.Runtime.InteropServices.Marshal.ReleaseComObject(tempXSheet) ;
tempXSheet=null ;
//
//儲存
//
xBook.Saved = true ;
xBook.SaveCopyAs(this.mFilePath ) ;
//
//釋放資源
//
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSheet) ;
xSheet=null ;
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSheets) ;
xSheets=null ;
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBook) ;
xBook=null ;
xApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xApp);
xApp = null ;
GC.Collect();//強行銷毀
return true ;
}
catch
{
return false ;
}
}
/// <summary>
/// 重載convert,將資料集裡所有的錶轉換活頁簿的sheet
/// </summary>
/// <returns></returns>
public bool Convert()
{
return this.Convert( 0 ,this.mDs.Tables.Count ) ;
}
}
/// <summary>
/// WorkBook2DataSet 的摘要說明。將活頁簿轉換成dataset
/// </summary>
public class WorkBook2DataSet
{
private string mFilePath = "" ;
private DataSet mDs = new DataSet() ;
public WorkBook2DataSet(string path , ref DataSet ds)
{
//
// TODO: 在此處添加建構函式邏輯
//
this.mDs = ds ;
this.mFilePath = path ;
}
/// <summary>
/// 將活頁簿中指定的sheet轉換成dataset中的表
/// </summary>
/// <param name="pos">sheet在活頁簿中的位置</param>
/// <returns>成功返回true</returns>
public bool Convert(int pos)
{
bool r = false ;
string strSql = "" ;
string sheetName = "" ;
System.Data.DataTable tTable;
OleDbDataAdapter objDa ;
//
//建立excel進程
//
object obj = System.Reflection.Missing.Value;
Excel.ApplicationClass xxApp= new Excel.ApplicationClass() ;//.Application();
Excel.Workbook xxBook =null ;
Excel._Worksheet xxSheet =null ;
try
{
//
//開啟excel檔案,並擷取指定sheet的名字
//
xxBook = xxApp.Workbooks.Open(this.mFilePath ,obj ,obj,obj,obj,obj,obj,obj,obj,obj,obj,obj,obj,obj,obj) ;//添加新活頁簿
xxSheet = (Excel._Worksheet) (xxBook.Worksheets[pos]) ;
sheetName =xxSheet.Name.ToString() ;
//
//釋放excel資源
//
System.Runtime.InteropServices.Marshal.ReleaseComObject(xxSheet) ;
xxSheet=null ;
GC.Collect() ;
xxBook.Close(false,obj,obj) ;
System.Runtime.InteropServices.Marshal.ReleaseComObject(xxBook) ;
xxBook=null ;
xxApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xxApp);
xxApp = null ;
//
//建立資料連線
//
OleDbConnection objConn = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ this.mFilePath +";Extended Properties=Excel 8.0;");
//
//擷取活頁簿中的表
//
strSql = "select * from [" + sheetName +"$]" ;
tTable = new System.Data.DataTable( sheetName ) ;
//
//將sheet填入table中
//
objDa = new OleDbDataAdapter(strSql ,objConn) ;
objDa.Fill(tTable) ;
this.mDs.Tables.Add(tTable) ;
//
//摧毀串連
//
objConn.Dispose() ;
r = true ;
}
catch
{
r = false ;
}
GC.Collect() ;
return r ;
}
/// <summary>
/// 轉換活頁簿中所有的sheet到dataset
/// </summary>
/// <returns></returns>
public bool Convert()
{
bool r = false ; //傳回值
//
//建立excel進程
//
object obj = System.Reflection.Missing.Value;
Excel.Application xApp= new Excel.Application();
xApp.Visible = false ;
Excel.Workbook xBook = xApp.Workbooks.Open(this.mFilePath ,false ,false,obj,obj,obj,obj,obj,obj,obj,obj,obj,obj,obj,obj) ;//
int count = xBook.Sheets.Count ;
//
//釋放資源
//
xBook.Close(false , this.mFilePath ,obj) ;
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBook) ;
xBook=null ;
xApp.Quit() ;
System.Runtime.InteropServices.Marshal.ReleaseComObject(xApp);
xApp = null ;
GC.Collect() ;
for(int i = 1 ; i <= count ; i++)
{
r = Convert(i) ;
}
return r ;
//return this.Convert(1,count) ;
}
}