• 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); }
    
    
    
     
  • 相关阅读:
    sql
    字符和字符串处理例子
    如何用火狐设置代理
    数组指针的一个小例子
    (转)数组指针和指针数组的区别
    函数
    (转)C语言指针5分钟教程
    通俗版解释网关,IP地址,ARP欺骗,DDOS攻击
    计算网络地址
    计算机网络性能指标
  • 原文地址:https://www.cnblogs.com/chunxiong/p/7614071.html
Copyright © 2020-2023  润新知