急救:excel导出问题 ( 积分: 60 )

  • 主题发起人 主题发起人 cyndi_blue
  • 开始时间 开始时间
C

cyndi_blue

Unregistered / Unconfirmed
GUEST, unregistred user!
我在excel导出的时候出现如下的错误:<br>Rasied&nbsp;&nbsp;&nbsp;Exception&nbsp;&nbsp;&nbsp;class&nbsp;&nbsp;&nbsp;EoleException&nbsp;&nbsp;&nbsp;with&nbsp;&nbsp;&nbsp;message&nbsp;&nbsp;&nbsp;'无效索引'。<br>代码出错的地方:<br>&nbsp;Screen.Cursor:=crHourGlass;<br>&nbsp;ExcelApplication1.Connect;<br>&nbsp;ExcelApplication1.Workbooks.Add(Null,0);<br>&nbsp;ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks[1]);<br>&nbsp;ExcelWorkSheet1.ConnectTo(ExcelWorkBook1.Sheets[1]&nbsp;as&nbsp;_WorkSheet);<br>&nbsp;ExcelWorkSheet2.ConnectTo(ExcelWorkBook1.Sheets[2]&nbsp;as&nbsp;_WorkSheet);<br>&nbsp;ExcelWorkSheet3.ConnectTo(ExcelWorkBook1.Sheets[3]&nbsp;as&nbsp;_WorkSheet);<br>//&nbsp;ExcelWorkSheet4.ConnectTo(ExcelWorkBook1.Sheets[4]&nbsp;as&nbsp;_WorkSheet);<br>如果没有加上ExcelWorkSheet4这句的话,一切正常,加上后就出现错误。<br>问题是不是不能4个sheets,最多只能3个sheets&nbsp;吗?&nbsp;高手请指教。在线等。<br>&nbsp;ExcelWorkSheet1.Name:='Page1';<br>&nbsp;ExcelWorkSheet2.Name:='Page2';<br>&nbsp;ExcelWorkSheet3.Name:='Page3';<br>&nbsp;ExcelWorkSheet1.Columns.ColumnWidth:=15;<br>&nbsp;ExcelWorkSheet1.Columns.Font.Size:=10;<br>&nbsp;ExcelWorkSheet2.Columns.Font.Size:=10;<br>&nbsp;ExcelWorkSheet3.Columns.Font.Size:=10;<br>&nbsp;ExcelWorkSheet1.Columns.NumberFormatLocal:='@';&nbsp;&nbsp;//设置EXCEL表为文本格式<br>&nbsp;ExcelWorkSheet2.Columns.NumberFormatLocal:='@';&nbsp;&nbsp;<br>&nbsp;ExcelWorkSheet3.Columns.NumberFormatLocal:='@';<br>&nbsp;self.ProgressBar1.Max:=DM.ADO_PRO2EXCEL.RecordCount;<br>&nbsp;self.ProgressBar1.Position:=0;&nbsp;<br>。。。。。。
 
晕,因为excel创建一个workbook,默认只有3个sheet,所以你访问第4个时,不存在,当然报错了<br>你可以这样<br>for&nbsp;i:=1&nbsp;to&nbsp;Excel.WorkSheets.count&nbsp;do<br>begin<br>&nbsp;&nbsp;ExcelWorkSheet.ConnectTo(ExcelWorkBook.Sheets&nbsp;as&nbsp;_WorkSheet);<br>&nbsp;&nbsp;ExcelWorkSheet.Name:='Page'+inttostr(i);<br>&nbsp;&nbsp;ExcelWorkSheet.Columns.ColumnWidth:=15;<br>&nbsp;&nbsp;ExcelWorkSheet.Columns.Font.Size:=10;<br>&nbsp;&nbsp;ExcelWorkSheet.Columns.NumberFormatLocal:='@';&nbsp;&nbsp;//设置EXCEL表为文本格式<br>end;<br><br>如果你要增加sheet用这句:<br>ExcelWorkBook.Sheets.add;
 
请问hs-kill,我现在只想要在workbook1增加第四个sheet,在哪里写代码?<br>ExcelWorkBook.Sheets.add;&nbsp;这句好像不是这样的,<br>&nbsp;ExcelWorkBook1.Sheets.Add();&nbsp;括号还要写参数。。
 
?你用的是excel控件?&nbsp;我上面的代码是olevariant+com接口的.....<br>参数,你这样写:<br>ExcelWorkBook1.Sheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0);
 
代码,你可以在这里加:<br>ExcelWorkSheet3.ConnectTo(ExcelWorkBook1.Sheets[3]&nbsp;as&nbsp;_WorkSheet);<br>if&nbsp;ExcelWorkBook1.Sheets.count=3&nbsp;then<br>begin<br>&nbsp;&nbsp;ExcelWorkBook1.Sheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0);&nbsp;<br>&nbsp;&nbsp;ExcelWorkSheet4.ConnectTo(ExcelWorkBook1.Sheets[4]&nbsp;as&nbsp;_WorkSheet);<br>end;
 
太感谢你了。已经可以了。<br>但是有点不一样的,我是这样做的。<br>&nbsp;Screen.Cursor:=crHourGlass;<br>&nbsp;ExcelApplication1.Connect;<br>&nbsp;ExcelApplication1.Workbooks.Add(Null,0);<br>&nbsp;ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks[1]);<br>&nbsp;ExcelWorkBook1.Sheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0);<br>&nbsp;ExcelWorkSheet1.ConnectTo(ExcelWorkBook1.Sheets[1]&nbsp;as&nbsp;_WorkSheet);<br>&nbsp;ExcelWorkSheet2.ConnectTo(ExcelWorkBook1.Sheets[2]&nbsp;as&nbsp;_WorkSheet);<br>&nbsp;ExcelWorkSheet3.ConnectTo(ExcelWorkBook1.Sheets[3]&nbsp;as&nbsp;_WorkSheet);<br>&nbsp;ExcelWorkSheet4.ConnectTo(ExcelWorkBook1.Sheets[4]&nbsp;as&nbsp;_WorkSheet);
 
接受答案了.
 
后退
顶部