vb.net 2010/2008 导出到excel,无需引用

这是网上的一个在Vb.net中将数据库中的数据导出到Excel的例子,经过了小小的修改,这种方法的好处是不需要另外的引用,代码如下:

       

ASP/Visual Basic Code复制内容到剪贴板
  1. Dim Mytable As DataTable = myDataSet("select * from JL").Tables(0)   '红色的是自定函数,可见最下面代码
  2.   
  3. If Mytable Is Nothing Then  
  4.     MessageBox.Show("没有记录不能导出数据""导出提示", MessageBoxButtons.OK, MessageBoxIcon.Information)  
  5.     Exit Sub  
  6. End If  
  7. Dim SaveFileDialog As FileDialog = New SaveFileDialog  
  8. If Mytable.Rows.Count > 0 Then  
  9.     Dim MyFileName As String  
  10.     Dim FileName As String = ""  
  11.     With SaveFileDialog  
  12.         .AddExtension = True '如果用户忘记添加扩展名,将自动加上  
  13.         .DefaultExt = "xls" '默认扩展名  
  14.         .Filter = "Excel文件(*.xls)|*.xls"  
  15.         .Title = "文件保存到"  
  16.         If .ShowDialog = Windows.Forms.DialogResult.OK Then  
  17.             FileName = .FileName  
  18.         End If  
  19.     End With  
  20.     MyFileName = Microsoft.VisualBasic.Right(FileName, 4)  
  21.     If MyFileName = "" Then  
  22.         Exit Sub  
  23.     End If  
  24.     If MyFileName = ".xls" Or MyFileName = ".XLS" Then  
  25.         Dim fs As FileStream = New FileStream(FileName, FileMode.Create)  
  26.         Dim sw As StreamWriter = New StreamWriter(fs, System.Text.Encoding.Default)  
  27.         sw.WriteLine(vbTab & FileName & vbTab & Date.Now)  
  28.         Dim i, j As Integer  
  29.         Dim str As String = ""  
  30.         For i = 0 To Mytable.Columns.Count - 1  
  31.             str = Mytable.Columns(i).Caption  
  32.             sw.Write(str & vbTab)  
  33.         Next  
  34.         sw.Write(vbCrLf)  
  35.         For j = 0 To Mytable.Rows.Count - 1  
  36.             For i = 0 To Mytable.Columns.Count - 1  
  37.                 Dim strRow As String  
  38.                 strRow = IIf(Mytable.Rows(j).Item(i) Is DBNull.Value, "", Mytable.Rows(j).Item(i))  
  39.                 sw.Write(strRow & vbTab)  
  40.             Next  
  41.             sw.Write(vbCrLf)  
  42.         Next  
  43.         sw.Close()  
  44.         fs.Close()  
  45.         MessageBox.Show("数据导出成功!""导出提示", MessageBoxButtons.OK, MessageBoxIcon.Information)  
  46.     Else  
  47.         Exit Sub  
  48.     End If  
  49. Else  
  50.     MessageBox.Show("没有记录不能导出数据""导出提示", MessageBoxButtons.OK, MessageBoxIcon.Information)  
  51. End If  

折叠C/C++ Code复制内容到剪贴板
  1. Public Function myDataSet(ByVal SQLStr As String) As DataSet  
  2.     dbName = My.Settings.dbNamer  
  3.   
  4.     Try  
  5.         ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Application.StartupPath & "\" & dbName & ".mdb" 
  6.         Conn = New OleDbConnection(ConnStr) 
  7.         Conn.Open() 
  8.         Dim da As OleDbDataAdapter = New OleDbDataAdapter(SQLStr, Conn) 
  9.         Dim ds As DataSet = New DataSet 
  10.         da.Fill(ds) 
  11.         Return ds 
  12.     Catch ex As Exception 
  13.         MsgBox(ex.Message.ToString, MsgBoxStyle.Information, "提示") 
  14.         ' MsgBox("请检查数据库名称是否正确!!", MsgBoxStyle.Information, "找不到数据库") 
  15.         'frmSet.txtDbName.Text = "data2003" 
  16.         dbName = My.Settings.dbNamer = "site"  
  17.         'frmMain.toolReReadData.PerformClick() 
  18.         ' frmSet.Show()  
  19.     Finally  
  20.         Conn.Close()  
  21.     End Try  
  22.     Return Nothing  '为去无返回警告  
  23. End Function  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值