zoukankan      html  css  js  c++  java
  • winfrom大数据导入excel

    最近由于项目的需要 需要从excel里面导入大量数据   网上找了很久  都没有找到一个好的方法  直到。。。。。。。。  不多说  直接上代码

    using System.Collections.Generic;
    using NPOI.HSSF.UserModel;
    using NPOI.SS.UserModel;
    using NPOI.XSSF.UserModel;
    using System.IO;
    using System.Data;
    using System;
         private void button10_Click(object sender, EventArgs e)
            {
                npoiWorkBook = null;
                OpenFileDialog file = new OpenFileDialog();
                if (file.ShowDialog() == DialogResult.OK)
                {
                    string filePath = file.FileName;
                    FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    StreamReader sr = new StreamReader(fs);
                    if (filePath.Contains(".xlsx"))
                        npoiWorkBook = new XSSFWorkbook(fs);
                    else
                        npoiWorkBook = new HSSFWorkbook(fs);
                }
                else { return; }
                ExcelHelper excel = new ExcelHelper(npoiWorkBook);
    //遍历第几个sheet(2就表示遍历sheet1 ,1就表示从第一行开始) DataTable dataTable
    = excel.ExportExcelToDataTable(2, 1); int rowCount = dataTable.Rows.Count;//总行数 Thread td = new Thread(new ThreadStart(delegate { foreach (DataRow row in dataTable.Rows) { using (NewContext context = new NewContext()) { try { string stationCode = row["站址编码"].ToString(); if (string.IsNullOrWhiteSpace(stationCode)) continue; var station = context.Station.FirstOrDefault(o => o.StationCode == stationCode); if (station == null) { station = new Station() { BuildType = GetBType(row["xxxx"].ToString()), City = "xxxx", County = row["所属区域"].ToString().Trim(), CreateTime = DateTime.Now, CrmOrderCode = "未知", MobileIsShare = row["xxxx"].ToString().Contains("xx"), UnicomIsShare = row["xxxx"].ToString().Contains("xx"), TelecomIsShare = row["xxxx"].ToString().Contains("xx"), PROperator = GetPR(row["xxxx"].ToString()), Remark = "", ShareType = row["xxxx"].ToString().Contains("x") ? 1 : 2, StationCode = row["xxxx"].ToString(), StationName = row["xxxx"].ToString(), }; context.Station.Add(station); } } catch (Exception) { throw; } context.SaveChanges(); } } })); td.Start(); }
           
    //查询遍历第几个sheet 返回DataTable

    public DataTable ExportExcelToDataTable(int sheetIndex, int satartNum) { return ExportToDataTable(_IWorkbook.GetSheetAt(sheetIndex - 1), satartNum); }
    
    
    
     
  • 相关阅读:
    关于2021-01-13爆发的incaseformat病毒相关信息收集1.2(持续更新和关注中...)
    (转)服务器UDIMM、LRDIMM、RDIMM三种内存的区别
    Shred命令 学习
    关于图片文件的元数据信息清理和时间戳修改
    关于Chrome 浏览器 “小尾巴”问题的解决办法
    CF471D MUH and Cube Walls -kmp
    UVA12467 Secret Word -kmp
    UVA12604 Caesar Cipher -kmp
    【JLOI2007】周期串
    Http协议
  • 原文地址:https://www.cnblogs.com/chunxiong/p/7614071.html
Copyright © 2011-2022 走看看