把DataGrid要转成DataSet,很简单,就不说了
使用时必须先引入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 ;
}