CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VB >  基础类

如何把表格控件中的数据保存为Excel??

楼主dczlxl(野猫)2003-07-01 08:51:01 在 VB / 基础类 提问

如题! 问题点数:100、回复次数:9Top

1 楼mark3798(.net菜鸟)回复于 2003-07-01 09:04:19 得分 70

下面是我写的代码,你参照这看吧。应该能看懂的。  
  我用的控件是MShFlexGrid,记得引用一下Application   Excel哦!  
  Private   Sub   SaveToExcel_Click()  
  Dim   mrc   As   ADODB.Recordset  
  Dim   xlApp   As   Excel.Application  
  Dim   xlBook   As   Excel.Workbook  
  Dim   xlSheet   As   Excel.Worksheet  
  Dim   MsgText   As   String  
  Dim   filename   As   String  
  Dim   i   As   Integer  
  Dim   intCount   As   Integer  
   
  If   flagTedit   Then  
          CommonDialog1.Filter   =   "excel(*.xls)|*.xls"  
          CommonDialog1.ShowSave  
          If   CommonDialog1.filename   =   ""   Then  
                  Exit   Sub  
          End   If  
          filename   =   CommonDialog1.filename  
  Else  
          MsgBox   "你还没打开数据窗口!",   vbOKOnly   +   vbExclamation,   "警告"  
          Exit   Sub  
  End   If  
   
  Set   xlApp   =   CreateObject("Excel.Application")  
  Set   xlBook   =   xlApp.Workbooks.Add  
  Set   xlSheet   =   xlApp.Worksheets(1)  
   
  If   flagTedit   Then  
          xlSheet.Cells(1,   1).Value   =   "生产课号"  
          xlSheet.Cells(1,   2).Value   =   "批量号码"  
          xlSheet.Cells(1,   3).Value   =   "制品编号"  
          xlSheet.Cells(1,   4).Value   =   "品种"  
          xlSheet.Cells(1,   5).Value   =   "客户"  
          xlSheet.Cells(1,   6).Value   =   "制造/加工日期"  
          xlSheet.Cells(1,   7).Value   =   "B/M   No."  
          xlSheet.Cells(1,   8).Value   =   "钨丝NO."  
          xlSheet.Cells(1,   9).Value   =   "送出数"  
          xlSheet.Cells(1,   10).Value   =   "灯丝"  
          xlSheet.Cells(1,   11).Value   =   "灯头"  
          xlSheet.Cells(1,   12).Value   =   "流水线"  
          xlSheet.Cells(1,   13).Value   =   "材料投入数"  
          xlSheet.Cells(1,   14).Value   =   "工程检查"  
          xlSheet.Cells(1,   15).Value   =   "检查项目"  
          xlSheet.Cells(1,   16).Value   =   "检查日期"  
          xlSheet.Cells(1,   17).Value   =   "检查人"  
          xlSheet.Cells(1,   18).Value   =   "不良编号"  
          xlSheet.Cells(1,   19).Value   =   "不良名称"  
          xlSheet.Cells(1,   20).Value   =   "不良数"  
          xlSheet.Cells(1,   21).Value   =   "合计"  
          xlSheet.Cells(1,   22).Value   =   "入库数量"  
          xlSheet.Cells(1,   23).Value   =   "入库日期"  
          xlSheet.Cells(1,   24).Value   =   "备注"  
          For   intCount   =   1   To   frmJGZPChuanPiaoShow.msgList2.Rows   -   1  
                  frmJGZPChuanPiaoShow.msgList2.Row   =   intCount  
                          For   i   =   1   To   24  
                                  xlSheet.Cells(intCount   +   1,   i).Value   =   frmJGZPChuanPiaoShow.msgList2.TextMatrix(intCount,   i)  
                          Next   i  
          Next   intCount  
                  'xlSheet.Range(xlSheet.Cells(1,   1),   xlSheet.Cells((intCount),   (i   -   1))).Select  
                  xlSheet.Columns.AutoFit  
                  xlSheet.SaveAs   filename  
                  xlSheet.Application.Quit  
                  Set   xlSheet   =   Nothing  
  Else  
            MsgBox   "加工/制品传票一览表中没有记录,请重试!",   vbOKOnly   +   vbExclamation,   "警告"  
   
  End   If  
  End   Sub  
   
  Top

2 楼dandy1437((乐天派!直面所有问题))回复于 2003-07-01 09:05:08 得分 20

Dim   excel_app   As   Excel.Application  
          Dim   excel_sheet   As   Excel.Worksheet        
    Set   excel_app   =   CreateObject("Excel.Application")  
   
          excel_app.Visible   =   True  
          excel_app.Workbooks.add  
          Set   excel_sheet   =   excel_app.Workbooks(1).Worksheets(1)  
          excel_sheet.Cells(1,   9)   =   co_chinese_name  
          excel_sheet.Cells(2,   1)   =   "目的港"  
          excel_sheet.Cells(2,   2)   =   "承运人"  
          excel_sheet.Cells(2,   3)   =   "起运港"  
          excel_sheet.Cells(2,   4)   =   "级别"  
          excel_sheet.Cells(2,   5)   =   "货箱类型"  
          excel_sheet.Cells(2,   6)   =   "开船日期"  
          excel_sheet.Cells(2,   7)   =   "20'价格"  
          excel_sheet.Cells(2,   8)   =   "40'价格"  
          excel_sheet.Cells(2,   9)   =   "40HQ价格"  
          excel_sheet.Cells(2,   10)   =   "航程"  
          excel_sheet.Cells(2,   11)   =   "中转港"  
          excel_sheet.Cells(2,   12)   =   "生效日期"  
          excel_sheet.Cells(2,   13)   =   "实效日期"  
          excel_sheet.Cells(2,   14)   =   "报价(修改)日期"  
          excel_sheet.Cells(2,   15)   =   "备注"  
          excel_sheet.Cells(2,   16)   =   "付款日期"  
          excel_sheet.Cells(2,   17)   =   "运输方式"  
          excel_sheet.Cells(2,   18)   =   "报价协议号"  
          excel_sheet.Cells(2,   19)   =   "内序号"  
           
          excel_sheet.StandardWidth   =   10  
          For   P   =   1   To   spdInformation.MaxRows  
          For   N   =   1   To   spdInformation.MaxCols  
          spdInformation.Row   =   P  
          spdInformation.Col   =   N  
  '         If   N   =   12   Or   N   =   13   Or   N   =   14   Then  
  '             TXT   =   Format(TXT,   "yyyy-mm-dd")  
  '         End   If  
          TXT   =   spdInformation.Text  
          excel_sheet.Cells(P   +   2,   N)   =   Format(TXT)  
           
          Next   N  
          Next   P  
          excel_sheet.Cells(P   +   3,   1)   =   "本报价有相关软件支持,如需购买请以下联系方法联系"  
          excel_sheet.Cells(P   +   4,   1)   =   "上海顺勇货运服务有限公司"  
          excel_sheet.Cells(P   +   5,   1)   =   "电话"  
        excel_sheet.SaveAs   (App.Path   +   "\Outemail\WExecltemplate.xls")  
        excel_app.Quit  
  这个是以前做的一个东西,你参考一下吧Top

3 楼dczlxl(野猫)回复于 2003-07-01 10:13:27 得分 0

Dim   mrc   As   ADODB.Recordset  
  上面的语句出错!  
  该引用什么呀!Top

4 楼heng_s(渣渣)回复于 2003-07-01 10:40:03 得分 0

那是连接远程数据库的,没用数据库就不用那条语句.  
   
  你主要看人家的操作Excel的方法,数据库的不用管了Top

5 楼dczlxl(野猫)回复于 2003-07-01 15:36:08 得分 0

问题已经解决,即将给分!  
  不过,还有几个小问题:  
  1.当弹出保存对话框时,如何判断是按了确定按钮还是取消按钮?  
  2.如何去掉默认的保存文件名?  
  3.默认文件保存的不是所要格式怎么办(默认设置:CmmDlog1.Filter   =   "Exel(*.xls)|*.xls")?Top

6 楼heng_s(渣渣)回复于 2003-07-01 17:03:27 得分 0

1.按了取消按钮返回的文件名为空.  
  Top

7 楼frankwong(黄梓钿)回复于 2003-07-01 17:22:42 得分 10

to   dczlxl(野猫):  
  1、当点击取消时,不论有否输入文件名,filename属性为"";  
  2、在执行showsave之前,对filename属性进行赋值,如filename=""者去掉默认的保存文件名;  
  3、在保存对话框中,文件类型选择“所有文件”(如果有的话),然后输入完整文件名。Top

8 楼dczlxl(野猫)回复于 2003-07-01 17:31:15 得分 0

奇怪的是:当按了取消按钮返回的文件名为空!!  
  测试语句:If   CmmDlog1.FileName   =   ""   Then   Exit   Sub  
  我真搞不懂:在打开文件窗口时,当按了取消按钮返回的文件名为空;而在保存文件窗口时,当按了取消按钮返回的文件名不为空,文件名居然时打开时的文件???Top

9 楼dczlxl(野猫)回复于 2003-07-01 18:41:14 得分 0

5分钟后结帖!!Top

相关问题

  • 找表格控件
  • 表格控件 come in!
  • 求救:有什么控件可以打开/保存Excel文档?
  • datagrid中有子控件的话,保存成excel出错?
  • 如何制作自己的控件?比如类似excel表格的那种
  • 请教各位老大,EXCEL表格的控件是怎么做出来的?
  • [广告贴]EjunGrid1.0 一款高品质的Excel风格表格控件
  • 有誰做過在excel復制,然后貼到mshflexgrid的控件的表格中
  • 表格控件的应用
  • 谁有表格控件?

关键词

  • 文件名
  • 文件
  • excel
  • 数据库
  • xlsheet
  • 保存
  • cells
  • commondialog
  • 默认
  • xlapp

得分解答快速导航

  • 帖主:dczlxl
  • mark3798
  • dandy1437
  • frankwong

相关链接

  • Visual Basic类图书
  • Visual Basic类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo