关于数据导出到Excel文件的保存问题
我用以下代码企图将数据表导出为一个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."
我确定我没有犯以上任何一个错误.代码贴出来,大家帮我把把关,谢了.