把GridView1的数据导出到excel的sheet1把GridView2的数据导出到excel的sheet2.

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了把GridView1的数据导出到excel的sheet1把GridView2的数据导出到excel的sheet2.相关的知识,希望对你有一定的参考价值。

情况是这个样子的:
在一个页面里有俩个GridView,有一个导出按钮,点击导出按钮的时候,把GridView1的内容导出到excel的sheet1里,
把GridView2的内容导出到excel的sheet2里。
大家帮帮忙吧,谢谢。

另外还有几个小问题:
1.导出的时候怎么在excel中插入一行,就是想写个表头,表头的字自己写。(用代码实现)
2.excel的sheet1的名字怎么改掉?(用代码实现)
谢谢大家了。能帮一个就帮一个吧。

/// <summary>
/// 把DataGridView到导出到Excel中
/// </summary>
/// <param >数据源DataGridView</param>
/// <param >保存文件名称</param>
/// <param >是否显示Excel界面</param>
/// <returns>导出是否成功</returns>
private bool ExportForDataGridview(System.Windows.Forms.DataGridView gridView, string fileName, bool isShowExcle, out string exceptionInfo)

bool isok = false;
exceptionInfo = "";
//建立Excel对象
Microsoft.Office.Interop.Excel.Application app = null;
try

app = new Microsoft.Office.Interop.Excel.Application();

catch

exceptionInfo = "无法创建Excel对象,请确保您的机器安装有MicroSoft Office 2003 Excel。";
isok = false;

if (app != null)

try

app.Visible = isShowExcle;
Workbooks workbooks = app.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Sheets sheets = workbook.Worksheets;
_Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
if (worksheet == null)

exceptionInfo = "无法创建Excel工作表,未知错误。";
isok = false;

else

//求得当前显示列的数目
int columnCount = 0;
for (int i = 0; i < gridView.ColumnCount; i++)

if (gridView.Columns[i].Visible)

columnCount++;



string sLen = "";
//取得最后一列列名
char H = (char)(64 + columnCount / 26);
char L = (char)(64 + columnCount % 26);
if (columnCount < 26)

sLen = L.ToString();

else

sLen = H.ToString() + L.ToString();


//标题
string sTmp = sLen + "1";
Range ranCaption = worksheet.get_Range(sTmp, "A1");
string[] asCaption = new string[columnCount];
int asCaptionIndex = 0;
for (int i = 0; i < gridView.ColumnCount; i++)

if (gridView.Columns[i].Visible)

asCaption[asCaptionIndex] = gridView.Columns[i].HeaderText;
asCaptionIndex++;


ranCaption.Value2 = asCaption;

//数据
object[] obj = new object[columnCount];
int objIndex = 0;
for (int r = 0; r < gridView.RowCount - 1; r++)

for (int l = 0; l < gridView.Columns.Count; l++)

if (gridView.Columns[l].Visible)

if (gridView[l, r].ValueType == typeof(DateTime))

obj[objIndex] = gridView[l, r].Value.ToString();

else if (gridView[l, r].ValueType == typeof(String))

obj[objIndex] = "\'" + gridView[l, r].Value.ToString();

else

obj[objIndex] = gridView[l, r].Value;

objIndex = (objIndex + 1) % columnCount;


string cell1 = sLen + ((int)(r + 2)).ToString();
string cell2 = "A" + ((int)(r + 2)).ToString();
Range ran = worksheet.get_Range(cell1, cell2);
ran.Value2 = obj;


//保存
workbook.SaveCopyAs(fileName);
workbook.Saved = true;

isok = true;



catch (Exception err)

exceptionInfo = err.Message;
isok = false;

finally

if (app != null)

//关闭
app.UserControl = false;
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null;



return isok;



需要引入
using Microsoft.Office.Interop.Excel;

----另外一种方法:

// 导出列表信息到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("\\n",""));



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("\\n",""));

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();




参考技术A 职位信息收藏] [该公司所有职位] [介绍给朋友] 参考技术B 楼上的大能,这个也可以啊

以上是关于把GridView1的数据导出到excel的sheet1把GridView2的数据导出到excel的sheet2.的主要内容,如果未能解决你的问题,请参考以下文章

asp.net中,如何实现将page页面各个空间的属性值,导出下载到一个word或excel中

C#winform中如何把表导出到EXCEL

C#把查询出来的数据导出到Excel代码怎么写

怎样从数据库里把自己需要的数据导出到excel表格中?

ASP.NET 导出EXCEL时如何不把一些不是EXCEL的符号一起导出到EXCEL?

如何把SQLServer表数据导出为Excel文件