怎样在C#中把GRIDVIEW的数据导出为EXCEL
推荐于2016-05-24
展开全部
给你一个DataGrid的代码:
// 导出列表信息到Excel
public static void gSendGridInfoToExcel(DataGrid GridX)
{
Excel.Application excel= new Excel.ApplicationClass();
Excel._Workbook xBk = excel.Workbooks.Add(true);
Excel._Worksheet xSt = (Excel._Worksheet)xBk.ActiveSheet;
Excel.Range excelCell=null;
try
{
//赋值对象
object[] objarr;
DataTable dtTest=new DataTable();;
int i,j;
int iRows,iCows;
int iVisable;
iVisable=0;
iCows=0;
ArrayList list=new ArrayList();
//如果绑定数据源是DataTable和DataSet,取得行数
if (GridX.DataSource is System.Data.DataSet || GridX.DataSource is System.Data.DataTable)
{
dtTest=(DataTable)GridX.DataSource;
iRows=dtTest.Rows.Count;
}
else if (GridX.DataSource is System.Data.DataView)
{
DataView dvTest=(DataView)GridX.DataSource;
iRows=dvTest.Count;
dtTest=dvTest.Table;
}
//如果是集合取得行数
else
{
System.Collections.CollectionBase ColTest;
ColTest=(System.Collections.CollectionBase)GridX.DataSource;
iRows=ColTest.Count;
}
//如果有TableStyles则根据TableStyles取得(标题行)
if (GridX.TableStyles.Count>0)
{
iCows=GridX.TableStyles[0].GridColumnStyles.Count;
for(i=0;i<iCows;i++)
{
if(GridX.TableStyles[0].GridColumnStyles[i].Width>0)
{
iVisable++;
list.Add(GridX.TableStyles[0].GridColumnStyles[i].HeaderText);
}
}
objarr = new object[iVisable];
objarr=list.ToArray();
excelCell = xSt.get_Range(excel.Cells[1,1],excel.Cells[1,iVisable]);
excelCell.Value2 = objarr;
//数据行
for(i=0;i<iRows;i++)
{
objarr = new object[iVisable];
list.Clear();
for(j=0;j<iCows;j++)
{
if(GridX.TableStyles[0].GridColumnStyles[j].Width>0)
{
list.Add("'"+GridX[i,j].ToString().Replace("\
",""));
}
}
if (list.Equals(System.DBNull.Value))
{
break;
}
objarr=list.ToArray();
excelCell = xSt.get_Range(excel.Cells[i+2,1],excel.Cells[i+2,iVisable]);
excelCell.Value2 = objarr;
}
}
else
{
iCows=dtTest.Columns.Count;
for(i=0;i<iCows;i++)
{
list.Add(dtTest.Columns[i].Caption);
}
objarr = new object[iCows];
objarr=list.ToArray();
excelCell = xSt.get_Range(excel.Cells[1,1],excel.Cells[1,iCows]);
excelCell.Value2 = objarr;
//数据行
for(i=0;i<iRows;i++)
{
objarr = new object[iCows];
list.Clear();
for(j=0;j<iCows;j++)
{
list.Add("'"+GridX[i,j].ToString().Replace("\
",""));
}
if (list.Equals(System.DBNull.Value))
{
break;
}
objarr=list.ToArray();
excelCell = xSt.get_Range(excel.Cells[i+2,1],excel.Cells[i+2,iCows]);
excelCell.Value2 = objarr;
}
}
dtTest.Dispose();
excel.Visible=true;
}
catch (System.Exception e)
{
throw e;
}
finally
{
excelCell=null;
xBk=null;
xSt=null;
excel=null;
GC.Collect();
}
}
}
// 导出列表信息到Excel
public static void gSendGridInfoToExcel(DataGrid GridX)
{
Excel.Application excel= new Excel.ApplicationClass();
Excel._Workbook xBk = excel.Workbooks.Add(true);
Excel._Worksheet xSt = (Excel._Worksheet)xBk.ActiveSheet;
Excel.Range excelCell=null;
try
{
//赋值对象
object[] objarr;
DataTable dtTest=new DataTable();;
int i,j;
int iRows,iCows;
int iVisable;
iVisable=0;
iCows=0;
ArrayList list=new ArrayList();
//如果绑定数据源是DataTable和DataSet,取得行数
if (GridX.DataSource is System.Data.DataSet || GridX.DataSource is System.Data.DataTable)
{
dtTest=(DataTable)GridX.DataSource;
iRows=dtTest.Rows.Count;
}
else if (GridX.DataSource is System.Data.DataView)
{
DataView dvTest=(DataView)GridX.DataSource;
iRows=dvTest.Count;
dtTest=dvTest.Table;
}
//如果是集合取得行数
else
{
System.Collections.CollectionBase ColTest;
ColTest=(System.Collections.CollectionBase)GridX.DataSource;
iRows=ColTest.Count;
}
//如果有TableStyles则根据TableStyles取得(标题行)
if (GridX.TableStyles.Count>0)
{
iCows=GridX.TableStyles[0].GridColumnStyles.Count;
for(i=0;i<iCows;i++)
{
if(GridX.TableStyles[0].GridColumnStyles[i].Width>0)
{
iVisable++;
list.Add(GridX.TableStyles[0].GridColumnStyles[i].HeaderText);
}
}
objarr = new object[iVisable];
objarr=list.ToArray();
excelCell = xSt.get_Range(excel.Cells[1,1],excel.Cells[1,iVisable]);
excelCell.Value2 = objarr;
//数据行
for(i=0;i<iRows;i++)
{
objarr = new object[iVisable];
list.Clear();
for(j=0;j<iCows;j++)
{
if(GridX.TableStyles[0].GridColumnStyles[j].Width>0)
{
list.Add("'"+GridX[i,j].ToString().Replace("\
",""));
}
}
if (list.Equals(System.DBNull.Value))
{
break;
}
objarr=list.ToArray();
excelCell = xSt.get_Range(excel.Cells[i+2,1],excel.Cells[i+2,iVisable]);
excelCell.Value2 = objarr;
}
}
else
{
iCows=dtTest.Columns.Count;
for(i=0;i<iCows;i++)
{
list.Add(dtTest.Columns[i].Caption);
}
objarr = new object[iCows];
objarr=list.ToArray();
excelCell = xSt.get_Range(excel.Cells[1,1],excel.Cells[1,iCows]);
excelCell.Value2 = objarr;
//数据行
for(i=0;i<iRows;i++)
{
objarr = new object[iCows];
list.Clear();
for(j=0;j<iCows;j++)
{
list.Add("'"+GridX[i,j].ToString().Replace("\
",""));
}
if (list.Equals(System.DBNull.Value))
{
break;
}
objarr=list.ToArray();
excelCell = xSt.get_Range(excel.Cells[i+2,1],excel.Cells[i+2,iCows]);
excelCell.Value2 = objarr;
}
}
dtTest.Dispose();
excel.Visible=true;
}
catch (System.Exception e)
{
throw e;
}
finally
{
excelCell=null;
xBk=null;
xSt=null;
excel=null;
GC.Collect();
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询