예제 #1
0
        public ActionResult UploadOrder(IEnumerable <HttpPostedFileBase> UploadFile)
        {
            ViewBag.PageName = "批次上傳";

            // 儲存檔案
            foreach (var file in UploadFile)
            {
                if (file == null)
                {
                    ViewBag.Error = "請選擇檔案!";
                    return(View());
                }

                if (Path.GetExtension(file.FileName) != ".xlsx")
                {
                    ViewBag.Error = "副檔名錯誤,請下載樣板並上傳資料!";
                    return(View());
                }

                MemoryStream ms = new MemoryStream();
                file.InputStream.CopyTo(ms);

                string FilePath = @"D:\";
                file.InputStream.Position = 0;
                string FileName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + Path.GetExtension(file.FileName);
                file.SaveAs(FilePath + FileName);
                ms.Dispose();
                ms.Close();

                // NPOI讀取
                XSSFWorkbook wb;
                using (FileStream fs = new FileStream(FilePath + FileName, FileMode.Open, FileAccess.ReadWrite))
                {
                    wb = new XSSFWorkbook(fs);
                    XSSFSheet MySheet;
                    MySheet = (XSSFSheet)wb.GetSheetAt(0);
                    // 迴圈讀每筆資料,從1開始(跳過標題列)
                    for (int i = 1; i <= MySheet.LastRowNum; i++)
                    {
                        XSSFRow Row = (XSSFRow)MySheet.GetRow(i);

                        // 讀取每欄資料
                        for (int k = 0; i < Row.Cells.Count; i++)
                        {
                            string MyTemp = Row.GetCell(k).ToString();
                        }
                    }
                }
            }

            return(View());
        }