From 78ccddadb87e0d4dd7f74733a031393395db2869 Mon Sep 17 00:00:00 2001
From: yl <ykxkd@outlook.com>
Date: 星期三, 29 五月 2024 14:38:39 +0800
Subject: [PATCH] nothing

---
 VueWebApi/Tools/ImportExcel.cs |  372 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 368 insertions(+), 4 deletions(-)

diff --git a/VueWebApi/Tools/ImportExcel.cs b/VueWebApi/Tools/ImportExcel.cs
index 699a575..e8dc1c2 100644
--- a/VueWebApi/Tools/ImportExcel.cs
+++ b/VueWebApi/Tools/ImportExcel.cs
@@ -53,6 +53,10 @@
             ISheet sheet = Workbook.GetSheetAt(0);
             //绗竴琛屼负鏍囬琛�
             IRow headerRow = sheet.GetRow(0);
+            if (headerRow == null)
+            {
+                return table;
+            }
             int cellCount = headerRow.LastCellNum;
             int rowCount = sheet.LastRowNum;
 
@@ -68,7 +72,7 @@
             {
                 IRow row = sheet.GetRow(i);
                 DataRow dataRow = table.NewRow();
-                if (row != null)
+                if (row != null&&row.Cells.Count>0)
                 {
                     for (int j = row.FirstCellNum; j < cellCount; j++)
                     {
@@ -146,6 +150,10 @@
                 ISheet sheet = Workbook.GetSheetAt(0);
                 //绗竴琛屼负鏍囬琛�
                 IRow headerRow = sheet.GetRow(0);
+                if (headerRow == null) 
+                {
+                   return list;
+                }
                 int cellCount = headerRow.LastCellNum;
                 int rowCount = sheet.LastRowNum;
 
@@ -161,7 +169,7 @@
                 {
                     IRow row = sheet.GetRow(i);
                     DataRow dataRow = table.NewRow();
-                    if (row != null)
+                    if (row != null&&row.Cells.Count>0)
                     {
                         for (int j = row.FirstCellNum; j < cellCount; j++)
                         {
@@ -198,13 +206,17 @@
             }
             else
             {
-                for (int m = 0; m < 2; m++)
+                for (int m = 0; m < count; m++)
                 {
                     DataTable table = new DataTable();
                     //瀹氫綅鍦ㄧ涓�涓猻heet
                     ISheet sheet = Workbook.GetSheetAt(m);
                     //绗竴琛屼负鏍囬琛�
                     IRow headerRow = sheet.GetRow(0);
+                    if (headerRow == null)
+                    {
+                        return list;
+                    }
                     int cellCount = headerRow.LastCellNum;
                     int rowCount = sheet.LastRowNum;
 
@@ -220,7 +232,7 @@
                     {
                         IRow row = sheet.GetRow(i);
                         DataRow dataRow = table.NewRow();
-                        if (row != null)
+                        if (row != null&&row.Cells.Count>0)
                         {
                             for (int j = row.FirstCellNum; j < cellCount; j++)
                             {
@@ -258,6 +270,170 @@
             }
             return list;
         }
+
+
+        /// <summary>
+        /// Excel 3涓猄heet瀵煎叆鎴怐atable
+        /// </summary>
+        /// <param name="file">瀵煎叆璺緞(鍖呭惈鏂囦欢鍚嶄笌鎵╁睍鍚�)</param>
+        /// <returns></returns>
+        public static List<DataTable> ExcelToThreeTableList(string file)
+        {
+            List<DataTable> list = new List<DataTable>();
+            IWorkbook Workbook;
+            try
+            {
+                using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
+                {
+                    //XSSFWorkbook 閫傜敤XLSX鏍煎紡锛孒SSFWorkbook 閫傜敤XLS鏍煎紡
+                    string fileExt = Path.GetExtension(file).ToLower();
+                    if (fileExt == ".xls")
+                    {
+                        Workbook = new HSSFWorkbook(fileStream);
+                    }
+                    else if (fileExt == ".xlsx")
+                    {
+                        Workbook = new XSSFWorkbook(fileStream);
+                    }
+                    else
+                    {
+                        Workbook = null;
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                throw ex;
+            }
+            int count = Workbook.NumberOfSheets; //鑾峰彇鎵�鏈塖heetName
+            if (count < 3)
+            {
+                DataTable table = new DataTable();
+                //瀹氫綅鍦ㄧ涓�涓猻heet
+                ISheet sheet = Workbook.GetSheetAt(0);
+                //绗竴琛屼负鏍囬琛�
+                IRow headerRow = sheet.GetRow(0);
+                if (headerRow == null)
+                {
+                    return list;
+                }
+                int cellCount = headerRow.LastCellNum;
+                int rowCount = sheet.LastRowNum;
+
+                //寰幆娣诲姞鏍囬鍒�
+                for (int i = headerRow.FirstCellNum; i < cellCount; i++)
+                {
+                    DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);
+                    table.Columns.Add(column);
+                }
+
+                //鏁版嵁
+                for (int i = (sheet.FirstRowNum + 1); i <= rowCount; i++)
+                {
+                    IRow row = sheet.GetRow(i);
+                    DataRow dataRow = table.NewRow();
+                    if (row != null && row.Cells.Count > 0)
+                    {
+                        for (int j = row.FirstCellNum; j < cellCount; j++)
+                        {
+                            if (row.GetCell(j) != null)
+                            {
+                                dataRow[j] = GetCellValue(row.GetCell(j)).Trim();
+                            }
+                        }
+                    }
+                    table.Rows.Add(dataRow);
+                }
+                //娓呴櫎鏈�鍚庣殑绌鸿
+                for (int i = 0; i < table.Rows.Count; i++)
+                {
+                    bool isnull = true;
+                    for (int j = 0; j < table.Columns.Count; j++)
+                    {
+                        if (table.Rows[i][j].ToString() != null && table.Rows[i][j].ToString() != "")
+                        {
+                            if (table.Rows[i][j].ToString().Trim() != "" && table.Rows[i][j].ToString().Trim() != null)
+                            {
+                                isnull = false;
+                                break;
+                            }
+                        }
+                    }
+                    if (isnull)
+                    {
+                        table.Rows[i].Delete();
+                        i = 0;
+                    }
+                }
+                list.Add(table);
+            }
+            else
+            {
+                for (int m = 0; m < count; m++)
+                {
+                    DataTable table = new DataTable();
+                    //瀹氫綅鍦ㄧ涓�涓猻heet
+                    ISheet sheet = Workbook.GetSheetAt(m);
+                    //绗竴琛屼负鏍囬琛�
+                    IRow headerRow = sheet.GetRow(0);
+                    if (headerRow == null)
+                    {
+                        return list;
+                    }
+                    int cellCount = headerRow.LastCellNum;
+                    int rowCount = sheet.LastRowNum;
+
+                    //寰幆娣诲姞鏍囬鍒�
+                    for (int i = headerRow.FirstCellNum; i < cellCount; i++)
+                    {
+                        DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);
+                        table.Columns.Add(column);
+                    }
+
+                    //鏁版嵁
+                    for (int i = (sheet.FirstRowNum + 1); i <= rowCount; i++)
+                    {
+                        IRow row = sheet.GetRow(i);
+                        DataRow dataRow = table.NewRow();
+                        if (row != null && row.Cells.Count > 0)
+                        {
+                            for (int j = row.FirstCellNum; j < cellCount; j++)
+                            {
+                                if (row.GetCell(j) != null)
+                                {
+                                    dataRow[j] = GetCellValue(row.GetCell(j)).Trim();
+                                }
+                            }
+                        }
+                        table.Rows.Add(dataRow);
+                    }
+                    //娓呴櫎鏈�鍚庣殑绌鸿
+                    for (int i = 0; i < table.Rows.Count; i++)
+                    {
+                        bool isnull = true;
+                        for (int j = 0; j < table.Columns.Count; j++)
+                        {
+                            if (table.Rows[i][j].ToString() != null && table.Rows[i][j].ToString() != "")
+                            {
+                                if (table.Rows[i][j].ToString().Trim() != "" && table.Rows[i][j].ToString().Trim() != null)
+                                {
+                                    isnull = false;
+                                    break;
+                                }
+                            }
+                        }
+                        if (isnull)
+                        {
+                            table.Rows[i].Delete();
+                            i = 0;
+                        }
+                    }
+                    list.Add(table);
+                }
+            }
+            return list;
+        }
+
 
         /// <summary>
         /// 鑾峰彇鍗曞厓鏍肩被鍨�
@@ -688,7 +864,195 @@
             return list;
         }
 
+        /// <summary>
+        /// Excel 3涓猄heet 鏁版嵁楠岃瘉
+        /// </summary>
+        /// <param name="file">瀵煎叆璺緞(鍖呭惈鏂囦欢鍚嶄笌鎵╁睍鍚�)</param>
+        /// <returns></returns>
+        public static List<ExcelErro> ExcelToThreeTableListErro(string file)
+        {
+            List<ExcelErro> list = new List<ExcelErro>();
+            IWorkbook Workbook;
+            bool iscell = false;
+            int iscellCount = 0;
+            try
+            {
+                using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
+                {
+                    //XSSFWorkbook 閫傜敤XLSX鏍煎紡锛孒SSFWorkbook 閫傜敤XLS鏍煎紡
+                    string fileExt = Path.GetExtension(file).ToLower();
+                    if (fileExt == ".xls")
+                    {
+                        Workbook = new HSSFWorkbook(fileStream);
+                    }
+                    else if (fileExt == ".xlsx")
+                    {
+                        Workbook = new XSSFWorkbook(fileStream);
+                    }
+                    else
+                    {
+                        Workbook = null;
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                throw ex;
+            }
+            for (int mm = 0; mm < 3; mm++)
+            {
+                DataTable table = new DataTable();
+                //瀹氫綅鍦ㄧ涓�涓猻heet
+                ISheet sheet = Workbook.GetSheetAt(mm);
+                //sheet.ShiftRows(sheet.FirstRowNum-1, sheet.LastRowNum,1);  
+                //绗竴琛屼负鏍囬琛�
+                IRow headerRow = sheet.GetRow(0);
+                int cellCount = headerRow.LastCellNum;
+                int rowCount0 = sheet.FirstRowNum;
+                //int rowCount = sheet.LastRowNum;
+                int rowCount = Rownum(headerRow, cellCount, sheet, sheet.LastRowNum); //鑾峰彇鍒犻櫎绌鸿涔嬪悗鐨勬暟鎹 (鍙拡瀵逛簬鏈�鍚庢暟鎹」鏈夌┖琛岀殑)
+                if (rowCount - rowCount0 >= 1)
+                {
+                    //-------------鍒ゆ柇蹇呭~椤规槸鍚︿负绌�----------------//
+                    //寰幆鏁版嵁琛�
+                    for (int i = (sheet.FirstRowNum + 1); i <= rowCount; i++)
+                    {
+                        iscell = false;
+                        iscellCount = 0;
+                        IRow row = sheet.GetRow(i);
+                        //鍒ゆ柇鏄惁鏈夌┖琛�
+                        if (row == null)
+                        {
+                            ExcelErro erro = new ExcelErro();
+                            erro.RoeNumber = (i + 1).ToString();
+                            erro.ErrorField = "绌鸿";
+                            erro.ErrorCont = "绌鸿";
+                            list.Add(erro);
+                            continue;
+                        }
+                        //鍒ゆ柇鏄惁瀛樺湪绌烘暟鎹
+                        for (int c = 0; c < cellCount; c++)
+                        {
+                            if (GetCellValue(row.GetCell(c)).Trim() != null && GetCellValue(row.GetCell(c)).Trim() != "")
+                            {
+                                iscell = false;
+                            }
+                            else
+                            {
+                                iscell = true;
+                                iscellCount++;
+                            }
+                        }
+                        if (iscell == true && iscellCount == cellCount)  //绌鸿
+                        {
+                            continue;
+                        }
+                        else
+                        {
+                            //寰幆鏍囬鍒�
+                            for (int j = headerRow.FirstCellNum; j < cellCount; j++)
+                            {
+                                //鑾峰彇鍗曞厓鏍肩殑濉厖鑹�
+                                string rgb = headerRow.Cells[j].CellStyle.FillForegroundColor.ToString();
+                                if (rgb == "51")  //蹇呭~椤瑰瓧娈靛垪澶村~鍏呰壊
+                                {
+                                    if (sheet.GetRow(i) == null || sheet.GetRow(i).ToString() == "")  //瀛樺湪绌鸿
+                                    {
+                                        ExcelErro erro = new ExcelErro();
+                                        erro.RoeNumber = i.ToString();
+                                        erro.ErrorField = "{绗�" + i.ToString() + "琛寎";
+                                        erro.ErrorCont = "{绗�" + i.ToString() + "}琛屼负绌�";
+                                        list.Add(erro);
+                                        break;
+                                    }
+                                    else
+                                    {
+                                        string cellValue = GetCellValue(sheet.GetRow(i).GetCell(j)).Trim();   //GetCell:杩斿洖鍖呮嫭绌哄垪鍊�  Cells:鍙繑鍥炴湁鍊肩殑鍒�
+                                        if (cellValue == "" || cellValue == null)
+                                        {
+                                            ExcelErro erro = new ExcelErro();
+                                            erro.RoeNumber = (sheet.GetRow(i).RowNum + 1).ToString();
+                                            erro.ErrorField = "{" + headerRow.Cells[j].StringCellValue + "}";
+                                            erro.ErrorCont = mm == 0 ? "涓昏〃涓細" + "{" + headerRow.Cells[j].StringCellValue + "}瀛楁涓嶈兘涓虹┖" : "瀛愯〃涓細" + "{" + headerRow.Cells[j].StringCellValue + "}瀛楁涓嶈兘涓虹┖";
+                                            list.Add(erro);
+                                        }
+                                    }
+                                }
 
+                            }
+                        }
+                    }
+                    //-------------鍒ゆ柇鍞竴椤规槸鍚﹂噸澶�----------------//
+                    //寰幆娣诲姞鏍囬鍒�
+                    for (int i = headerRow.FirstCellNum; i < cellCount; i++)
+                    {
+                        DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);
+                        table.Columns.Add(column);
+                    }
+
+                    //鏁版嵁
+                    for (int i = (sheet.FirstRowNum + 1); i <= rowCount; i++)
+                    {
+                        IRow row = sheet.GetRow(i);
+                        DataRow dataRow = table.NewRow();
+                        if (row != null)
+                        {
+                            for (int j = row.FirstCellNum; j < cellCount; j++)
+                            {
+                                if (row.GetCell(j) != null)
+                                {
+                                    dataRow[j] = GetCellValue(row.GetCell(j)).Trim();
+                                }
+                            }
+                        }
+                        table.Rows.Add(dataRow);
+                    }
+                    //娓呴櫎鏈�鍚庣殑绌鸿
+                    for (int i = 0; i < table.Rows.Count; i++)
+                    {
+                        bool isnull = true;
+                        for (int j = 0; j < table.Columns.Count; j++)
+                        {
+                            if (table.Rows[i][j].ToString() != null && table.Rows[i][j].ToString() != "")
+                            {
+                                if (table.Rows[i][j].ToString().Trim() != "" && table.Rows[i][j].ToString().Trim() != null)
+                                {
+                                    isnull = false;
+                                    break;
+                                }
+                            }
+                        }
+                        if (isnull)
+                        {
+                            table.Rows[i].Delete();
+                            i = 0;
+                        }
+                    }
+                    DataView view = new DataView(table);
+                    //鑾峰彇鎵�鏈夊敮涓�鍒�
+                    List<string> list0 = new List<string>();
+                    //寰幆鏍囬鍒�
+                    for (int j = headerRow.FirstCellNum; j < cellCount; j++)
+                    {
+                        bool isContain = headerRow.Cells[j].StringCellValue.Contains("鍞竴");  //鏍囬鍒椾腑鐨勫繀濉垪
+                        if (isContain == true)
+                        {
+                            //
+                            if (view.ToTable(true, new string[] { "" + headerRow.Cells[j].StringCellValue + "" }).Rows.Count < table.Rows.Count)
+                            {
+                                ExcelErro erro = new ExcelErro();
+                                erro.RoeNumber = "/";
+                                erro.ErrorField = "{" + headerRow.Cells[j].StringCellValue + "}";
+                                erro.ErrorCont = mm == 0 ? "涓昏〃涓細" + "{" + headerRow.Cells[j].StringCellValue + "}瀛楁鏈夐噸澶嶆暟鎹�" : "瀛愯〃涓細" + "{" + headerRow.Cells[j].StringCellValue + "}瀛楁鏈夐噸澶嶆暟鎹�";
+                                list.Add(erro);
+                            }
+                        }
+                    }
+                }
+            }
+            List<ExcelErro> stuList = list.OrderBy(s => s.RoeNumber).ToList<ExcelErro>();
+            return list;
+        }
 
 
 

--
Gitblit v1.9.3