Excel 导入至 SQL Server

1.新建项目在这里插入图片描述
2.拷贝 Aspose.Cell 文件到项目文件夹并引用在这里插入图片描述
3.添加 AsposeExcelHelper 插件类

#region 构造函数
        public AsposeExcelHelper()
        {
            book = new Workbook();
            SetSheets(0);
        }
        #endregion

        #region Excel属性
        public Worksheet sheet { get; set; }
        public Workbook book { get; set; }
        #endregion

        #region Excel操作方法
        /// <summary>
        /// 打开Excel文件,默认第一个Sheet
        /// </summary>
        /// <param name="strPath"></param>
        public void OpenExcel(string strPath)
        {
            if (string.IsNullOrEmpty(strPath))
            {
                book = new Workbook();
                SetSheets(0);
            }
            else
            {
                book.Open(strPath);
                SetSheets(0);
            }
        }

        /// <summary>
        /// 打开Excel文件,选择打开某一个Sheet
        /// </summary>
        /// <param name="strPath"></param>
        /// <param name="sheetIndex"></param>
        /// <returns></returns>
        public bool OpenExcel(string strPath, int sheetIndex)
        {
            try
            {
                if (string.IsNullOrEmpty(strPath))
                {
                    book = new Workbook();
                    SetSheets(sheetIndex);
                }
                else
                {
                    book.Open(strPath);
                    SetSheets(sheetIndex);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            //return wbk;
        }

        public void CopySheet(int A)
        {
            book.Worksheets.AddCopy(A);
        }

        /// <summary>
        /// 添加一个行
        /// </summary>
        /// <param name="rowIndex"></param>
        public void InsertRow(int rowIndex)
        {
            sheet.Cells.InsertRow(rowIndex);
        }

        /// <summary>
        /// 添加一个行
        /// </summary>
        /// <param name="rowIndex"></param>
        public void InsertColumn(int colIndex)
        {
            sheet.Cells.InsertColumn(colIndex);
        }

        /// <summary>
        /// 设置Excel操作的Sheet页
        /// </summary>
        /// <param name="sheetIndex"></param>
        public void SetSheets(int sheetIndex)
        {
            sheet = book.Worksheets[sheetIndex];
        }

        /// <summary>
        /// 获取Sheet页面使用的列总数
        /// </summary>
        /// <returns></returns>
        public int GetUsedRangeColumnsCount()
        {
            return sheet.Cells.MaxColumn;
        }

        /// <summary>
        /// 获取Sheet页面使用的行总数
        /// </summary>
        /// <returns></returns>
        public int GetUsedRangeRowCount()
        {
            return sheet.Cells.MaxDataRow;
        }

        /// <summary>
        /// 根据行号,和文本获取列索引。
        /// </summary>
        /// <param name="rowIndex"></param>
        /// <param name="strText"></param>
        /// <returns></returns>
        public int RowGetColumnIndex(int rowIndex, string strText)
        {
            int result = 0;
            int colCount = GetUsedRangeColumnsCount();
            for (int i = 0; i < colCount; i++)
            {
                string strTemp = GetCellsText(rowIndex, i);
                if (strTemp == strText)
                {
                    result = i;
                    break;
                }
            }
            return result;
        }

        /// <summary>
        /// 获取单元格的值
        /// </summary>
        /// <param name="rowA"></param>
        /// <param name="colA"></param>
        /// <returns></returns>
        public string GetCellsText(int rowA, int colA)
        {
            Cell cell = sheet.Cells[rowA, colA];
            string strText = "";
            if (cell.StringValue != null)
            {
                strText = cell.StringValue;
            }
            return strText;
        }


        /// <summary>
        /// 设置单元格的值
        /// </summary>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <param name="strText"></param>
        public void SetCellText(int row, int col, string strText)
        {
            if (string.IsNullOrEmpty(strText))
            {
                return;
            }
            double number = CheckISNumber(strText);
            if (number == -0.99999)
            {
                sheet.Cells[row, col].PutValue(strText);
            }
            else
            {
                sheet.Cells[row, col].PutValue(number);
            }
        }

        /// <summary>
        /// 设置单元格的值
        /// </summary>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <param name="strText"></param>
        public void SetCellText(int row, int col, Color color, string strText)
        {
            if (string.IsNullOrEmpty(strText))
            {
                return;
            }
            double number = CheckISNumber(strText);
            if (number == -0.99999)
            {
                sheet.Cells[row, col].PutValue(strText);
            }
            else
            {
                sheet.Cells[row, col].PutValue(number);
            }
            SetCellColor(row, col, color);
        }

        /// <summary>
        /// 设置单元格值
        /// </summary>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <param name="strText"></param>
        public void SetCellString(int row, int col, object strText)
        {
            string strTemp = string.Empty;
            try
            {
                strTemp = strText.ToString();
                if (string.IsNullOrEmpty(strTemp))
                {
                    return;
                }
            }
            catch (Exception)
            {
                return;
            }
            sheet.Cells[row, col].PutValue(strTemp);
        }

        public void SetCellValue(int row, int col, string strText)
        {
            try
            {
                if (string.IsNullOrEmpty(strText))
                {
                    return;
                }
            }
            catch (Exception)
            {
                return;
            }
            sheet.Cells[row, col].PutValue(strText);
        }

        public void SetCellObject(int row, int col, Object obj)
        {
            try
            {
                if (obj != null)
                {
                    return;
                }
                sheet.Cells[row, col].PutValue(obj);
            }
            catch (Exception)
            {
                return;
            }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值