ASP.NET C# Excell导入导出

本文详述了在ASP.NET WebForm中实现Excel的导入导出功能,包括从数据库导出数据到Excel,以及从Excel导入数据到数据库的完整代码示例。示例展示了如何使用C#与Office Interop库进行操作,以及如何处理数据的读写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在写WebForm下的Excel的导入导出,在网上查了大量的资料,没有找到自己满意的,不过通过查找资料倒是学了不少东西;网上很多关于这方面的文章都是只说一部分,像技术小白看了就一知半解的,现在这篇文章呢,是我参考了网上的一些资料,再加上自己的倾心研究,写了个简单的但完整实现的Excel导入导出;

下面就直接上代码:

这是新建的一个vs2012空项目,文件列表如图,接下来分别贴出代码:iExcelToInportAndExport.aspx前段代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="iExcelToInportAndExport.aspx.cs" Inherits="WebExcelExportInport.iExcelToInportAndExport" %>

<!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>
            <table style="width: 858px">
                <tr>
                    <td style="width: 334px">
                        <asp:GridView ID="GridView1" runat="server"
                            AutoGenerateColumns="True" BackColor="White" BorderColor="#E7E7FF"
                            BorderStyle="None" BorderWidth="1px" CellPadding="3" Font-Names="Arial"
                            Font-Size="12px" GridLines="Horizontal" RowStyle-HorizontalAlign="Center"
                            Width="98%">
                            <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />

                            <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C"
                                HorizontalAlign="Center" />
                            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True"
                                ForeColor="#F7F7F7" />
                            <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C"
                                HorizontalAlign="Right" />
                            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True"
                                ForeColor="#F7F7F7" HorizontalAlign="Center" />
                            <AlternatingRowStyle BackColor="#F7F7F7" />
                        </asp:GridView>
                        <asp:Button ID="Button_Export" runat="server" Text="Excel导出" OnClick="Excel_Export_Button" /><br/></br>
                        <asp:FileUpload ID="FileUpload1" runat="server" /><asp:Button ID="Button_Inport" runat="server" Text="Excel导入" OnClick="Excel_Inport_Button" />
                    </td>
                </tr>
            </table>
        </div>

    </form>
</body>
</html>

 

//===========================================

iExcelToInportAndExport.aspx.cs的代码

 

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Excel;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.Data.OleDb;

namespace WebExcelExportInport
{
    public partial class iExcelToInportAndExport : System.Web.UI.Page
    {
        string strConn = SiteSetting.ConnectionString;
        string sqlSelectALL = SiteSetting.sqlSelALL;
        string exToDB = SiteSetting.ExToDB;
        string fileName = SiteSetting.FileName;
        string outPutPath = SiteSetting.OutPutPath;    
        protected void Page_Load(object sender, EventArgs e)
        {
            Bind();
        }
        //绑定数据,显示到web页面上
        public void Bind()
        {
            DataSet ds = new DataSet();
            using (SqlConnection conn = new SqlConnection())
            {
                SqlDataAdapter sda = new SqlDataAdapter(sqlSelectALL, strConn);
                sda.Fil

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值