D Deoline Unregistered / Unconfirmed GUEST, unregistred user! 2007-10-26 #1 当使用ListView控件显示数据时(ListView.ViewStyle设置为vsReport),如何将该控件的全部数据导出Excel呢,请教各位大侠,在线等。。。
清 清新空气 Unregistered / Unconfirmed GUEST, unregistred user! 2007-10-26 #2 var<br> i,j:integer;<br> StrList:TStringList;<br> FStr:String;<br>begin<br> StrList:=TStringList.Create;<br> try<br> for i := 0 To listview1.items.count - 1 do<br> begin<br> FStr:= listview1.Items.Caption;<br> for j := 0 to listview1.Items.SubItems.Count - 1 do<br> FStr := FStr + #9 + listview1.Items.Subitems[j];<br> StrList.Add( FStr <br> end;<br> StrList.SaveToFile('d:/aa.xls');<br> finally<br> StrList.Free;<br> end;<br>end;
var<br> i,j:integer;<br> StrList:TStringList;<br> FStr:String;<br>begin<br> StrList:=TStringList.Create;<br> try<br> for i := 0 To listview1.items.count - 1 do<br> begin<br> FStr:= listview1.Items.Caption;<br> for j := 0 to listview1.Items.SubItems.Count - 1 do<br> FStr := FStr + #9 + listview1.Items.Subitems[j];<br> StrList.Add( FStr <br> end;<br> StrList.SaveToFile('d:/aa.xls');<br> finally<br> StrList.Free;<br> end;<br>end;
D Deoline Unregistered / Unconfirmed GUEST, unregistred user! 2007-10-26 #3 这个写法的确是比较实用,也简单,但他还是解决不了我的问题,比如如何使他在导出之后直接打开该Excel表呢,还有就是,我要是想把导出去的数据在Excel中加上边框,那该如何处理啊?<br>大侠,能否再深入点,谢谢!
这个写法的确是比较实用,也简单,但他还是解决不了我的问题,比如如何使他在导出之后直接打开该Excel表呢,还有就是,我要是想把导出去的数据在Excel中加上边框,那该如何处理啊?<br>大侠,能否再深入点,谢谢!
J jjtl Unregistered / Unconfirmed GUEST, unregistred user! 2007-10-26 #4 使用oleVariant对象处理<br>procedure CopyTOClibrd;<br>var<br> i,j:integer;<br> StrList:TStringList;<br> FStr:String;<br>begin<br> StrList:=TStringList.Create;<br> try<br> for i := 0 To listview1.items.count - 1 do<br> begin<br> FStr:= listview1.Items.Caption;<br> for j := 0 to listview1.Items.SubItems.Count - 1 do<br> FStr := FStr + #9 + listview1.Items.Subitems[j];<br> StrList.Add( FStr <br> end;<br> Clipboard.AsText := strlist.Text; //修改了空气清晰的代码,其他的都照搬的<br> finally<br> StrList.Free;<br> end;<br>end; <br><br>procedure SaveListViewToExcel;<br>var<br> SaveDialog:TSaveDialog;<br> ExcelApp:Variant;<br>begin<br> if (DataSet.Active = false) or (DataSet.RecordCount = 0) then<br> begin<br> exit;<br> end;<br> SaveDialog:=TSaveDialog.Create(nil);<br> try<br> SaveDialog.DefaultExt := 'xls';<br> SaveDialog.Filter:='*.xls';<br> SaveDialog.FileName:='ExcelFileName'; <br> CopyTOClibrd;<br> excelApp := createoleobject('Excel.Application');<br> excelApp.visible := True; //显示打开excel<br> ExcelApp.WorkBooks.Add;<br> <br> ExcelApp.Range['A1:J'+inttostr(listview.count+1)].Select;<br> ExcelApp.Selection.NumberFormatLocal := '@' ;<br> //设置excel的cell格式为文件格式<br> excelApp.Range['A1'].PasteSpecial; //粘贴剪贴板中的内容到excel中<br> <br> //这里你就要自己处理了,可以在excel中录制宏,看vb代码<br> ExcelApp.Selection.HorizontalAlignment := alBottom;<br> excelApp.Selection.ColumnWidth := 18;<br> excelApp.Rows['1:1'].Select;<br> excelApp.Selection.Font.bold := True;<br> // <br> try<br> if SaveDialog.Execute then<br> begin<br> excelApp.ActiveWorkBook.SaveAs(saveDialog.FileName);<br> end<br> else<br> begin<br> excelApp.activeworkbook.saved := true; <br> end;<br> except<br> //异常显示<br> end;<br> finally<br> SaveDialog.Free;<br> excelApp.ActiveWorkBook.close; //如果要不关掉excel的话<br> excelApp.quit; //这两行就注释掉<br> clipboard.Clear;<br> clipboard.Close;<br> end;<br>end;<br><br>随便写的代码可能有问题 ,自己修改一下
使用oleVariant对象处理<br>procedure CopyTOClibrd;<br>var<br> i,j:integer;<br> StrList:TStringList;<br> FStr:String;<br>begin<br> StrList:=TStringList.Create;<br> try<br> for i := 0 To listview1.items.count - 1 do<br> begin<br> FStr:= listview1.Items.Caption;<br> for j := 0 to listview1.Items.SubItems.Count - 1 do<br> FStr := FStr + #9 + listview1.Items.Subitems[j];<br> StrList.Add( FStr <br> end;<br> Clipboard.AsText := strlist.Text; //修改了空气清晰的代码,其他的都照搬的<br> finally<br> StrList.Free;<br> end;<br>end; <br><br>procedure SaveListViewToExcel;<br>var<br> SaveDialog:TSaveDialog;<br> ExcelApp:Variant;<br>begin<br> if (DataSet.Active = false) or (DataSet.RecordCount = 0) then<br> begin<br> exit;<br> end;<br> SaveDialog:=TSaveDialog.Create(nil);<br> try<br> SaveDialog.DefaultExt := 'xls';<br> SaveDialog.Filter:='*.xls';<br> SaveDialog.FileName:='ExcelFileName'; <br> CopyTOClibrd;<br> excelApp := createoleobject('Excel.Application');<br> excelApp.visible := True; //显示打开excel<br> ExcelApp.WorkBooks.Add;<br> <br> ExcelApp.Range['A1:J'+inttostr(listview.count+1)].Select;<br> ExcelApp.Selection.NumberFormatLocal := '@' ;<br> //设置excel的cell格式为文件格式<br> excelApp.Range['A1'].PasteSpecial; //粘贴剪贴板中的内容到excel中<br> <br> //这里你就要自己处理了,可以在excel中录制宏,看vb代码<br> ExcelApp.Selection.HorizontalAlignment := alBottom;<br> excelApp.Selection.ColumnWidth := 18;<br> excelApp.Rows['1:1'].Select;<br> excelApp.Selection.Font.bold := True;<br> // <br> try<br> if SaveDialog.Execute then<br> begin<br> excelApp.ActiveWorkBook.SaveAs(saveDialog.FileName);<br> end<br> else<br> begin<br> excelApp.activeworkbook.saved := true; <br> end;<br> except<br> //异常显示<br> end;<br> finally<br> SaveDialog.Free;<br> excelApp.ActiveWorkBook.close; //如果要不关掉excel的话<br> excelApp.quit; //这两行就注释掉<br> clipboard.Clear;<br> clipboard.Close;<br> end;<br>end;<br><br>随便写的代码可能有问题 ,自己修改一下
C chyzpc Unregistered / Unconfirmed GUEST, unregistred user! 2007-10-26 #5 若问题还未解决可加我<br>qq:292044357
D Deoline Unregistered / Unconfirmed GUEST, unregistred user! 2007-10-26 #6 问题基本解决了,顺便问下,如何设置Excel单元格的背景颜色和边框的颜色。谢谢!<br>明天散分,非常感谢!