5 个解决方案
#1
不是很麻烦,不过你的分数不够,等加了分给我短信。OK?
#2
建议现在msdn搜索一下
1。 如何把数据库中的变量度到一个变量种。
2。 如何把一些数据写到 Excel 中。
网上相关的很多。
good luck
1。 如何把数据库中的变量度到一个变量种。
2。 如何把一些数据写到 Excel 中。
网上相关的很多。
good luck
#3
http://www.csdn.net/develop/read_article.asp?id=16987
#4
T-SQL代码:
将SQL SERVER中查询到的数据导成一个Excel文件
EXEC master..xp_cmdshell 'bcp 库名.dbo.表名out c:\Temp.xls -c -q -S"servername" -U"sa" -P""'
参数:S 是SQL服务器名;U是用户;P是密码
说明:还可以导出文本文件等多种格式
实例:EXEC master..xp_cmdshell 'bcp saletesttmp.dbo.CusAccount out c:\temp1.xls -c -q -S"pmserver" -U"sa" -P"sa"'
EXEC master..xp_cmdshell 'bcp "SELECT au_fname, au_lname FROM pubs..authors ORDER BY au_lname" queryout C:\ authors.xls -c -Sservername -Usa -Ppassword'
详见:
http://www.csdn.net/develop/read_article.asp?id=18623
将SQL SERVER中查询到的数据导成一个Excel文件
EXEC master..xp_cmdshell 'bcp 库名.dbo.表名out c:\Temp.xls -c -q -S"servername" -U"sa" -P""'
参数:S 是SQL服务器名;U是用户;P是密码
说明:还可以导出文本文件等多种格式
实例:EXEC master..xp_cmdshell 'bcp saletesttmp.dbo.CusAccount out c:\temp1.xls -c -q -S"pmserver" -U"sa" -P"sa"'
EXEC master..xp_cmdshell 'bcp "SELECT au_fname, au_lname FROM pubs..authors ORDER BY au_lname" queryout C:\ authors.xls -c -Sservername -Usa -Ppassword'
详见:
http://www.csdn.net/develop/read_article.asp?id=18623
#5
先将SQL数据利用DATAADAPTER填充到DATASET中,比如填充为表TABLE1
定义一个DATATABLE对象,假设为MyTable_RT
MyTable_RT=Dataset1.Tables("Table1")
下面是导入Excel表的代码
Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
Dim MyOleDbCn As New OleDbConnection()
Dim MyOleDbCmd As New OleDbCommand()
Dim intRowsCnt, intColsCnt As Integer
Dim strSql As String, strFlName As String
Dim Fso As New FileSystemObject()
SaveDl.Title = "将结果保存为"
SaveDl.Filter = "*.xls|*.xls"
SaveDl.ShowDialog()
If SaveDl.FileName <> "" Then
strFlName = SaveDl.FileName()
Else
Exit Sub
End If
Try
Me.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
MyOleDbCn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;" & _
"Data Source=" & strFlName & ";" & _
"Extended ProPerties=""Excel 8.0;HDR=Yes;"""
MyOleDbCn.Open()
MyOleDbCmd.Connection = MyOleDbCn
MyOleDbCmd.CommandType = CommandType.Text
'第一行插入列标题
strSql = "CREATE TABLE MyTab("
For intColsCnt = 0 To MyTable_RT.Columns.Count - 1
If intColsCnt <> MyTable_RT.Columns.Count - 1 Then
strSql = strSql & MyTable_RT.Columns(intColsCnt).Caption & " text,"
Else
strSql = strSql & MyTable_RT.Columns(intColsCnt).Caption & " text)"
End If
Next
MyOleDbCmd.CommandText = strSql
MyOleDbCmd.ExecuteNonQuery()
'插入各行
For intRowsCnt = 0 To MyTable_RT.Rows.Count - 1
strSql = "INSERT INTO MyTab VALUES('"
For intColsCnt = 0 To MyTable_RT.Columns.Count - 1
If intColsCnt <> MyTable_RT.Columns.Count - 1 Then
strSql = strSql & MyTable_RT.Rows(intRowsCnt).Item(intColsCnt) & "','"
Else
strSql = strSql & MyTable_RT.Rows(intRowsCnt).Item(intColsCnt) & "')"
End If
Next
MyOleDbCmd.CommandText = strSql
MyOleDbCmd.ExecuteNonQuery()
Next
MessageBox.Show("数据已经成功导入EXCEL文件" & strFlName, "数据导出", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ErrCode As Exception
MsgBox("错误信息:" & ErrCode.Message & vbCrLf & vbCrLf & _
"引发事件:" & ErrCode.TargetSite.ToString, MsgBoxStyle.OKOnly + MsgBoxStyle.Information, "错误来源:" & ErrCode.Source)
Exit Sub
Finally
MyOleDbCmd.Dispose()
MyOleDbCn.Close()
MyOleDbCn.Dispose()
Me.Cursor.Current = System.Windows.Forms.Cursors.Default
End Try
End Sub
定义一个DATATABLE对象,假设为MyTable_RT
MyTable_RT=Dataset1.Tables("Table1")
下面是导入Excel表的代码
Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
Dim MyOleDbCn As New OleDbConnection()
Dim MyOleDbCmd As New OleDbCommand()
Dim intRowsCnt, intColsCnt As Integer
Dim strSql As String, strFlName As String
Dim Fso As New FileSystemObject()
SaveDl.Title = "将结果保存为"
SaveDl.Filter = "*.xls|*.xls"
SaveDl.ShowDialog()
If SaveDl.FileName <> "" Then
strFlName = SaveDl.FileName()
Else
Exit Sub
End If
Try
Me.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
MyOleDbCn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;" & _
"Data Source=" & strFlName & ";" & _
"Extended ProPerties=""Excel 8.0;HDR=Yes;"""
MyOleDbCn.Open()
MyOleDbCmd.Connection = MyOleDbCn
MyOleDbCmd.CommandType = CommandType.Text
'第一行插入列标题
strSql = "CREATE TABLE MyTab("
For intColsCnt = 0 To MyTable_RT.Columns.Count - 1
If intColsCnt <> MyTable_RT.Columns.Count - 1 Then
strSql = strSql & MyTable_RT.Columns(intColsCnt).Caption & " text,"
Else
strSql = strSql & MyTable_RT.Columns(intColsCnt).Caption & " text)"
End If
Next
MyOleDbCmd.CommandText = strSql
MyOleDbCmd.ExecuteNonQuery()
'插入各行
For intRowsCnt = 0 To MyTable_RT.Rows.Count - 1
strSql = "INSERT INTO MyTab VALUES('"
For intColsCnt = 0 To MyTable_RT.Columns.Count - 1
If intColsCnt <> MyTable_RT.Columns.Count - 1 Then
strSql = strSql & MyTable_RT.Rows(intRowsCnt).Item(intColsCnt) & "','"
Else
strSql = strSql & MyTable_RT.Rows(intRowsCnt).Item(intColsCnt) & "')"
End If
Next
MyOleDbCmd.CommandText = strSql
MyOleDbCmd.ExecuteNonQuery()
Next
MessageBox.Show("数据已经成功导入EXCEL文件" & strFlName, "数据导出", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ErrCode As Exception
MsgBox("错误信息:" & ErrCode.Message & vbCrLf & vbCrLf & _
"引发事件:" & ErrCode.TargetSite.ToString, MsgBoxStyle.OKOnly + MsgBoxStyle.Information, "错误来源:" & ErrCode.Source)
Exit Sub
Finally
MyOleDbCmd.Dispose()
MyOleDbCn.Close()
MyOleDbCn.Dispose()
Me.Cursor.Current = System.Windows.Forms.Cursors.Default
End Try
End Sub
#1
不是很麻烦,不过你的分数不够,等加了分给我短信。OK?
#2
建议现在msdn搜索一下
1。 如何把数据库中的变量度到一个变量种。
2。 如何把一些数据写到 Excel 中。
网上相关的很多。
good luck
1。 如何把数据库中的变量度到一个变量种。
2。 如何把一些数据写到 Excel 中。
网上相关的很多。
good luck
#3
http://www.csdn.net/develop/read_article.asp?id=16987
#4
T-SQL代码:
将SQL SERVER中查询到的数据导成一个Excel文件
EXEC master..xp_cmdshell 'bcp 库名.dbo.表名out c:\Temp.xls -c -q -S"servername" -U"sa" -P""'
参数:S 是SQL服务器名;U是用户;P是密码
说明:还可以导出文本文件等多种格式
实例:EXEC master..xp_cmdshell 'bcp saletesttmp.dbo.CusAccount out c:\temp1.xls -c -q -S"pmserver" -U"sa" -P"sa"'
EXEC master..xp_cmdshell 'bcp "SELECT au_fname, au_lname FROM pubs..authors ORDER BY au_lname" queryout C:\ authors.xls -c -Sservername -Usa -Ppassword'
详见:
http://www.csdn.net/develop/read_article.asp?id=18623
将SQL SERVER中查询到的数据导成一个Excel文件
EXEC master..xp_cmdshell 'bcp 库名.dbo.表名out c:\Temp.xls -c -q -S"servername" -U"sa" -P""'
参数:S 是SQL服务器名;U是用户;P是密码
说明:还可以导出文本文件等多种格式
实例:EXEC master..xp_cmdshell 'bcp saletesttmp.dbo.CusAccount out c:\temp1.xls -c -q -S"pmserver" -U"sa" -P"sa"'
EXEC master..xp_cmdshell 'bcp "SELECT au_fname, au_lname FROM pubs..authors ORDER BY au_lname" queryout C:\ authors.xls -c -Sservername -Usa -Ppassword'
详见:
http://www.csdn.net/develop/read_article.asp?id=18623
#5
先将SQL数据利用DATAADAPTER填充到DATASET中,比如填充为表TABLE1
定义一个DATATABLE对象,假设为MyTable_RT
MyTable_RT=Dataset1.Tables("Table1")
下面是导入Excel表的代码
Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
Dim MyOleDbCn As New OleDbConnection()
Dim MyOleDbCmd As New OleDbCommand()
Dim intRowsCnt, intColsCnt As Integer
Dim strSql As String, strFlName As String
Dim Fso As New FileSystemObject()
SaveDl.Title = "将结果保存为"
SaveDl.Filter = "*.xls|*.xls"
SaveDl.ShowDialog()
If SaveDl.FileName <> "" Then
strFlName = SaveDl.FileName()
Else
Exit Sub
End If
Try
Me.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
MyOleDbCn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;" & _
"Data Source=" & strFlName & ";" & _
"Extended ProPerties=""Excel 8.0;HDR=Yes;"""
MyOleDbCn.Open()
MyOleDbCmd.Connection = MyOleDbCn
MyOleDbCmd.CommandType = CommandType.Text
'第一行插入列标题
strSql = "CREATE TABLE MyTab("
For intColsCnt = 0 To MyTable_RT.Columns.Count - 1
If intColsCnt <> MyTable_RT.Columns.Count - 1 Then
strSql = strSql & MyTable_RT.Columns(intColsCnt).Caption & " text,"
Else
strSql = strSql & MyTable_RT.Columns(intColsCnt).Caption & " text)"
End If
Next
MyOleDbCmd.CommandText = strSql
MyOleDbCmd.ExecuteNonQuery()
'插入各行
For intRowsCnt = 0 To MyTable_RT.Rows.Count - 1
strSql = "INSERT INTO MyTab VALUES('"
For intColsCnt = 0 To MyTable_RT.Columns.Count - 1
If intColsCnt <> MyTable_RT.Columns.Count - 1 Then
strSql = strSql & MyTable_RT.Rows(intRowsCnt).Item(intColsCnt) & "','"
Else
strSql = strSql & MyTable_RT.Rows(intRowsCnt).Item(intColsCnt) & "')"
End If
Next
MyOleDbCmd.CommandText = strSql
MyOleDbCmd.ExecuteNonQuery()
Next
MessageBox.Show("数据已经成功导入EXCEL文件" & strFlName, "数据导出", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ErrCode As Exception
MsgBox("错误信息:" & ErrCode.Message & vbCrLf & vbCrLf & _
"引发事件:" & ErrCode.TargetSite.ToString, MsgBoxStyle.OKOnly + MsgBoxStyle.Information, "错误来源:" & ErrCode.Source)
Exit Sub
Finally
MyOleDbCmd.Dispose()
MyOleDbCn.Close()
MyOleDbCn.Dispose()
Me.Cursor.Current = System.Windows.Forms.Cursors.Default
End Try
End Sub
定义一个DATATABLE对象,假设为MyTable_RT
MyTable_RT=Dataset1.Tables("Table1")
下面是导入Excel表的代码
Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
Dim MyOleDbCn As New OleDbConnection()
Dim MyOleDbCmd As New OleDbCommand()
Dim intRowsCnt, intColsCnt As Integer
Dim strSql As String, strFlName As String
Dim Fso As New FileSystemObject()
SaveDl.Title = "将结果保存为"
SaveDl.Filter = "*.xls|*.xls"
SaveDl.ShowDialog()
If SaveDl.FileName <> "" Then
strFlName = SaveDl.FileName()
Else
Exit Sub
End If
Try
Me.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
MyOleDbCn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;" & _
"Data Source=" & strFlName & ";" & _
"Extended ProPerties=""Excel 8.0;HDR=Yes;"""
MyOleDbCn.Open()
MyOleDbCmd.Connection = MyOleDbCn
MyOleDbCmd.CommandType = CommandType.Text
'第一行插入列标题
strSql = "CREATE TABLE MyTab("
For intColsCnt = 0 To MyTable_RT.Columns.Count - 1
If intColsCnt <> MyTable_RT.Columns.Count - 1 Then
strSql = strSql & MyTable_RT.Columns(intColsCnt).Caption & " text,"
Else
strSql = strSql & MyTable_RT.Columns(intColsCnt).Caption & " text)"
End If
Next
MyOleDbCmd.CommandText = strSql
MyOleDbCmd.ExecuteNonQuery()
'插入各行
For intRowsCnt = 0 To MyTable_RT.Rows.Count - 1
strSql = "INSERT INTO MyTab VALUES('"
For intColsCnt = 0 To MyTable_RT.Columns.Count - 1
If intColsCnt <> MyTable_RT.Columns.Count - 1 Then
strSql = strSql & MyTable_RT.Rows(intRowsCnt).Item(intColsCnt) & "','"
Else
strSql = strSql & MyTable_RT.Rows(intRowsCnt).Item(intColsCnt) & "')"
End If
Next
MyOleDbCmd.CommandText = strSql
MyOleDbCmd.ExecuteNonQuery()
Next
MessageBox.Show("数据已经成功导入EXCEL文件" & strFlName, "数据导出", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ErrCode As Exception
MsgBox("错误信息:" & ErrCode.Message & vbCrLf & vbCrLf & _
"引发事件:" & ErrCode.TargetSite.ToString, MsgBoxStyle.OKOnly + MsgBoxStyle.Information, "错误来源:" & ErrCode.Source)
Exit Sub
Finally
MyOleDbCmd.Dispose()
MyOleDbCn.Close()
MyOleDbCn.Dispose()
Me.Cursor.Current = System.Windows.Forms.Cursors.Default
End Try
End Sub