我用的是VS2005,读取方法如下private void EcxelToGridView(string BtnExcPath,string sheetName)
{
//根据路径打开一个Excel文件并将数据填充到ds中
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + BtnExcPath + ";Extended Properties ='Excel 8.0;HDR=YES;IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = "select *,'我是文本'&科目代码 as 文本科目代码 from [" + sheetName + "]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
System.Data.DataSet ds = new System.Data.DataSet();
myCommand.Fill(ds, "table1");
conn.Close();
List<string> strList = new List<string>();
string str = string.Empty;
strList.Clear();
if (ds.Tables["table1"].Rows.Count == 0)
{
MessageBox.Show("要导入的Excel没有数据");
}
else
{
this.dataGridView1.DataSource = ds.Tables["table1"];
}
}
科目代码列数据是这样的一些数据
科目代码
5502.03
4105.07A
4104.03我本身的开发环境用的是Excel2003,读出科目代码这一列没问题。
但是我在客户测试(Excel2007),发现像4105.07A这样文本类型的就没有值,后面在Select中加了一列'我是文本'&科目代码 as 文本科目代码,对应没值得只有“我是文本”几个字,也没起到效果请问怎么在读的时候就控制读出的内容为文本?
谢谢
{
//根据路径打开一个Excel文件并将数据填充到ds中
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + BtnExcPath + ";Extended Properties ='Excel 8.0;HDR=YES;IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = "select *,'我是文本'&科目代码 as 文本科目代码 from [" + sheetName + "]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
System.Data.DataSet ds = new System.Data.DataSet();
myCommand.Fill(ds, "table1");
conn.Close();
List<string> strList = new List<string>();
string str = string.Empty;
strList.Clear();
if (ds.Tables["table1"].Rows.Count == 0)
{
MessageBox.Show("要导入的Excel没有数据");
}
else
{
this.dataGridView1.DataSource = ds.Tables["table1"];
}
}
科目代码列数据是这样的一些数据
科目代码
5502.03
4105.07A
4104.03我本身的开发环境用的是Excel2003,读出科目代码这一列没问题。
但是我在客户测试(Excel2007),发现像4105.07A这样文本类型的就没有值,后面在Select中加了一列'我是文本'&科目代码 as 文本科目代码,对应没值得只有“我是文本”几个字,也没起到效果请问怎么在读的时候就控制读出的内容为文本?
谢谢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货