VB导出到EXCEL
现在VB到成EXCEL是用EXEC master..xp_cmdshell 'bcp "Select * from pubs..jobs" queryout c:\Out.txt -c -SHTSQL02 -Uitsa -Psa1234 '
还是用其他的 ?
用此中方法导出的文件好像在服务器路径下面,不是在你本机路径?
用VB调用的语句怎么写?我写的都提示有错误!
strSQL = "master..xp_cmdshell 'bcp "select appr_status,BATCH_NO,REF_NO,ITEM_NO,ITEM_DESCE,oms_itemno,ITEM_GRP,MODEL_NO,CUST_PN,REV,DomeSale, " & _
"MANU_NAME,MANU_PN,PROCUR,SPEC_PROCU,UOM,NET_W,NET_UNIT,CUST_CODE,Safe_Mat,mold_code, " & _
"material_type,factory_code,item1,item2,item3,item4,item5,item6,item7,item8 ,i_date,by_user,export_fg, conrohs = case itemmstr.rohs when 1 then 'ROHSR0' when 2 then 'ROHSR1' when 3 then 'ROHSR2' when 4 then 'ROHSRE' else '' end,suggest_itemno,batchmgt From itemmstr where '" & strRefFrom & "'<=REF_NO and REF_NO<='" & strRefTo & "' order by REF_NO " queryout c:\temp\itemmstr.xls -c -SHTSQL02 -Usa -Phtit2005'"
cn.Execute (strSQL)
帮忙修改下!谢谢!! --------------------编程问答-------------------- 自己顶下!!有了解的帮帮忙!谢谢!!
--------------------编程问答--------------------
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlRange As Excel.Range
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
' 设置统计信息
With xlSheet
.Cells.Font.Name = "宋体"
.Cells.Font.Size = 12
.Range(.Cells(1, 1), .Cells(1, 2)).Font.Bold = True
.Cells(1, 1) = "支出统计"
.Cells(2, 1) = "统计单位:"
.Cells(2, 2) = sTotalTimeType
.Cells(3, 1) = lblOutFromTodate.Caption ' 统计日期期间
.Cells(4, 1) = "金额:"
.Cells(4, 2) = sMoney
.Cells(5, 1) = "金额类型:"
.Cells(5, 2) = sMoneyValueType
.Cells(6, 1) = "支出类型:"
.Cells(6, 2) = sType
.Cells(7, 1) = "小分类:"
.Cells(7, 2) = sTypeSmall
.Cells(8, 1) = "浪费的钱:"
.Cells(8, 2) = sLangfei
End With
Dim iBaseNum As Integer
iBaseNum = 10
' 统计结果
For i = 0 To MSFlexGridOut.Rows - 1
For j = 0 To MSFlexGridOut.Cols - 1
Set xlRange = xlSheet.Range(xlSheet.Cells(i + iBaseNum, j + 1), xlSheet.Cells(i + iBaseNum, j + 1))
xlRange.Borders(xlEdgeBottom).Weight = 2
xlRange.Borders(xlEdgeTop).Weight = 2
xlRange.Borders(xlEdgeLeft).Weight = 2
xlRange.Borders(xlEdgeRight).Weight = 2
' 第一列左对齐
If j = 0 Then
xlRange.HorizontalAlignment = xlLeft '对齐方式
End If
' 标题设置为粗体
If i = 0 Then
xlRange.Font.Bold = True
xlRange.HorizontalAlignment = xlCenter '对齐方式
End If
If i Mod 2 = 1 Then
xlSheet.Cells(i + iBaseNum, j + 1).Interior.ColorIndex = 34 ' 设置隔行颜色,ColorIndex对应的颜色 0~56
End If
If j > 0 Then
xlSheet.Cells(i + iBaseNum, j + 1).ColumnWidth = 12 ' 设置除第一列外的列宽度
If MSFlexGridOut.Cols = 6 Then
If j = 1 Or j = 2 Then
xlSheet.Cells(i + iBaseNum, j + 1).ColumnWidth = 8 ' 设置财务年,财务周、财务月、财务季的列宽度
End If
End If
End If
xlSheet.Cells(i + iBaseNum, j + 1) = MSFlexGridOut.TextMatrix(i, j)
Next j
Next i
' 保存
xlBook.SaveAs FileName:=mfile, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
Set xlSheet = Nothing
xlBook.Close False
Set xlBook = Nothing
xlApp.Quit '// 结束Excel对象
Set xlApp = Nothing '// 释放xlApp对象
以上是我写的从MSFlexGrid控件导出到excel文件的代码,
看看对你有没有帮助,另外,需要在Project/Refrence中添加Microsoft Excel 11.0 Object Libray --------------------编程问答-------------------- 我导出的不是DataGrid的数据,是用sql查询出来的 !!!然后直接导入EXCEL
--------------------编程问答--------------------
改写一下不就可以了吗?
把DataGrid换成RecordSet记录集 --------------------编程问答-------------------- 我们是一次导9个EXCEL!不是一个画面,一个画面导的 !! --------------------编程问答--------------------
这个绝对正解,很标准 --------------------编程问答-------------------- 这个和前面的一样的啊 !!是去DataGrid的 !
--------------------编程问答-------------------- 数据多会慢一点.只是个例子
Public ex As Excel.Application--------------------编程问答--------------------
Public wb As Excel.Workbook
Public sh As Excel.Worksheet
Private Sub Command1_Click()
Dim i As Long, A As Long
Dim arr() As String, str As String
Dim Path As String
Set ex = CreateObject("Excel.Application")
Set wb = ex.Workbooks.Add '新建excel
Set sh = wb.Sheets(1) '第一个工作表
str = "A,C,B,D,F" '你要导入的数据
'导入
arr = Split(str, ",")
For i = 0 To UBound(arr)
sh.Cells(i + 1, 1) = arr(i)
Next i
Path = "C:\book1.xls" '保存路径
ActiveWorkbook.SaveAs FileName:=Path, FileFormat:=xlNormal, _
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False
wb.Close SaveChanges:=True '直接关闭不保存
ex.Quit
MsgBox "数据导入完毕-" & "保存在:" & Path, , "提示"
'ex.Visible = True
Set ex = Nothing
Set wb = Nothing
Set sh = Nothing
End Sub
EXEC master..xp_cmdshell 'bcp "Select * from pubs..jobs" queryout c:\Out.txt -c -SHTSQL02 -Uitsa -Psa1234 '
有这样做的朋友吗 ?? 类似这样的 !!! --------------------编程问答--------------------
strSQL="INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',--------------------编程问答--------------------
'Excel 8.0;Database=c:\temp\itemmstr.xls;',
'SELECT select appr_status,BATCH_NO,REF_NO,ITEM_NO,ITEM_DESCE,oms_itemno,ITEM_GRP,MODEL_NO,CUST_PN,REV,DomeSale, MANU_NAME,MANU_PN,PROCUR,SPEC_PROCU,UOM,NET_W,NET_UNIT,CUST_CODE,Safe_Mat,mold_code, material_type,factory_code,item1,item2,item3,item4,item5,item6,item7,item8 ,i_date,by_user,export_fg, conrohs ,suggest_itemno,batchmgt FROM [Sheet1$]')
SELECT select appr_status,BATCH_NO,REF_NO,ITEM_NO,ITEM_DESCE,oms_itemno,ITEM_GRP,MODEL_NO,CUST_PN,REV,DomeSale, MANU_NAME,MANU_PN,PROCUR,SPEC_PROCU,UOM,NET_W,NET_UNIT,CUST_CODE,Safe_Mat,mold_code, material_type,factory_code,item1,item2,item3,item4,item5,item6,item7,item8 ,i_date,by_user,export_fg, conrohs ,suggest_itemno,batchmgt FROM itemmstr where '" & strRefFrom & "'<=REF_NO and REF_NO<='" & strRefTo & "' order by REF_NO "
cn.Execute (strSQL)
10楼的兄弟,能详细一点吗 ? 不是很明白!!
网上关于INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',
这个好像讲的不大一样!谢谢!!
补充:VB , 基础类