Browse Source

修复excel不能导入的bug

李士越 1 year ago
parent
commit
4dbc108b2c

BIN
wwwroot/Bin/ICSharpCode.SharpZipLib.dll


BIN
wwwroot/Bin/NPOI.OOXML.dll


BIN
wwwroot/Bin/NPOI.OpenXml4Net.dll


BIN
wwwroot/Bin/NPOI.OpenXmlFormats.dll


BIN
wwwroot/Bin/NPOI.dll


+ 74 - 4
wwwroot/mes/pm/fqcitems/fqcitemsdetail.ashx

@@ -10,6 +10,10 @@ using Curtain.DataAccess;
 using DK.XuWei.WebMes;
 using Newtonsoft.Json;
 using System.Collections;
+using System.IO;
+using NPOI.HSSF.UserModel;
+using NPOI.SS.UserModel;
+using NPOI.XSSF.UserModel;
 
 /// <summary>
 /// TP_PM_FQCITEMSDETAIL
@@ -169,14 +173,15 @@ public class fqcitemsdetail : IHttpHandler, IReadOnlySessionState
                             string filePath = "/mes/upload/" + DateTime.Now.ToString("yyyy-MM-dd");
                             System.IO.Directory.CreateDirectory(context.Server.MapPath(filePath));
                             string fileName = filePath + "/检验项目明细" + DateTime.Now.ToString("yyyy-MM");
-                            fileName += System.IO.Path.GetExtension(context.Request.Files[0].FileName+"x");
+                            fileName += System.IO.Path.GetExtension(context.Request.Files[0].FileName + "x");
                             string diskFileName = context.Server.MapPath(fileName);
                             if (System.IO.File.Exists(diskFileName)) System.IO.File.Delete(diskFileName);
                             context.Request.Files[0].SaveAs(diskFileName);
                             //文件转Table
-                            DataTable detailTable = Import.ExcelToDataTable(diskFileName);
+                            var detailTable = ExcelToDatatable(diskFileName);
+                            //DataTable detailTable = Import.ExcelToDataTable(diskFileName);
                             int itemsID = Convert.ToInt32(context.Request["id"]);
-                            context.Response.Write(Imports(detailTable,itemsID));
+                            context.Response.Write(Imports(detailTable, itemsID));
                         }
                         else
                         {
@@ -1473,5 +1478,70 @@ public class fqcitemsdetail : IHttpHandler, IReadOnlySessionState
             return false;
         }
     }
-
+    #region 读取excel数据
+    /// <summary>
+    /// 读取excel数据
+    /// </summary>
+    /// <param name="fileName">地址</param>
+    /// 李士越 2024-07-01
+    /// <returns></returns>
+    public static DataTable ExcelToDatatable(string fileName)
+    {
+        ISheet sheet = null;
+        DataTable datatable = new DataTable();
+        int startRow = 0;
+        FileStream fs;
+        IWorkbook workbook = null;
+        int cellCount = 0;
+        int rowCount = 0;
+        try
+        {
+            fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
+            if (fileName.IndexOf(".xlsx") > 0)
+            {
+                workbook = new XSSFWorkbook(fs);
+            }
+            else if (fileName.IndexOf(".xls") > 0)
+            {
+                workbook = new HSSFWorkbook(fs);
+            }
+            sheet = workbook.GetSheetAt(0);
+            if (sheet != null)
+            {
+                IRow firstRow = sheet.GetRow(0);
+                cellCount = firstRow.LastCellNum;
+                for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
+                {
+                    DataColumn column = new DataColumn(firstRow.GetCell(i).StringCellValue);
+                    datatable.Columns.Add(column);
+                }
+                startRow = sheet.FirstRowNum + 1;
+                rowCount = sheet.LastRowNum;
+                for (int i = startRow; i <= rowCount; ++i)
+                {
+                    IRow row = sheet.GetRow(i);
+                    if (row == null)
+                    {
+                        continue;
+                    }
+                    DataRow dataRow = datatable.NewRow();
+                    for (int y = 0; y <= cellCount; y++)
+                    {
+                        if (row.GetCell(y) != null && row.GetCell(y).ToString() != String.Empty && row.GetCell(y).ToString() != "" && row.GetCell(y).ToString().Trim() != "")
+                        {
+                            dataRow[y] = row.GetCell(y).ToString();
+                        }
+                    }
+                    datatable.Rows.Add(dataRow);
+                }
+            }
+            return datatable;
+        }
+        catch (Exception ex)
+        {
+            Console.WriteLine("Exception: " + ex.Message);
+            return null;
+        }
+    }
+    #endregion
 }

+ 5 - 2
wwwroot/mes/rpt2/rpt8009/rpt.ashx

@@ -71,11 +71,14 @@ public class rpt : IHttpHandler, IReadOnlySessionState
                 string testmouldflag = " ";
                 if (testType == "0")
                 {
-                    testmouldflag = " AND GDD.TESTMOULDFLAG = '0' ";
+                    //testmouldflag = " AND GDD.TESTMOULDFLAG = '0' ";
+                    testmouldflag = @" AND ((0 = 0 AND GDD.TESTMOULDFLAG  = 0 AND (GDD.TESTFORMFLAG IN(0,1) OR GDD.TESTFORMFLAG IS NULL ) )  ";
                 }
                 else if (testType == "1")
                 {
-                    testmouldflag = " AND GDD.TESTMOULDFLAG = '1' ";
+                    //testmouldflag = " AND GDD.TESTMOULDFLAG = '1' ";
+                    testmouldflag = " AND (1 = 1 AND (GDD.TESTFORMFLAG = 2 OR GDD.TESTMOULDFLAG  = 1))   ";
+                        
                 }
 
                 string testflagmaster = " ";