NPOI把Excel导入到数据库

二,把Excel中的数据导入到数据库的具体步骤:


        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                //文件流
                using (Stream stream = new FileStream(@"G:\userInfo.xls", FileMode.Open, FileAccess.Read))
                {
                    HSSFWorkbook workbook = new HSSFWorkbook(stream);
                    HSSFSheet sheet = workbook.GetSheetAt(0);

                    //Execel第一行是标题,不是要导入数据库的数据
                    for (int i = 1; i <= sheet.LastRowNum; i++)
                    {
                        HSSFRow row = sheet.GetRow(i);
                        UserInfo userinfo = new UserInfo();
                        userinfo.UserName = row.GetCell(0).StringCellValue;
                        //判断Excel中的Age的类型,根据不同的类型来用不同的方式取值
                        if (row.GetCell(1).CellType == HSSFCell.CELL_TYPE_NUMERIC)
                        {
                            userinfo.Age = row.GetCell(1).NumericCellValue;
                        }
                        else
                        {
                            userinfo.Age =Convert.ToInt32(row.GetCell(1).StringCellValue);
                        }
                        userinfo.Email = row.GetCell(2).StringCellValue;
                        //电话号码同样如此
                        if (row.GetCell(3).CellType == HSSFCell.CELL_TYPE_NUMERIC)
                        {
                            userinfo.Telephone = row.GetCell(3).NumericCellValue.ToString();
                        }
                        else
                        {
                            userinfo.Telephone = row.GetCell(3).StringCellValue;
                        }
                        userinfo.AddDate = row.GetCell(4).DateCellValue;
                        userinfo.Address = row.GetCell(5).StringCellValue;
                        //注意:Excel中可空的地方,Remark可以不填,因此我们需要判断。
                        if (row.GetCell(6)==null)
                        {
                            userinfo.Remarks = "";
                        }
                        else
                        {
                            userinfo.Remarks = row.GetCell(6).StringCellValue;
                        }
                        new UserInfoBLL().AddNew(userinfo);

                    }
                }
                 Response.Write("导入数据成功");
            }
            catch (Exception ex)
            {
                Response.Write("错误:" + ex.Message);
            }
        }

 另一个方法:

Aspose

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Excel文件中的数据导入到Oracle数据库可以使用以下步骤: 1. 首先,需要将Excel文件读取到C#中,可以使用NPOI库来读取Excel文件中的数据。 2. 连接Oracle数据库,并打开连接。 3. 创建一个OracleCommand对象,该对象用于执行SQL命令。 4. 遍历Excel文件中的每一行数据,并将数据插入到Oracle数据库中,可以使用OracleCommand对象的ExecuteNonQuery方法来执行SQL语句。 以下是一个示例代码: ```csharp using System; using System.Data; using System.Data.OracleClient; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.SS.Util; // 读取Excel文件 HSSFWorkbook workbook = new HSSFWorkbook(new FileStream("Excel文件路径", FileMode.Open)); ISheet sheet = workbook.GetSheetAt(0); // 连接Oracle数据库 string connectionString = "Data Source=数据库地址;User ID=用户名;Password=密码;"; OracleConnection connection = new OracleConnection(connectionString); connection.Open(); // 创建OracleCommand对象 OracleCommand command = new OracleCommand(); command.Connection = connection; // 遍历Excel文件中的每一行数据,将数据插入到Oracle数据库中 for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++) { IRow row = sheet.GetRow(i); if (row == null) continue; string col1 = row.GetCell(0).ToString().Trim(); string col2 = row.GetCell(1).ToString().Trim(); string col3 = row.GetCell(2).ToString().Trim(); string sql = "insert into table_name(col1, col2, col3) values(:col1, :col2, :col3)"; command.CommandText = sql; command.Parameters.Clear(); command.Parameters.Add(new OracleParameter(":col1", col1)); command.Parameters.Add(new OracleParameter(":col2", col2)); command.Parameters.Add(new OracleParameter(":col3", col3)); command.ExecuteNonQuery(); } // 关闭连接 connection.Close(); ``` 需要注意的是,在执行SQL语句时,使用了参数化查询,可以防止SQL注入攻击。另外,需要根据Excel文件和Oracle数据库的实际情况,修改代码中的表名、列名和连接字符串等信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值