From a7044c64ea21e15c96d756c6bc03563d15d6b12d Mon Sep 17 00:00:00 2001 From: gaofei1985 <181547018@qq.com> Date: Thu, 2 Apr 2026 09:19:01 +0800 Subject: [PATCH] 1 --- SGGL/BLL/Common/NPOIHelper.cs | 88 +++++++++++++++++ .../HSSE/SitePerson/PersonInfoIn.aspx.cs | 99 ++++++++++--------- 2 files changed, 140 insertions(+), 47 deletions(-) diff --git a/SGGL/BLL/Common/NPOIHelper.cs b/SGGL/BLL/Common/NPOIHelper.cs index bfe84611..a6814daf 100644 --- a/SGGL/BLL/Common/NPOIHelper.cs +++ b/SGGL/BLL/Common/NPOIHelper.cs @@ -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; + } } } diff --git a/SGGL/FineUIPro.Web/HSSE/SitePerson/PersonInfoIn.aspx.cs b/SGGL/FineUIPro.Web/HSSE/SitePerson/PersonInfoIn.aspx.cs index 194791a4..935d475a 100644 --- a/SGGL/FineUIPro.Web/HSSE/SitePerson/PersonInfoIn.aspx.cs +++ b/SGGL/FineUIPro.Web/HSSE/SitePerson/PersonInfoIn.aspx.cs @@ -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) {