重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
解题思路:
10年积累的成都网站建设、做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站后付款的网站建设流程,更有南票免费网站建设让你可以放心的选择与我们合作。
把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
先将excel中的数据读入到 DataTable中
然后用一个For循环,逐条Insert 到对应数据库的表中
页面做好一个GridView控件
导出数据库文件并处理好,导入到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()
本例子在窗体上添加一个按钮,一个空的DataGridView控件;
在按钮的单击事件里编写代码如下:
'读取CSV文件到DataGridView控件
Dim r As New StreamReader("C:\工作簿1.csv", System.Text.Encoding.Default) '用StreamReader打开文件
Dim MyRows(0) As String
Dim d() As String
Dim i As Integer = -1
Dim j As Integer = 0
Do While r.Peek -1 '是否到文件尾
i = i + 1
ReDim Preserve MyRows(i)
MyRows(i) = r.ReadLine '从打开的文件中读取一行内容
Loop
r.Close() '关闭对象
'获得数据的列数
d = Split(MyRows(1), ",")
'首先在DataGridView控件中添加列
DataGridView1.ColumnCount = UBound(d) + 1
'然后在DataGridView控件中添加行
DataGridView1.Rows.Add(UBound(MyRows))
'在DataGridView控件中添加数据
For i = 0 To UBound(MyRows)
d = Split(MyRows(i), ",")
For j = 0 To UBound(d)
DataGridView1.Item(j, i).Value = d(j)
Next
Next
1.使用PHP
Excel Parser Pro软件,但是这个软件为收费软件;
2.可将EXCEL表保存为CSV格式,然后通过phpmyadmin或者SQLyog导入,SQLyog导入的方法为:
·将EXCEL表另存为CSV形式;
·打开SQLyog,对要导入的表格右击,点击“导入”-“导入使用加载本地CSV数据”;
·在弹出的对话框中,点击“改变..”,把选择“填写excel友好值”,点击确定;
·在“从文件导入”中选择要导入的CSV文件路径,点击“导入”即可导入数据到表上;
一个按钮,一个文本框,把文本框设置成多行显示。
代码如下:
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim r As New StreamReader(Application.StartupPath "\ABC.txt", System.Text.Encoding.Default) '用StreamReader打开文本文件
Dim s As String
TextBox1.Text = ""
Do While r.Peek -1 '是否到文件尾
s = r.ReadLine '从打开的文件中读取一行内容
TextBox1.Text = TextBox1.Text s vbCrLf '添加到文本框TextBox1.Text的后面并回车
Loop
r.Close() '关闭对象
End Sub
End Class