关于数据导出到Excel文件的保存问题

dunheng 2004-04-08 10:50:00
我用以下代码企图将数据表导出为一个Excel文件并保存.

Excel.Application myExcel=new Excel.ApplicationClass();
myExcel.Application.Workbooks.Add(true);
myExcel.Visible=true;


int rowIndex=1;
int colIndex=0;
this.table=ado.exSqlReturnTable("select * from FAQ");
foreach(DataColumn col in this.table.Columns)
{
colIndex++;
myExcel.Cells[1,colIndex]=col.ColumnName;
}

foreach(DataRow row in table.Rows)
{
colIndex=0;
rowIndex++;
foreach(DataColumn col in table.Columns)
{
colIndex++;
myExcel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}
}
//////////////////////////以上都没有问题
try
{
myExcel.Save("E:\a.xls");////此处异常
}
catch(Exception ex)
{
Trace.WriteLine(ex.ToString());
}


导出的过程没有问题,我们可以看到数据被成功的导出到了一个Excel文件中了,但是最后一句,保存的那行Save却出错了,提示错误信息"
Message "The file could not be accessed. Try one of the following:\n\n• Make sure the specified folder exists. \n• Make sure the folder that contains the file is not read-only.\n• Make sure the file name does not contain any of the following characters: < > ? [ ] : | or *\n• Make sure the file/path name doesn't contain more than 218 characters."
我确定我没有犯以上任何一个错误.代码贴出来,大家帮我把把关,谢了.
...全文
153 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
kob 2004-04-17
  • 打赏
  • 举报
回复
楼上的是不是要有个Excel的模板文件?
能否贴点代码出来?
李洪喜 2004-04-17
  • 打赏
  • 举报
回复
这种方式导出数据太慢了,
采用ADO。NET直接把EXCEL作为数据源进行操作,速度快多了。
starheart 2004-04-16
  • 打赏
  • 举报
回复
E:\\a.xls
eTopFancy 2004-04-16
  • 打赏
  • 举报
回复

在前面声明:
Excel.WorkBook myBook = ExcelApp.Workbooks.Add(true);
保存:
myBook.SaveAs(strfilepath,Missing.Value,Missing.Value,Missing.Value,
Missing.Value,Missing.Value,Excel.XlSaveAsAccessMode.xlShared,
Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
退出excel应用:
myExcel.Quit();
GC.Collect();
zxj7611 2004-04-16
  • 打赏
  • 举报
回复
你的exSqlReturnTable()函数是什么意思?
lazygui 2004-04-12
  • 打赏
  • 举报
回复
试试
myExcel.Save(@"E:\a.xls");

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧