• 利用NPOI读取Execl数据插入数据库


    首先你得准备NPOI的dll文件

    这里提供百度云链接:

    链接:https://pan.baidu.com/s/1EavAso5hztTbuexgb9XsvA
    提取码:jyk6

    前台代码

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="导入到数据库.aspx.cs" Inherits="导入到数据库" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:FileUpload ID="FileUpload1" runat="server" />
          <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </div>
          
        </form>
    </body>
    </html>

    直接上后台代码,一定要先添加dll引用

    using NPOI.HSSF.UserModel;
    using NPOI.XSSF.UserModel;
    using NPOI.SS.UserModel;
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class 导入到数据库 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            IWorkbook workbook =null;
          
            if (FileUpload1.HasFile)
            {
                string filePath = Server.MapPath("~/Files/");//先要创建项目文件夹Files
                string fileName = FileUpload1.PostedFile.FileName;
                FileUpload1.SaveAs(filePath + fileName);//存到项目文件夹下
    
                string fileExt = Path.GetExtension(fileName);
                using (FileStream fileReader = File.OpenRead(filePath + fileName))
                {
                    //创建工作簿对象接收文件流(Excel信息)
                    try
                    {
                        using (var file = new FileStream(filePath+fileName, FileMode.Open, FileAccess.Read))
                        {
                            if (fileExt == ".xls")
                            {
                                workbook = new HSSFWorkbook(file);
                            }
                            else if (fileExt == ".xlsx")
                            {
                                workbook = new XSSFWorkbook(file);
                            }
                          
                            ISheet sheet = workbook.GetSheetAt(0);
                            int ii = 1;
                            //r = 2,剔除表头2行
                            for (int r = 2; r <= sheet.LastRowNum; r++)
                            {
                                //定义参数数组para
    
                                //创建一行获取sheet行数据
                                IRow row = sheet.GetRow(r);
                                string a = row.GetCell(0).ToString();//excel中的第一个单元格数据
                                string b = row.GetCell(12).ToString();//excel中的第12个单元格数据
                             //{这里使用的你自己的orm框架进行入库操作}
                                ii++;
                            }
                            fileReader.Close();//关闭流
                            file.Close();//关闭流
                            //删除文件
                            System.IO.File.Delete(filePath + fileName);
                            if (ii == sheet.LastRowNum)
                            {
                                Response.Write("<script>alert('已将Excel数据插入到数据库表')</script>");
                            }
                            else
                            {
                                Response.Write("<script>alert('Excel数据插入到数据库表失败!')</script>");
                            }
                        }
    
                    }
                    catch (Exception d)
                    {
    
                    }
                }
            }
        }
    
    
    }
  • 相关阅读:
    Unity 游戏框架搭建 2019 (二十九) 方法所在类命名问题诞生的原因
    Unity 游戏框架搭建 2019 (二十七、二十八)弃用的代码警告解决&弃用的代码删除
    Unity 游戏框架搭建 2019 (二十六) 第一轮整理完结
    Unity 游戏框架搭建 2019 (二十五) 类的第一个作用 与 Obselete 属性
    排序算法之冒泡排序
    java中List Array相互转换
    java迭代器浅析
    谈谈java中遍历Map的几种方法
    浅谈java反射机制
    springMvc注解之@ResponseBody和@RequestBody
  • 原文地址:https://www.cnblogs.com/Vinkong/p/12618492.html
Copyright © 2020-2023  润新知