扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
页面做好一个GridView控件
创新互联公司自2013年起,是专业互联网技术服务公司,拥有项目成都网站设计、网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元同安做网站,已为上家服务,为同安各地企业和个人服务,联系电话:18982081108
导出数据库文件并处理好,导入到GridView
导出到EXCEL文档代码:
Response.Clear()
Response.ClearHeaders()
Response.ClearContent()
Response.Buffer = True
Response.HeaderEncoding = Encoding.GetEncoding("gb2312")
Response.ContentType = "application/octet-stream"
Dim min As String = "_555663.xls"
Response.AddHeader("content-disposition", "attachment; filename=" min)
Response.ContentType = "up/"
'GridView1.Page.EnableViewState = False
Dim sw As StringWriter = New StringWriter
Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
GridView1.RenderControl(htw)
Response.Write(sw.ToString())
Response.Flush()
Response.End()
asp.net 中将gridview数据导入到excel的类 usingSystem; usingSystem.Data; usingSystem.Configuration; usingSystem.IO; usingSystem.Web; usingSystem.Web.Security; usingSystem.Web.UI; usingSystem.Web.UI. WebControls; usingSystem.Web.UI. WebControls.WebParts; usingSystem.Web.UI. HtmlControls; ///summary /// ////summary publicclass GridViewExportUtil { ///summary /// ////summary ///paramname="fileName" /param ///paramname="gv"/ param publicstaticvoidExport( stringfileName,GridViewgv) { HttpContext.Current. Response.Clear(); HttpContext.Current. Response.AddHeader( "content- disposition",string.Format(" attachment;filename={0}", System.Web.HttpUtility. UrlEncode(fileName,System. Text.Encoding.UTF8))); HttpContext.Current. Response.ContentType=" application/ms-excel"; HttpContext.Current. Response.Charset="utf-8"; HttpContext.Current. Response.ContentEncoding= System.Text.Encoding. GetEncoding("utf-8"); using(StringWriter sw=newStringWriter()) { using( HtmlTextWriterhtw=new HtmlTextWriter(sw)) { //Createa formtocontainthegrid Tabletable= newTable(); //addthe headerrowtothetable if(gv. HeaderRow!=null) { GridViewExportUtil. PrepareControlForExport(gv. HeaderRow); table. Rows.Add(gv.HeaderRow); } //addeach ofthedatarowstothetable foreach( GridViewRowrowingv.Rows) { GridViewExportUtil. PrepareControlForExport(row); table. Rows.Add(row); } //addthe footerrowtothetable if(gv. FooterRow!=null) { GridViewExportUtil. PrepareControlForExport(gv. FooterRow); table. Rows.Add(gv.FooterRow); } //render thetableintothehtmlwriter table. RenderControl(htw); //render thehtmlwriterintothe response HttpContext. Current.Response.Write(sw. ToString()); HttpContext. Current.Response.End(); } } } 作者: 后天美丽 2007-8-31 09:24 回复此发言 2 回复: asp.net 中将gridview数据导入到excel 的类 ///summary ///Replaceanyofthe containedcontrolswith literals ////summary ///paramname="control"/ param privatestaticvoid PrepareControlForExport( Controlcontrol) { for(inti=0;icontrol. Controls.Count;i++) { Controlcurrent=control. Controls[i]; if(currentisLinkButton) { control.Controls.Remove( current); control.Controls.AddAt(i, newLiteralControl((current asLinkButton).Text)); } elseif(currentis ImageButton) { control.Controls.Remove( current); control.Controls.AddAt(i, newLiteralControl((current asImageButton).AlternateText) ); } elseif(currentis HyperLink) { control.Controls.Remove( current); control.Controls.AddAt(i, newLiteralControl((current asHyperLink).Text)); } elseif(currentis DropDownList) { control.Controls.Remove( current); control.Controls.AddAt(i, newLiteralControl((current asDropDownList).SelectedItem. Text)); } elseif(currentisCheckBox) { control.Controls.Remove( current); control.Controls.AddAt(i, newLiteralControl((current asCheckBox).Checked?"True" :"False")); } if(current.HasControls()) { GridViewExportUtil. PrepareControlForExport( current); } } } }
比如说用Richtext控件
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim file As String = "f:/x.txt"
RichTextBox1.LoadFile(file)
MsgBox("ok")
End Sub
解题思路:
把EXCEL看做数据源来连接。
用一个list来显示EXCEL中的所有表,选择指定表名,打开EXCEL到DG
——————————如下:
引用ADODB 2.8
导入:
Imports System.Data
Imports System.Data.Odbc
Imports System.Data.OleDb
申明
Private Excelpath As String
Public conn As New ADODB.Connection
Public rs As New ADODB.Recordset
创建conn
Public Sub connDB(ByVal filePath As String)
On Error Resume Next
Dim strConnString As String = "Provider=microsoft.ace.oledb.12.0;Data Source=" filePath ";Extended Properties='Excel 12.0; HDR=yes;IMEX=1';"
conn.ConnectionString = strConnString
conn.CursorLocation = ADODB.CursorLocationEnum.adUseClient
conn.Open()
End Sub
Public Function 获取电子表集合(filePath) As List(Of String)
Dim ConnStr As String = "Provider=microsoft.ace.oledb.12.0;Data Source=" filePath ";Extended Properties='Excel 12.0; HDR=no;IMEX=1';"
Dim list As New List(Of String)()
Dim Conn2 As New OleDbConnection(ConnStr)
Try
If Conn2.State = ConnectionState.Closed Then
Conn2.Open()
End If
Dim dt As DataTable = Conn2.GetSchema("Tables")
For Each row As DataRow In dt.Rows
If row(3).ToString() = "TABLE" Then
list.Add(row(2).ToString())
End If
Next
Catch e As Exception
'Throw e
Finally
If Conn2.State = ConnectionState.Open Then
Conn2.Close()
End If
Conn2.Dispose()
End Try
Return list
End Function
//////////////////
开始打开指定EXCEL到DG
Call connDB(Excelpath)
Dim sqlstr As String = "select * from [" Me.ListBox1.Text "]"
With rs
If rs.State 0 Then rs.Close()
.CursorLocation = ADODB.CursorLocationEnum.adUseClient
.Open(sqlstr, conn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
If Not rs.EOF And Not rs.BOF Then
Dim da As New System.Data.OleDb.OleDbDataAdapter
Dim ds As New DataSet
da.Fill(ds, rs, "注册表")
DG.DataSource = Nothing
DG.DataSource = ds.Tables(0)
DG.Refresh()
End If
End With
可以参考Spire.XLS for .NET关于数据导入导出的方法。
excel数据导入dataset如下代码:
'创建Workbook对象并加载Excel文档
Dim workbook As New Workbook()
workbook.LoadFromFile("F:\ExportData.xlsx", ExcelVersion.Version2013)
'获取第一张sheet
Dim sheet As Worksheet = workbook.Worksheets(0)
'设置range范围
Dim range As CellRange = sheet.Range(sheet.FirstRow, sheet.FirstColumn, sheet.LastRow, sheet.LastColumn)
'输出数据, 同时输出列名以及公式值
Dim dt As DataTable = sheet.ExportDataTable(range, True, True)
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流