asp.net excel 的导入导出
excel07或者03能够导入数据库,但是我把从网页上导出的excel(格式和我要求的一样)再导入时就会出现“Externaltableisnotintheexpecte...
excel07或者03能够导入数据库,但是我把从网页上导出的excel(格式和我要求的一样)再导入时就会出现 “External table is not in the expected format. “的错误。不论导出的表格存为07或者03. 但我新建一个表再把网站上保存下来的表的内容复制到新的表中又是可以导入成功的。应该是导出的部分有问题,请给位赐教~
这是导出的 代码
Export("application/ms-excel", "是非题导入格式.xlsx");
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
//table1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
这是导入部分关键的两句:
string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 12.0; HDR=YES; IMEX=1'";
string strcom = "select *from [Sheet1$]"; 展开
这是导出的 代码
Export("application/ms-excel", "是非题导入格式.xlsx");
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
//table1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
这是导入部分关键的两句:
string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 12.0; HDR=YES; IMEX=1'";
string strcom = "select *from [Sheet1$]"; 展开
展开全部
07和03的连接代码不一样
if(".xls".Equals(suffix.ToLower()))//03
{
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';";
}
if(".xlsx".Equals(suffix.ToLower()))//07
{
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=YES'";
}
if(".xls".Equals(suffix.ToLower()))//03
{
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';";
}
if(".xlsx".Equals(suffix.ToLower()))//07
{
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=YES'";
}
追问
我没纠结这个了,弄了个专门让用户下载的标准格式,而不是把网页上的table转化成excel
我也试过这种方法,还是一样的。其实07应该是兼容03的,不必要用两句。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询