如何处理exce里统计话费的功能
我们公司有个软件,就是可以把每台分机的费用都导入到excel表里,但是没有统计功能,不能统计出每台分机这个月的总费用,只有每台分机一个月里每天的费用,我现在想把exce里的数据用vb.net的一个按扭操作就导入到事先设定好字段的sqlserver的表里,最好用OleDbDataAdapter控件,导入后,我便可以写统计代码里,问题就是导入这里,请问exce里的格式关系导入操作吗。有高手帮我回答下,分全拿出来了,虽然只有80分,但已经是我的全部了。 问题点数:0、回复次数:2Top
1 楼wudiccj(软件黄牛)回复于 2005-04-01 09:50:14 得分 0
还有能不能用OleDbDataAdapter控件,直接连接excelTop
2 楼landlordh(work wonders)回复于 2005-04-01 09:58:02 得分 0
' Power by: landlordh
' 添加引用excel的COM控件
' Imports System.Data.OleDb
Function loadexecl() As DataTable
Dim path As String
Dim OpenFileDialog1 As New OpenFileDialog
OpenFileDialog1.AddExtension = True
OpenFileDialog1.CheckFileExists = True
OpenFileDialog1.Title = "请选择要导入的文件"
OpenFileDialog1.Filter = "*.Excel文件|*.xls"
OpenFileDialog1.ShowDialog()
path = OpenFileDialog1.FileName
Dim sheetnames As String
If Trim(path).Length > 8 Then
Try
Dim tempexcel As New Excel.Application
tempexcel.Workbooks.Open(path)
Dim i As Integer
For i = 1 To tempexcel.Application.Worksheets.Count
sheetnames &= " " & tempexcel.Worksheets.Item(i).name & Chr(10)
Next
tempexcel.Quit()
Catch ex As Exception
MsgBox(ex.Message)
Finally
GC.Collect()
End Try
Dim sheet As String
sheet = Trim(InputBox("可供选择的表名有:" & Chr(10) & sheetnames & "(注:只可填一个!)", "请输入要导入的表名"))
If sheet.Length > 0 Then
Dim myConn As OleDbConnection
Dim myAdaptor As OleDbDataAdapter
Dim ds As New DataSet
Try
myConn = New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;data source=" & path)
If myConn.State <> ConnectionState.Open Then myConn.Open()
myAdaptor = New OleDbDataAdapter("Select * From [" & sheet & "$]", myConn)
myAdaptor.Fill(ds, "ExcelData")
Return ds.Tables(0)
Catch ex As Exception
MsgBox(ex.Message)
Finally
If myConn.State <> ConnectionState.Closed Then myConn.Close()
End Try
End If
End If
End Function
//新表插入
SELECT * into Table1 FROM
OPENROWSET('MICROSOFT.JET.OLEDB.4.0'
,'Excel 5.0;HDR=YES;DATABASE=c:\test.xls',sheet1$)
//然后:向已有表插入数据
INSERT INTO Table1
SELECT * FROM OpenRowSet('microsoft.jet.oledb.4.0','Excel 8.0;
HDR=NO;IMEX=1;database=c:\test2.xls;','select * from [Sheet1$]')
方法2,
使用OpenDataSource进行插入操作,可以实现指定列的插入(同一记录其他列则为NULL)
insert OPENDATASOURCE
('SQLOLEDB','Data Source=服务器ip;User ID=用户名;Password=密码')
.SXSBD.dbo.Table1(字段1,字段2)
SELECT 字段1,字段2
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:\test.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...sheet1$
要注意,
1。使用转义字符。写在程序中应该是c:\\temp\\test1.xls
2。导入好像只认本机文件,所以需要把excel文件上传到server,写一段代码即可
Top




