Aspose.Cells导入Excel

        /// <summary>
        /// 读取配管员绑定站点的Excel文件
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string ReadPiPingManStationExcel()
        {
            WebResults rs = new WebResults();
            try
            {
                var file = Request.Files["dataFile"];
                if (file.FileName.EndsWith(".xlsx") || file.FileName.EndsWith(".xls"))
                {
                    Workbook workbook = new Workbook(file.InputStream);
                    Cells cells = workbook.Worksheets[0].Cells;
                    int num=0;//读取到第几行
                    List<int> wrongRow = new List<int>();//更新失败的行数
                    List<int> stationIdList = new List<int>();
                    for (int i = 1; i <= cells.MaxDataRow; i++)//从第二行开始读取
                    {
                        try
                        {
                            num = i + 1;
                            int stationID = Convert.ToInt32(cells[i, 0].StringValue.Trim());
                            stationIdList.Add(stationID);
                        }
                        catch (Exception)
                        {
                            wrongRow.Add(num);
                            rs.Code = "00012";
                        }
                    }
                    if (wrongRow.Count > 0)
                    {
                        rs.Message = $"第{string.Join("", wrongRow)}行更新失败";
                    }
                    rs.Val = stationIdList;
                }
            }
            catch (Exception er)
            {
                rs.Code = "30013";
                rs.Message = "系统异常";
                rs.Val = null;
                WriteLog.CreateLog(er);
            }
            return JsonConvert.SerializeObject(rs);
        }

 

注意:

using Aspose.Cells;

posted @ 2017-08-11 14:31  花生打代码会头痛  阅读(151)  评论(0)    收藏  举报