DataSet ds = new DataSet();
myCommand.Fill(ds, "newtable");
我的数据都已经存放在ds内,并且数据的记录数和excel一致。问题出现在从ds里面读取数据的时候
for (int i = 0; i < ds.Tables["newtable"].Rows.Count; i++)
{
我是循环读取。
抓取数据的代码是ds.Tables["newtable"].Rows[i][2].ToString()。这个只能读取60000001这样的数据,而字母开头的无法读取,为什么?比如A0000001。
}
myCommand.Fill(ds, "newtable");
我的数据都已经存放在ds内,并且数据的记录数和excel一致。问题出现在从ds里面读取数据的时候
for (int i = 0; i < ds.Tables["newtable"].Rows.Count; i++)
{
我是循环读取。
抓取数据的代码是ds.Tables["newtable"].Rows[i][2].ToString()。这个只能读取60000001这样的数据,而字母开头的无法读取,为什么?比如A0000001。
}
解决方案 »
- C# 获得IP问题
- 如何在主界面显示登陆时的账号
- split 分割中文字的问题
- C#怎么截取Textbox里的前两个中文字
- 一个返回SqlDataAdapter类型的方法,如果出现异常被catch了,我应该返回一个什么值呢?
- 使用水晶报表的问题:应用程序的报表预览时总是弹出一对话框,要求输入密码,我想问如何在编程时就指定密码(即保存密码),而不需要每次
- 大家给点建议
- 挑战鼠标拖放的效果
- 求winform DataGrid 增删改查(无BUG)源码谢谢 请发到[email protected]
- 急: 使用远程桌面测试C#写的程序,遇到了奇怪的问题??大家帮帮忙
- 很新的新手提问:C#中如何创建txt文件?如何创建内存映射文件并保持映射文件和硬盘文件同步?
- Graphics画图,图形的刷新问题
string filePath = "";
if (this.File1.Value == "")
{
// Comm.Jscript.Alert("请先选择您要导入的文件!");
}
else
{
int index = fileName.LastIndexOf(".");
if (index > 0)
{
if (fileName.Substring(index) == ".xls")
{
DateTime now = DateTime.Now;
fileName = now.ToShortDateString() + now.ToLongTimeString();
fileName = fileName.Replace("-", "").Replace(":", "").Replace(" ", "");
filePath = @"../Example/uploads/" + fileName + ".xls";
this.File1.PostedFile.SaveAs(Server.MapPath(filePath)); }
else
{
//Comm.Jscript.Alert("读入的文件不是XLS");
}
}
}
if(filePath != "")
{
fileName = Request.MapPath(filePath);
// string strConn ="Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + Dir + "\\"+fileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1;\"
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ fileName +";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
string query = "SELECT * FROM [Sheet1$]"
;
OleDbCommand oleCommand = new OleDbCommand(query, new OleDbConnection(strConn));
OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand);
DataSet myDataSet = new DataSet(); // 将 Excel 的[Sheet1]表内容填充到 DataSet 对象
try
{
oleAdapter.Fill(myDataSet,"[Sheet1$]");
// 数据绑定
this.GridView1.DataSource = myDataSet;
this.GridView1.DataMember = "[Sheet1$]";
this.GridView1.DataBind();
this.GridView1.Visible = true;
this.btnSave.Visible = true;
}
catch(Exception exx)
{
Response.Write(exx.Message);
//Comm.Jscript.Alert("注意:请用默认的Sheet1$页名称!");
}
finally
{
if(File.Exists(filePath))
{
File.Delete(filePath);
}
}
}代码给你了,自己调试吧
IMEX設定是否將混合型做為文本讀取。0-輸出模式;1-輸入模式;2-鏈接模式(完全更新能力)。
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string strSheetName = dt.Rows[0][2].ToString().Trim();string strExcel = "select * from [" + strSheetName + "]";
OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
adapter.Fill(ds, strSheetName);
conn.Close();//IMEX:将该列作为文字列
因为你第一行该列为数字,所以自动将其作为数字处理
好像IMEX也不能保证都正确,设置IMEX后,根据前8行中所占比例来确定该列类型,如果数字比列占多,那么也会将该列作为数字来处理,所以也不是万全之策
后来我通过Range先把数据转换成数组,再来处理的