从系统生成的excel文件中读取数据时出错. [英] Error while read data from system generatd excel file.
本文介绍了从系统生成的excel文件中读取数据时出错.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试上传系统生成的excel文件时出现以下错误.
External table is not in the expected format.
对于生成excel文件:-
Hi,
I am getting following error while i am trying to upload system generated excel file.
External table is not in the expected format.
For Generate excel File :-
public static void ExportToExcel(DataTable dtExportExcel, string FileName)
{
DataTable dt = dtExportExcel;
string attachment = "attachment; filename=" + FileName.ToString().Trim() + ".xls";
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel;tab delimated;";
string tab = "";
foreach (DataColumn dc in dt.Columns)
{
HttpContext.Current.Response.Write(tab + dc.ColumnName);
tab = "\t";
}
HttpContext.Current.Response.Write("\n");
int i;
foreach (DataRow dr in dt.Rows)
{
tab = "";
for (i = 0; i < dt.Columns.Count; i++)
{
HttpContext.Current.Response.Write(tab + dr[i].ToString());
tab = "\t";
}
HttpContext.Current.Response.Write("\n");
}
HttpContext.Current.Response.End();
}
用于上传生成的文件:-
For Uploading The generated file :-
public DataTable exceldata(string _sfile)
{
DataTable dtExcel = new DataTable();
OleDbConnection xlconn = new OleDbConnection();
xlconn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + _sfile + "';Extended Properties=Excel 8.0;");
xlconn.Open(); //here i am getting error External table is not in the expected format.
OleDbDataAdapter xlda = new OleDbDataAdapter("Select * from [" + _sfile + "]", xlconn);
DataTable xldt = new DataTable();
xlda.Fill(xldt);
xlconn.Close();
xlconn.Dispose();
return xldt;
}
谢谢
[edit]对代码块进行了较小的更改,以使第二个代码片段易于读取-OriginalGriff [/edit]
Thanks
[edit]Minor change to code blocks to make the second code fragment readable - OriginalGriff[/edit]
推荐答案
请尝试使用包含答案的方法.
Excel错误-外部表的格式不正确. [
Try this which has answers.
Excel error - External table is not in the expected format.[^]
这篇关于从系统生成的excel文件中读取数据时出错.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文