public static ReturnValue ReadExcelToDataSet(string xlsFullFileName, bool isHDR, bool isIMEX, int limitSheetCount, bool isOnlyVerify)
{
ReturnValue returnValue = new ReturnValue();
string fileExt = UploadFileUtils.GetFileExt(xlsFullFileName);
if (string.IsNullOrEmpty(fileExt) || !StringUtils.IsInLimitStr("xls,xlsx", fileExt))
{
returnValue.HasError = true;
returnValue.ReturnCode = 1;
returnValue.Message = "无效excel文件后缀";
return returnValue;
}
if (!File.Exists(xlsFullFileName))
{
returnValue.HasError = true;
returnValue.ReturnCode = 2;
returnValue.Message = "文件不存在";
return returnValue;
}
StringBuilder stringBuilder = new StringBuilder();
string str;
if ("xlsx".Equals(fileExt, StringComparison.CurrentCultureIgnoreCase))
{
stringBuilder.Append("Provider=Microsoft.ACE.OLEDB.12.0");
str = "Excel 12.0;";
}
else
{
stringBuilder.Append("Provider=Microsoft.Jet.OLEDB.4.0");
str = "Excel 8.0;";
}
stringBuilder.Append(";Data Source=" + xlsFullFileName);
stringBuilder.Append(";Extended Properties=\"" + str);
if (isHDR)
{
stringBuilder.Append(";HDR=Yes");
}
if (isIMEX)
{
stringBuilder.Append(";IMEX=1");
}
stringBuilder.Append("\"");
ExcelUtils.log.Debug(stringBuilder.ToString());
OleDbConnection oleDbConnection = new OleDbConnection(stringBuilder.ToString());
try
{
oleDbConnection.Open();
DataTable oleDbSchemaTable = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[]
{
null,
null,
null,
"Table"
});
if (oleDbSchemaTable == null || oleDbSchemaTable.Rows.Count == 0)
{
returnValue.HasError = true;
returnValue.ReturnCode = 3;
returnValue.Message = "读取不到sheet的信息";
ReturnValue result = returnValue;
return result;
}
if (isOnlyVerify)
{
returnValue.HasError = false;
returnValue.Message = "读取sheet信息正确";
returnValue.PutValue("dtSheet", oleDbSchemaTable);
ReturnValue result = returnValue;
return result;
}
int num = oleDbSchemaTable.Rows.Count;
if (limitSheetCount > 0 && limitSheetCount < oleDbSchemaTable.Rows.Count)
{
num = limitSheetCount;
}
string[] array = new string[num];
for (int i = 0; i < num; i++)
{
array[i] = oleDbSchemaTable.Rows[i]["TABLE_NAME"].ToString();
}
DataSet dataSet = new DataSet();
for (int j = 0; j < num; j++)
{
string text = "select * from [" + array[j] + "]";
ExcelUtils.log.Debug(text);
OleDbCommand selectCommand = new OleDbCommand(text, oleDbConnection);
OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(selectCommand);
DataTable dataTable = new DataTable(array[j]);
oleDbDataAdapter.Fill(dataTable);
dataSet.Tables.Add(dataTable);
}
returnValue.HasError = false;
returnValue.PutValue("dtSheet", oleDbSchemaTable);
returnValue.PutValue("arrTableName", array);
returnValue.ReturnObject = dataSet;
returnValue.Message = "读取成功";
}
catch (Exception ex)
{
returnValue.HasError = true;
returnValue.ReturnCode = -100;
returnValue.ReturnException = ex;
returnValue.Message = ex.Message;
ExcelUtils.log.WarnFormat("ReadExcelToDataSet sbConn={0} 出错,原因:{1}", stringBuilder.ToString(), ex.Message);
}
finally
{
oleDbConnection.Close();
}
return returnValue;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有