zoukankan      html  css  js  c++  java
  • 利用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)
                    {
    
                    }
                }
            }
        }
    
    
    }
  • 相关阅读:
    hdu5360 Hiking(水题)
    hdu5348 MZL's endless loop(欧拉回路)
    hdu5351 MZL's Border(规律题,java)
    hdu5347 MZL's chemistry(打表)
    hdu5344 MZL's xor(水题)
    hdu5338 ZZX and Permutations(贪心、线段树)
    hdu 5325 Crazy Bobo (树形dp)
    hdu5323 Solve this interesting problem(爆搜)
    hdu5322 Hope(dp)
    Lightoj1009 Back to Underworld(带权并查集)
  • 原文地址:https://www.cnblogs.com/Vinkong/p/12618492.html
Copyright © 2011-2022 走看看