多个Datagridview导出Excel

iiblue03 2010-10-11 01:19:19
各位大侠:
大家好!
我是vb.net的初学者,现在用的是vs2010,在TabControl里有三个tabpage,每一个page里有一个Datagridview,想把Datagridview里的数据导出至Excel里,并且每一个Datagridview里的数据分别导出到Excel里的一页sheet里。请问怎么实现啊?谢谢大侠们!呵呵。
...全文
173 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
iiblue03 2010-10-14
  • 打赏
  • 举报
回复

#Region "从Datatable保存到Excel"
Sub SaveToExcel(ByVal dtz As System.Data.DataTable, ByVal dtn As System.Data.DataTable, ByVal dtj As System.Data.DataTable)
Try
Dim xlApp As Excel.Application = New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheetZ As Excel.Worksheet
Dim xlSheetN As Excel.Worksheet
Dim xlSheetJ As Excel.Worksheet
Dim rowIndexz As Integer = 1
Dim colIndexz As Integer = 0
Dim rowIndexn As Integer = 1
Dim colIndexn As Integer = 0
Dim rowIndexj As Integer = 1
Dim colIndexj As Integer = 0

'路径
Dim filename As String = Environment.CurrentDirectory & "\Excel\" & Date.Now.Date & "_" & Date.Now.Hour & "时" & Date.Now.Minute & "分" & Date.Now.Second & "秒.xls"
xlBook = xlApp.Workbooks().Add
xlSheetZ = xlBook.Worksheets("sheet1")
xlSheetN = xlBook.Worksheets("sheet2")
xlSheetJ = xlBook.Worksheets("sheet3")

'将所得到的表的列名,赋值给单元格

Dim Colz As DataColumn
Dim Rowz As DataRow
Dim Coln As DataColumn
Dim Rown As DataRow
Dim Colj As DataColumn
Dim Rowj As DataRow

'保存总参数
For Each Colz In dtz.Columns
colIndexz = colIndexz + 1
xlSheetZ.Cells(1, colIndexz) = Colz.ColumnName
Next
'得到的表所有行,赋值给单元格
For Each Rowz In dtz.Rows
rowIndexz = rowIndexz + 1
colIndexz = 0
For Each Colz In dtz.Columns
colIndexz = colIndexz + 1
xlSheetZ.Cells(rowIndexz, colIndexz) = Rowz(Colz.ColumnName)
xlSheetZ.Columns.AutoFit()
Next
Next
xlSheetZ.Name = "总参数"
'xlSheetZ.Save()

'保存风网数据
For Each Coln In dtn.Columns
colIndexn = colIndexn + 1
xlSheetN.Cells(1, colIndexn) = Coln.ColumnName
Next
'得到的表所有行,赋值给单元格
For Each Rown In dtn.Rows
rowIndexn = rowIndexn + 1
colIndexn = 0
For Each Coln In dtn.Columns
colIndexn = colIndexn + 1
xlSheetN.Cells(rowIndexn, colIndexn) = Rown(Coln.ColumnName)
xlSheetN.Columns.AutoFit()
Next
Next
xlSheetN.Name = "风网数据"
'xlSheetN.Save()

'保存节点数据
For Each Colj In dtj.Columns
colIndexj = colIndexj + 1
xlSheetJ.Cells(1, colIndexj) = Colj.ColumnName
Next
'得到的表所有行,赋值给单元格
For Each Rowj In dtj.Rows
rowIndexj = rowIndexj + 1
colIndexj = 0
For Each Colj In dtj.Columns
colIndexj = colIndexj + 1
xlSheetJ.Cells(rowIndexj, colIndexj) = Rowj(Colj.ColumnName)
xlSheetJ.Columns.AutoFit()
Next
Next
xlSheetJ.Name = "节点数据"
'xlSheetJ.Save()

xlBook.SaveAs("基本参数数据")
If MsgBox("文件保存在:" & filename & "  是否查看?", MsgBoxStyle.YesNo, "提示") = MsgBoxResult.Yes Then
xlApp.Visible = True
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
#End Region
iiblue03 2010-10-14
  • 打赏
  • 举报
回复
多谢大家的帮助啊。看了很多例子。最后顺利完成导出excel多个sheet了。
lkhoji 2010-10-13
  • 打赏
  • 举报
回复
学习了
Tosp2012 2010-10-11
  • 打赏
  • 举报
回复
导出一个和多个DataGridView的思路都差不多的。
网上有很多导出Excel的例子。
楼主不妨找找看。
iiblue03 2010-10-11
  • 打赏
  • 举报
回复
谢谢大家啊。先吸收吸收。呵呵。
加油馒头 2010-10-11
  • 打赏
  • 举报
回复
调用DLL库导出

但是速度较慢
孟子E章 2010-10-11
  • 打赏
  • 举报
回复
hongjiaoli 2010-10-11
  • 打赏
  • 举报
回复
public void ExortExcel() //从GridView中导出Excel的函数
{
if (this.Gv.Rows.Count != 0)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"); //这里是用日期做名称
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentType = "application/excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
this.Gv.AllowPaging = false; //GridView不启用分页
Gv.RenderControl(htmlWrite);
HttpContext.Current.Response.Write(stringWrite.ToString());
}
}

private void ToExcel() //从GridView导出Excel函数2 (这两个方法可任选其一)
{
string style = @"<style> .text { mso-number-format:\@; } </script> ";
string Ftile = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString();
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=COM_" + Ftile + ".xls");
// Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
// Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.Gvexel.RenderControl(oHtmlTextWriter);//GVDY 是Gridview的ID名称
Response.Write(style);
Response.Write(oStringWriter.ToString());
Response.End();
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hongjiaoli/archive/2009/10/23/4716844.aspx

16,721

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧