且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将Excel文件导入Gridvew?

更新时间:2022-10-17 23:15:37

您好,>

在GridView下方检查到Excel

http://www.codeproject.com/Tips/345914 /Direct-Excel-Export-from-ASP-Net-GridView

http://www.c-sharpcorner.com/UploadFile/DipalChoksi/exportxl_asp2_dc11032006003657AM/export .aspx

现在开始,请在适当的论坛中发布您的问题.您的问题是关于Asp.Net的.

所以将您的问题发布到

forums.asp.net

致谢

Alisha


Hi

Its possible import a excel to grid view??

i try this solution but in the moment where my aplication show's the grid , nothings hapens.

this is the aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="PMESP.CIAF.AP.WebForm1" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="fupArquivo" runat="server" Width="510px" />
        <asp:Button ID="btnLerExcel" runat="server" OnClick="btnLerExcel_Click" Text="Ler Excel" /><br />
        <div>
            <asp:GridView ID="gvExcel" runat="server" CellPadding="4" ForeColor="#333333" ViewStateMode="Enabled"
                Width="592px" AutoGenerateColumns="False" AllowPaging="True" BorderStyle="Solid"
                HorizontalAlign="Justify" PageSize="50" ShowHeaderWhenEmpty="True">
                <RowStyle BackColor="#E3EAEB" />
                <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                <Columns>
                    <asp:BoundField HeaderText="Nome" ReadOnly="True" />
                    <asp:BoundField HeaderText="R.E" ReadOnly="True" />
                </Columns>
                <EditRowStyle BackColor="#7C6F57" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
        </div>
    </div>
    </form>
</body>
</html>

my code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


namespace PMESP.CIAF.AP
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


        }


        protected void btnLerExcel_Click(object sender, EventArgs e)
        {
            if (fupArquivo.FileContent != null)
            {
                string Excel = AppDomain.CurrentDomain.BaseDirectory + fupArquivo.FileName;
                fupArquivo.SaveAs(Excel);
                DataTable Dados = DadosExcel(Excel);
                /*
                if (Dados != null)
                {
                    gvExcel.DataSource = Dados;
                    gvExcel.DataBind();
                }
                 * */
                foreach (DataRow DR in Dados.Rows)
                {
                    string PrimeiraColuna = DR[0].ToString();
                    string SegundaColuna = DR[1].ToString();
                }


            }
        }


        private DataTable DadosExcel(string Arquivo)
        {
            Char aspas = '"';
            string Conexao = "Provider=Microsoft.Jet.OLEDB.4.0;" +
            "Data Source=" + Arquivo + ";" +
            "Extended Properties=" + aspas + "Excel 8.0;HDR=YES" + aspas;
            System.Data.OleDb.OleDbConnection Cn = new System.Data.OleDb.OleDbConnection();
            Cn.ConnectionString = Conexao;
            Cn.Open();
            object[] Restricoes = { null, null, null, "TABLE" };
            DataTable DTSchema = Cn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, Restricoes);
            if (DTSchema.Rows.Count > 0)
            {
                string Sheet = DTSchema.Rows[0]["TABLE_NAME"].ToString();
                System.Data.OleDb.OleDbCommand Comando = new System.Data.OleDb.OleDbCommand("SELECT * FROM [" + Sheet + "]", Cn);
                DataTable Dados = new DataTable();
                System.Data.OleDb.OleDbDataAdapter DA = new System.Data.OleDb.OleDbDataAdapter(Comando);
                DA.Fill(Dados);
                Cn.Close();
                return Dados;
            }
            return null;
        }
    }
}

so none report errors is show up, but the page still blank....: (

someone help please.


Rafael Soares Desenvolvedor Web

Hi,

Check below GridView to Excel

http://www.codeproject.com/Tips/345914/Direct-Excel-Export-from-ASP-Net-GridView

http://www.c-sharpcorner.com/UploadFile/DipalChoksi/exportxl_asp2_dc11032006003657AM/exportxl_asp2_dc.aspx

now onwards Please post your question in proper forum. your question is regarding Asp.Net.

So Post your question in

forums.asp.net

Regards

Alisha