bklLiudl
2024-07-23 277bbae216debe7e6c04e8cc6ee6e1ba9763e14b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using Common;
using BLL;
using Model;
using Lib;
using System.Web.Mvc;
using wms;
using System.Data;
using System.Web;
using System;
using NPOI.SS.UserModel;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;
 
namespace WMS.Areas.BasicInfo.Controllers
{
    public class MatController : MasterPage
    {
        [LoginFilter]
        public ActionResult MatList()
        {
            ViewBag.Brand = LocalHelper.GetBrandItemsHtml();
            ViewBag.MatType = LocalHelper.GetDictionaryHtml("MatType");
            ViewBag.IsDel = LocalHelper.GetMatIsDelHtml();
            ViewBag.Title = "物料管理";
            return View();
        }
 
        [LoginFilter]
        public ActionResult AddMat()
        {
            string Guid = Request.QueryString.Get("MatNo");
            if (Guid.IsEmpty())
            {
                ViewBag.UnitList = LocalHelper.GetUnitList("");
                ViewBag.Brand = LocalHelper.GetBrandItemsHtml();
                ViewBag.MatType = LocalHelper.GetDictionaryHtml("MatType");
                ViewBag.Admin = new Material();
 
            }
            else
            {
                IDALMatNo provider = new DALMatNo();
                Material entity = provider.GetModel(Guid);
                entity = entity == null ? new Material() : entity;
                ViewBag.UnitList = LocalHelper.GetUnitList(entity.UnitNum);
                ViewBag.Brand = LocalHelper.GetBrandItemsHtml(entity.BrandId);
                ViewBag.MatType = LocalHelper.GetDictionaryHtml("MatType",entity.MatTypeId);
                ViewBag.Admin = entity;
 
            }
            return View();
        }
        public ActionResult Dialog()
        {
            return View();
        }
        /// <summary>
        /// 导入Excel
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ExcelToUpload(HttpPostedFileBase file)
        {
            DataTable excelTable = new DataTable();
            string msg = string.Empty;
             if (Request.Files.Count > 0)
            {
                try
                {
                    HttpPostedFileBase mypost = Request.Files[0];
                    string fileName = Request.Files[0].FileName;
                    //在文件夹Report 用来接收上传的文件
                    string serverpath = Server.MapPath(string.Format("~/{0}", "Report/") + fileName);
                    mypost.SaveAs(serverpath);
                    NPOIExcel nPOI = new NPOIExcel("", "", "");
                    excelTable = NPOIExcel.InputExcel(serverpath);
                    //excelTable = GetExcelDataTable(serverpath);
                    //删除服务器生成的文件
                    System.IO.File.Delete(serverpath);
                    //注意Excel表内容格式,第一行必须为列名与数据库列名匹配 
                    //接下来为各列名对应下来的内容
                    string CreateUser = this.LoginUserCode;
                   
                    msg = DALMatNo.ImportExcel(excelTable, CreateUser);// 写入基础数据
 
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                }
            }
            else
            {
                msg = "请选择文件";
            }
            return Json(msg);
        }
        public static DataTable GetExcelDataTable(string filePath)
        {
            IWorkbook Workbook;
            DataTable table = new DataTable();
            try
            {
                using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
 
                    //XSSFWorkbook 适用XLSX格式,HSSFWorkbook 适用XLS格式
                    string fileExt = System.IO.Path.GetExtension(filePath).ToLower();
 
                    if (fileExt == ".xls")
                    {
                        Workbook = new HSSFWorkbook(fileStream);
                    }
                    else if (fileExt == ".xlsx")
                    {
 
                        Workbook = new XSSFWorkbook(fileStream);
                    }
                    else
                    {
                        Workbook = null;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
 
            //定位在第一个sheet
            ISheet sheet = Workbook.GetSheetAt(0);
            //第一行为标题行
            IRow headerRow = sheet.GetRow(0);
            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)
                {
                    for (int j = row.FirstCellNum; j < cellCount; j++)
                    {
                        if (row.GetCell(j) != null)
                        {
                            dataRow[j] = GetCellValue(row.GetCell(j));
                        }
                    }
                }
                table.Rows.Add(dataRow);
            }
           
            return table;
        }
 
        private static string GetCellValue(ICell cell)
        {
            if (cell == null)
            {
                return string.Empty;
            }
 
            switch (cell.CellType)
            {
                case CellType.Blank:
                    return string.Empty;
                case CellType.Boolean:
                    return cell.BooleanCellValue.ToString();
                case CellType.Error:
                    return cell.ErrorCellValue.ToString();
                case CellType.Numeric:
                case CellType.Unknown:
                default:
                    return cell.ToString();
                case CellType.String:
                    return cell.StringCellValue;
                case CellType.Formula:
                    try
                    {
                        HSSFFormulaEvaluator e = new HSSFFormulaEvaluator(cell.Sheet.Workbook);
                        e.EvaluateInCell(cell);
                        return cell.ToString();
                    }
                    catch
                    {
                        return cell.NumericCellValue.ToString();
                    }
            }
        }
    }
}