VB里怎样将datagrid记录导出excel
其实方法很多的,下面代码是将datagrid的记录逐行写入EXCELDimExcelAsObjectDimWorkBookAsObjectDimWorkSheetAsObjectDimRangeXAsObjectDimRAsLong,cAsLong,ccAsLongDimbmAsVariantOnErrorGoTo0SetExcel=CreateObject("excel.application")SetWorkBook=Excel.Workbooks.AddExcel.WindowState=2Excel.Visible=True'打开excel文件SetWorkSheet=Excel.Worksheets(1)WorkSheet.Cells.NumberFormatLocal="@"'将sheet1格式设置为“文本”格式WorkSheet.Columns(1).ColumnWidth="14"WorkSheet.Columns(2).ColumnWidth="35"Withdatagridbm=.Bookmark'是否有标题IfTrim(.Caption)<>""ThenWorkSheet.Cells(1,1)=Frame3.CaptionWorkSheet.Cells(1,2)=bcText.TextEndIf'表头R=2cc=1Forc=0To.Columns.Count-1If.Columns(c).Visible=TrueThenWorkSheet.Cells(R,cc)=.Columns(c).Captioncc=cc+1EndIfNextc'内容R=R+1IfmSQL.Recordset.EOFAndmSQL.Recordset.BOFThenWorkBook.CloseSetWorkSheet=NothingSetWorkBook=NothingSetExcel=NothingExitSubEndIfadodc1.Recordset.MoveFirstWhileNotadodc1.Recordset.EOFcc=1Forc=0To.Columns.Count-1If.Columns(c).Visible=TrueThenWorkSheet.Cells(R,cc)=.Columns(c).Textcc=cc+1EndIfNextcR=R+1adodc1.Recordset.MoveNextWend.Bookmark=bmEndWith'xlApp.Visible=Fals'xlBook.Save'xlBook.Close'xlApp.Quit