读书人

.net读取excel有关问题

发布时间: 2012-12-14 10:33:07 作者: rapoo

.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();
}

}


[其他解释]
这里给你完整的答案
[其他解释]
public DataSet EcxelToDataGridView(string filePath)
{
OleDbConnection conn = null;
DataSet ds2 = null;
try
{
//根据路径打开一个Excel文件并将数据填充到DataSet中
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + filePath + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";//导入时包含Excel中的第一行数据,并且将数字和字符混合的单元格视为文本进行导入
conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = "select * from [sheet1$] where F1<>'' and F1<>null ";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds2 = new DataSet();
myCommand.Fill(ds2, "table1");
}
catch (Exception ex)
{
conn.Close();
}
finally
{
conn.Close();
}
return ds2;
}

/// <summary>
/// 导入数据
/// </summary>
/// <param name="sender"></param>


/// <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]";这种格式
[其他解释]

引用:
string strCom = " SELECT * FROM [Shert1$A33:H44]";这种格式
试试
[其他解释]
string strCom = " SELECT * FROM [Shert1$A33:H44]";这种格
[其他解释]
引用:
string strCom = " SELECT * FROM [Shert1$A33:H44]";这种格式

拜膜
[其他解释]
关键是怎样加查询条件
[其他解释]
*.xls";
ofd.ValidateNames = true; //文件有效性验证ValidateNames,验证用户输入是否是一个有效的Windows文件名
ofd.CheckFileExists = true; //验证路径有效性
ofd.CheckPathExists = true; //验证文件有效性

string strName = string.Empty;
if (ofd.ShowDialog() == DialogResult.OK)
{
strName = ofd.FileName;
}
if (strName == "")
{
MessageBox.Show("没有选择Excel文件!无法进行数据导入");
return;
}

//调用导入数据方法


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呢
[其他解释]

引用:
每天回帖即可获得10分可用分

哈哈,有着想法的人很多,我就是其中一个
[其他解释]
引用:
string strCom = " SELECT * FROM [Shert1$A33:H44]";这种格式


试过, 好像不行呀.

------------------
至少一个参数没有被指定值。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Data.OleDb.OleDbException: 至少一个参数没有被指定值。

源错误:


行 23: OleDbDataAdapter da = new OleDbDataAdapter(sql, cn);
行 24: DataSet ds = new DataSet();
行 25: da.Fill(ds);
行 26: return ds;
行 27: }

读书人网 >asp.net

热点推荐