.net读取excel问题
怎样读取excel中34到44行的数据,但是excel中又没有唯一标识,该怎么写sql语句。
[最优解释]
读出Excel,到datatable。然后判断datatable的行数不就可以了。
/// <summary>
/// 连接Excel
/// </summary>
/// <returns></returns>
private OleDbConnection excelConn(string path)
{
OleDbConnection Conn = null;
string excelConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path.ToString().Trim() + ";" + "Extended Properties='Excel 8.0;hdr=no;IMEX=1'";
Conn = new OleDbConnection(excelConn);
return Conn;
}
public string ReadExcelToTempTable(string FName)
{
OleDbConnection connExcel = excelConn(FName);
string Error = "";
try
{
DataTable dt = new DataTable();
connExcel.Open();
string strCom = " SELECT * FROM [" + tableName + "]";///SQL操作语句,就是说:取得所有数据从Content
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, connExcel);
DataSet myDataSet = new DataSet();///建立新的数据集myDataSet
myCommand.Fill(myDataSet);///填充数据集
dt = myDataSet.Tables[0];//Exceltable
return Error;
}
catch (System.Exception e)
{
return e.ToString();
}
finally
{
connExcel.Close();
}
}
/// <param name="e"></param>
private void tsbtnImport_Click(object sender, EventArgs e)
{
//打开一个文件选择框
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Excel文件";
ofd.FileName = "";
ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);//为了获取特定的系统文件夹,可以使用System.Environment类的静态方法GetFolderPath()。该方法接受一个Environment.SpecialFolder枚举,其中可以定义要返回路径的哪个系统目录
ofd.Filter = "Excel文件(*.xls)
[其他解释]
每天回帖即可获得10分可用分
[其他解释]
string strCom = " SELECT * FROM [Shert1$A33:H44]";这种格式
[其他解释]
dgvTranslate.DataSource = EcxelToDataGridView(strName).Tables[0];
}
[其他解释]
该回复于2010-12-29 09:04:08被版主删除
[其他解释]
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + filePath + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";
[其他解释]
2楼,4楼是将文件当做数据库,读取其中的数据到DataTable,然后再用
for(int i=0;i<tb.rows.count;i++)
{
if(i=="33")
{}
}
应该行的通
[其他解释]
[Quote=引用:]
引用:
string strCom = " SELECT * FROM [Shert1$A33:H44]";这种格式
拜膜
[其他解释]
没试过,想学习一下!我还以为就导出EXl呢
[其他解释]