导出的Excel2007文件打不开,提示Excel无法打开文件"2007.xlsx",因为文件格式或扩展名无效。请确定文件未损坏,并且文件扩展名与文件的格式匹配。
但把后扩展名改成.xls后就可以打开
以下是代码:
public void TBLtoExcel(string slx, int clmN, string[] clmname, string filenm, string path, string bb)
{
int i, j, m, n;
if (slx == "") return;
string urlPath = HttpContext.Current.Request.ApplicationPath + "/files/";
string physicPath = HttpContext.Current.Server.MapPath(urlPath);
string fileName = filenm + bb;
//string path = physicPath + fileName;
System.IO.File.Delete(physicPath + fileName);
string strConn = "";
if (bb == ".xls") strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + physicPath + fileName + ";Extended Properties=Excel 8.0;";
else strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + physicPath + fileName + ";Extended Properties='Excel 12.0;'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
System.Data.OleDb.OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
string tblname;
if (title == "") tblname = "Sheet1"; else tblname = title;
string strsql = "Create table[" + tblname + "](" + clmname[0].Split(' ')[0] + " " + clmname[0].Split(' ')[1];
for (i = 1; i < clmname.Length; i++) strsql += "," + clmname[i].Split(' ')[0] + " " + clmname[i].Split(' ')[1];
strsql += ")";
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
string str = slx + "类型商品导入格式说明:删除此行后添加商品数据,再导入,第一列是商品类型,默认为" + slx + ",不需填写;商品编码和商品名称不可为空。";
strsql = "INSERT INTO [" + tblname + "] (" + clmname[0].Split(' ')[0] + ") values ('" + str + "')";
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
conn.Close();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.WriteFile(physicPath + fileName);
string httpHeader = "attachment;filename=" + fileName;
response.AppendHeader("Content-Disposition", httpHeader);
response.Flush();
System.IO.File.Delete(physicPath + fileName);//删除临时文件
response.End();
}
求高手帮忙
但把后扩展名改成.xls后就可以打开
以下是代码:
public void TBLtoExcel(string slx, int clmN, string[] clmname, string filenm, string path, string bb)
{
int i, j, m, n;
if (slx == "") return;
string urlPath = HttpContext.Current.Request.ApplicationPath + "/files/";
string physicPath = HttpContext.Current.Server.MapPath(urlPath);
string fileName = filenm + bb;
//string path = physicPath + fileName;
System.IO.File.Delete(physicPath + fileName);
string strConn = "";
if (bb == ".xls") strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + physicPath + fileName + ";Extended Properties=Excel 8.0;";
else strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + physicPath + fileName + ";Extended Properties='Excel 12.0;'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
System.Data.OleDb.OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
string tblname;
if (title == "") tblname = "Sheet1"; else tblname = title;
string strsql = "Create table[" + tblname + "](" + clmname[0].Split(' ')[0] + " " + clmname[0].Split(' ')[1];
for (i = 1; i < clmname.Length; i++) strsql += "," + clmname[i].Split(' ')[0] + " " + clmname[i].Split(' ')[1];
strsql += ")";
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
string str = slx + "类型商品导入格式说明:删除此行后添加商品数据,再导入,第一列是商品类型,默认为" + slx + ",不需填写;商品编码和商品名称不可为空。";
strsql = "INSERT INTO [" + tblname + "] (" + clmname[0].Split(' ')[0] + ") values ('" + str + "')";
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
conn.Close();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.WriteFile(physicPath + fileName);
string httpHeader = "attachment;filename=" + fileName;
response.AppendHeader("Content-Disposition", httpHeader);
response.Flush();
System.IO.File.Delete(physicPath + fileName);//删除临时文件
response.End();
}
求高手帮忙
response.WriteFile(physicPath + fileName);好吧是B/S的
服务器上的COM 应该是2003的把。
要不是不会有这个问题的。string fileName = filenm + bb; 换.xls 客户端都是高版本支持低版本。
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + physicPath + fileName + ";Extended Properties='Excel 12.0 Xml;'";