日期:2014-05-20  浏览次数:20516 次

急急急啊,客户上传一个.xls,如何读出该表格的行数。
感谢高手的解答啊,很急的啊。asp.net(C#)。

------解决方案--------------------
//****导入数据处理
//****1、获取上传的文件中径
string mFilePath = "UploadFile/YJBDMB/InputExcel/ " + hidInputFilePath.Value;

string mUploadPath = Server.MapPath( "~/ " + mFilePath);

//****2、验证文件是否存在
if (File.Exists(mUploadPath) == false)
{
return;
}

UFIDAFramework.CommonLibrary.Reports.ExcelAccess.ExcelAccess mExcelAccess = new UFIDAFramework.CommonLibrary.Reports.ExcelAccess.ExcelAccess();

//****3、打开Excel文件
mExcelAccess.Open(mUploadPath);

//****4、读取Excel单元格数据
Excel.Range strCell = mExcelAccess.GetRange(1, "HN ");

Excel.Range strDJYSMBBH = mExcelAccess.GetRange(1, "HO ");

//****5、获取行数
string strRange = (string)strCell.Text;

//****6、获取单机要素编号
string mDJYSMBBH = (string)strDJYSMBBH.Text;

if (strRange == " " && mDJYSMBBH == " ")
{
Page.ClientScript.RegisterStartupScript(typeof(string), "msg ", " <script> alert( '导入表单数据模版格式不正确,请重新选择! '); </script> ");
return;
}

//****7、重第3行开始读取数据
int intRow = 3;

do
{
string strYJBDSJ = " ";

//****11、拆分要素编号
string[] mFYJYSBH = mDJYSMBBH.Split( ', ');


//****8、循环获取行数据处理
for (int i = 1; i <= int.Parse(strRange); i++)
{
if (strYJBDSJ == " ")
{
strYJBDSJ = mExcelAccess.GetCellText(intRow, i) + ", " + mFYJYSBH[i - 1].ToString();
}
else
{
strYJBDSJ += "\t " + mExcelAccess.GetCellText(intRow, i) + ", " + mFYJYSBH[i - 1].ToString();
}
}

if (strYJBDSJ != " ")
{
//****调用保存数据接口
InitSaveBDYSSJ(strYJBDSJ);
}

intRow++;
}

while (mExcelAccess.GetCellText(intRow, 1).ToString() != " ");

//***9、关闭报表
mExcelAccess.Close();

//****10、导入成功以后删除文件
if (File.Exists(mUploadPath) == true)
{
File.Delete(mUploadPath);
}

//****11、提示导入数据成功
Page.ClientScript.RegisterStartupScript(typeof(string), "msg ", " <script> alert( '导入数据成功! '); </script> ");

//****12、加载显示导入的数据
Init_YJBDList();