This commit is contained in:
高飞 2026-04-02 09:19:01 +08:00
parent 5ae8d3bae4
commit a7044c64ea
2 changed files with 140 additions and 47 deletions

View File

@ -502,5 +502,93 @@ namespace BLL
return value.ToString();
}
#endregion
public static DataTable ExcelToDataTable1(string filePath)
{
var dt = new DataTable();
using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
var hssfworkbook = new HSSFWorkbook(file);
var sheet = hssfworkbook.GetSheetAt(0);
var rows = sheet.GetRowEnumerator();
rows.MoveNext();
var row = (HSSFRow)rows.Current;
for (var j = 0; j < row.LastCellNum; j++)
{
var cell = row.GetCell(j);
if (cell != null)
{
dt.Columns.Add(cell.StringCellValue);
}
else
{
dt.Columns.Add("collum" + j);
}
}
while (rows.MoveNext())
{
row = (HSSFRow)rows.Current;
var dr = dt.NewRow();
for (var i = 0; i < row.LastCellNum; i++)
{
var cell = row.GetCell(i);
if (cell == null)
{
dr[i] = null;
}
else
{
try
{
switch (cell.CellType)
{
case CellType.Blank:
//dr[i] = "[null]";
break;
case CellType.Boolean:
dr[i] = cell.BooleanCellValue;
break;
case CellType.Numeric:
dr[i] = cell.ToString();
break;
case CellType.String:
dr[i] = cell.StringCellValue;
break;
case CellType.Error:
dr[i] = cell.ErrorCellValue;
break;
case CellType.Formula:
try
{
dr[i] = cell.NumericCellValue;
}
catch
{
try
{
dr[i] = cell.StringCellValue;
}
catch
{
dr[i] = null;
}
}
break;
default:
dr[i] = "=" + cell.CellFormula;
break;
}
}
catch (Exception ex)
{
}
}
}
dt.Rows.Add(dr);
}
}
return dt;
}
}
}

View File

@ -120,35 +120,38 @@ namespace FineUIPro.Web.HSSE.SitePerson
{
try
{
string oleDBConnString = String.Empty;
oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;";
oleDBConnString += "Data Source=";
oleDBConnString += fileName;
oleDBConnString += ";Extended Properties=Excel 8.0;";
OleDbConnection oleDBConn = null;
OleDbDataAdapter oleAdMaster = null;
DataTable m_tableName = new DataTable();
DataSet ds = new DataSet();
//string oleDBConnString = String.Empty;
//oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;";
//oleDBConnString += "Data Source=";
//oleDBConnString += fileName;
//oleDBConnString += ";Extended Properties=Excel 8.0;";
//OleDbConnection oleDBConn = null;
//OleDbDataAdapter oleAdMaster = null;
//DataTable m_tableName = new DataTable();
//DataSet ds = new DataSet();
oleDBConn = new OleDbConnection(oleDBConnString);
oleDBConn.Open();
m_tableName = oleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
//oleDBConn = new OleDbConnection(oleDBConnString);
//oleDBConn.Open();
//m_tableName = oleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (m_tableName != null && m_tableName.Rows.Count > 0)
{
//if (m_tableName != null && m_tableName.Rows.Count > 0)
//{
m_tableName.TableName = m_tableName.Rows[0]["TABLE_NAME"].ToString().Trim();
// m_tableName.TableName = m_tableName.Rows[0]["TABLE_NAME"].ToString().Trim();
}
string sqlMaster;
sqlMaster = " SELECT * FROM [" + m_tableName.TableName + "]";
oleAdMaster = new OleDbDataAdapter(sqlMaster, oleDBConn);
oleAdMaster.Fill(ds, "m_tableName");
oleAdMaster.Dispose();
oleDBConn.Close();
oleDBConn.Dispose();
//}
//string sqlMaster;
//sqlMaster = " SELECT * FROM [" + m_tableName.TableName + "]";
//oleAdMaster = new OleDbDataAdapter(sqlMaster, oleDBConn);
//oleAdMaster.Fill(ds, "m_tableName");
//oleAdMaster.Dispose();
//oleDBConn.Close();
//oleDBConn.Dispose();
AddDatasetToSQL(ds.Tables[0]);
//AddDatasetToSQL(ds.Tables[0]);
DataTable dt = NPOIHelper.ExcelToDataTable1(fileName);
this.AddDatasetToSQL(dt);
}
catch (Exception ex)
{
@ -304,33 +307,35 @@ namespace FineUIPro.Web.HSSE.SitePerson
{
try
{
string oleDBConnString = String.Empty;
oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;";
oleDBConnString += "Data Source=";
oleDBConnString += fileName;
oleDBConnString += ";Extended Properties=Excel 8.0;";
OleDbConnection oleDBConn = null;
OleDbDataAdapter oleAdMaster = null;
DataTable m_tableName = new DataTable();
DataSet ds = new DataSet();
//string oleDBConnString = String.Empty;
//oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;";
//oleDBConnString += "Data Source=";
//oleDBConnString += fileName;
//oleDBConnString += ";Extended Properties=Excel 8.0;";
//OleDbConnection oleDBConn = null;
//OleDbDataAdapter oleAdMaster = null;
//DataTable m_tableName = new DataTable();
//DataSet ds = new DataSet();
oleDBConn = new OleDbConnection(oleDBConnString);
oleDBConn.Open();
m_tableName = oleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
//oleDBConn = new OleDbConnection(oleDBConnString);
//oleDBConn.Open();
//m_tableName = oleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (m_tableName != null && m_tableName.Rows.Count > 0)
{
//if (m_tableName != null && m_tableName.Rows.Count > 0)
//{
m_tableName.TableName = m_tableName.Rows[0]["TABLE_NAME"].ToString().Trim();
// m_tableName.TableName = m_tableName.Rows[0]["TABLE_NAME"].ToString().Trim();
}
string sqlMaster = " SELECT * FROM [" + m_tableName.TableName + "]";
oleAdMaster = new OleDbDataAdapter(sqlMaster, oleDBConn);
oleAdMaster.Fill(ds, "m_tableName");
oleAdMaster.Dispose();
oleDBConn.Close();
oleDBConn.Dispose();
AddDatasetToSQL2(ds.Tables[0]);
//}
//string sqlMaster = " SELECT * FROM [" + m_tableName.TableName + "]";
//oleAdMaster = new OleDbDataAdapter(sqlMaster, oleDBConn);
//oleAdMaster.Fill(ds, "m_tableName");
//oleAdMaster.Dispose();
//oleDBConn.Close();
//oleDBConn.Dispose();
DataTable dt = NPOIHelper.ExcelToDataTable1(fileName);
this.AddDatasetToSQL2(dt);
}
catch (Exception ex)
{