C#下导入excel到DataTable,不能读取工作簿表名,纠结啊解决方案
C#下导入excel到DataTable,不能读取工作簿表名,纠结啊!
远程运行后schemaTable.Rows.Count 为0
服务器本地运行为22。
是因为编码不同吗?我的web.config里2种编码都试了,还是不行啊。
<!--<globalization requestEncoding="gb2312" responseEncoding="gb2312" culture="zh-CN" fileEncoding="gb2312" uiCulture="en"/>-->
<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="zh-CN" fileEncoding="utf-8" />
网上查到的代码
HttpResponse resp;
253 resp = HttpContext.Current.Response;
254 resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
255 resp.AppendHeader("Content-disposition", "attachment;filename=" + FileName + ".xls");
256 resp.ContentType = "application/ms-excel";
这些应该怎么加列?谢谢
------解决方案--------------------
this.FileUpload1.PostedFile.FileName
这个文件在服务器上是不存在的,是读不到的。这个FileName指示了在客户机上的原始文件名。
你要把文件保存到临时文件夹再读取。
- C# code
strName = this.FileUpload1.PostedFile.FileName; txt0.Visible = true; txt0.Text += "<br />上传成功!开始判断导入文件名:" + strName + "!!!<br />"; #region 开始判断文件名 //if (this.FileUpload1.PostedFile.FileName != "") if (strName != "") { #region 开始导入工作薄名列表 string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strName + ";Extended Properties=Excel 8.0"; this.lbFileUpload1.Text = strConn; //aaa = strConn; //链接Excel OleDbConnection cnnxls = new OleDbConnection(strConn); OleDbCommand cmd = new OleDbCommand(); if (cnnxls.State == ConnectionState.Open) { cnnxls.Close(); cnnxls.Dispose(); cmd.Dispose(); } cnnxls.Open(); //DataTable schemaTable = cnnxls.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); DataTable schemaTable = cnnxls.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); txt0.Text += schemaTable.Rows.Count + "!!!<br />"; cnnxls.Close(); //读取Excel里面有 表Sheet1 if (schemaTable.Rows.Count > 0) { for (int i = 0; i < schemaTable.Rows.Count; i++) { string BiaoMC = schemaTable.Rows[i]["TABLE_NAME"].ToString(); ListItem li = new ListItem(BiaoMC, schemaTable.Rows[i]["TABLE_NAME"].ToString()); this.ddBiaoMC.Items.Insert(i, li); } } ............... } #endregion
远程运行后schemaTable.Rows.Count 为0
服务器本地运行为22。
是因为编码不同吗?我的web.config里2种编码都试了,还是不行啊。
<!--<globalization requestEncoding="gb2312" responseEncoding="gb2312" culture="zh-CN" fileEncoding="gb2312" uiCulture="en"/>-->
<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="zh-CN" fileEncoding="utf-8" />
网上查到的代码
HttpResponse resp;
253 resp = HttpContext.Current.Response;
254 resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
255 resp.AppendHeader("Content-disposition", "attachment;filename=" + FileName + ".xls");
256 resp.ContentType = "application/ms-excel";
这些应该怎么加列?谢谢
------解决方案--------------------
this.FileUpload1.PostedFile.FileName
这个文件在服务器上是不存在的,是读不到的。这个FileName指示了在客户机上的原始文件名。
你要把文件保存到临时文件夹再读取。