zoukankan      html  css  js  c++  java
  • .net excel利用NPOI导入oracle

    1.链接数据库

      引用System.Data.OracleClient;

      //数据库链接字符串   Data Source如:192.168.5.153:1521/orcl

      string linkStr = "User ID=" + name + "; Password=" + password + "; Data Source=" + oraLink;

      OracleConnection oraCon = new OracleConnection(linkStr);

      oraCon.Open();

    2.利用NPOI读取excel数据  

    public class ExcelHelper:IDisposable
        {
            private string fileName = null; //文件名
            private IWorkbook workbook = null;
            private FileStream fs = null;
            private bool disposed;
    
            public ExcelHelper(string fileName)
            {
                this.fileName = fileName;
                disposed = false;
            }
            /// <summary>
            /// 将Excel导入数据库
            /// </summary>
            /// <param name="sheetName"></param>
            /// <param name="isFirstRowColumn"></param>
            /// <returns></returns>
            public DataTable ExcelToDataTable(string sheetName, bool isFirstRowColumn)
            {
                ISheet sheet = null;
                DataTable data = new DataTable();
                int startRow = 0;
                try
                {
                    fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                    if (fileName.IndexOf(".xlsx") > 0) // 2007版本
                        workbook = new HSSFWorkbook(fs);
                    else if (fileName.IndexOf(".xls") > 0) // 2003版本
                        workbook = new HSSFWorkbook(fs);
    
                    if (sheetName != null)
                    {
                        sheet = workbook.GetSheet(sheetName);
                        if (sheet == null) //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
                        {
                            sheet = workbook.GetSheetAt(0);
                        }
                    }
                    else
                    {
                        sheet = workbook.GetSheetAt(0);
                    }
                    if (sheet != null)
                    {
                        IRow firstRow = sheet.GetRow(0);
                        int cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数
    
                        if (isFirstRowColumn)
                        {
                            for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                            {
                                ICell cell = firstRow.GetCell(i);
                                if (cell != null)
                                {
                                    string cellValue = cell.StringCellValue;
                                    if (cellValue != null)
                                    {
                                        DataColumn column = new DataColumn(cellValue);
                                        data.Columns.Add(column);
                                    }
                                }
                            }
                            startRow = sheet.FirstRowNum + 1;
                        }
                        else
                        {
                            startRow = sheet.FirstRowNum;
                        }
    
                        //最后一列的标号
                        int rowCount = sheet.LastRowNum;
                        for (int i = startRow; i <= rowCount; ++i)
                        {
                            IRow row = sheet.GetRow(i);
                            if (row == null) continue; //没有数据的行默认是null       
    
                            DataRow dataRow = data.NewRow();
                            for (int j = row.FirstCellNum; j < cellCount; ++j)
                            {
                                if (row.GetCell(j) != null) //同理,没有数据的单元格都默认是null
                                    dataRow[j] = row.GetCell(j).ToString();
                            }
                            data.Rows.Add(dataRow);
                        }
                    }
    
                    return data;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception: " + ex.Message);
                    return null;
                }
            }
    
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
    
            protected virtual void Dispose(bool disposing)
            {
                if (!this.disposed)
                {
                    if (disposing)
                    {
                        if (fs != null)
                            fs.Close();
                    }
    
                    fs = null;
                    disposed = true;
                }
            }
    excelHelper类

    3.插入数据库

     --利用openFileDialog读取文件名

     --代码如下:

      openFileDialog1.Filter = "(*.xls)|*.xls";
      openFileDialog1.ShowDialog();
      this.textBox1.Text = openFileDialog1.FileName;

     --将数据插入oracle数据库

      using(ExcelHelper excel = new ExcelHelper(this.textBox1.Text))

     {

          DataTable dbdata = excel.ExcelToDataTable(null, true); //调用excelHelper中的函数

          for (int i = 0; i < dbdata.Rows.Count; i++)  //解析数据   

         {

             string XMMC = dbdata.Rows[i][0].ToString();

             string sql ="insert into XXX(xmmc) values ('"+XMMC +"')";       

             OracleCommand cmd = new OracleCommand(sql, oraCon);
             OracleDataReader reader = cmd.ExecuteNonQuery();//增删改

             /*  判断数据是否存在

             OracleCommand cmd = new OracleCommand(sql1, oraCon);
             OracleDataReader reader = cmd.ExecuteReader();

             if(reader.Read()){}

           */

         }

     }

  • 相关阅读:
    Mac OS X配置环境变量
    react navite 学习资料
    协议是人造的交互(通信)规则
    语言的本质是更好的对客观世界作出抽象和描述
    编程语言评价标准:冯诺伊曼体系
    afnetwork moya 都符合通信协议七层模型
    Async/await promise实现
    协程 和 async await
    phpStorm字体大小无法调整, 怎么办?
    Composer常见问题
  • 原文地址:https://www.cnblogs.com/bobo-show/p/4686666.html
Copyright © 2011-2022 走看看