当前位置:编程学习 > C#/ASP.NET >>

新手急救。。如何导入导出excel..给个示例吧。。。

.net导入excel到数据库。。。在前台页面如何导入。。。excel中需要注意什么?? --------------------编程问答-------------------- 网上很多,自己搜下, 实在找不到再找我。 --------------------编程问答-------------------- 能给个简单的例子吗。。。小弟新手。。。 --------------------编程问答-------------------- 就是要自己学会搜索。自己解决,  实在解决不找高手 --------------------编程问答-------------------- using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;

/// <summary>
/// ToExcel 的摘要说明
/// </summary>
public class ToExcel
{
public ToExcel()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

    /// <summary>
    /// 将DataTable 导出为EXCEL,并直接提供下载
    /// </summary>
    /// <param name="ds">需要导处的DataTable</param>
    /// <param name="fileName">到处生成的文件名</param>
    /// 
    public bool ExportExcelByDataTable(DataTable dt, string fileName)
    {
        try
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Charset = "utf-7";
            HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-7");

            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
DataGrid dg=new DataGrid();
dg.HeaderStyle.CssClass="dgHead";
dg.DataSource=dt;
dg.DataBind();
dg.RenderControl(htmlWrite);
//            GridView gv = new GridView();
//            gv.HeaderStyle.CssClass = "gvHead";
//            gv.DataSource = dt;
//            gv.DataBind();
//            gv.RenderControl(htmlWrite);
            HttpContext.Current.Response.Write(stringWrite.ToString());
            HttpContext.Current.Response.AddHeader("content-disposition","attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)+".xls");
            HttpContext.Current.Response.Charset = "gb2312";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            HttpContext.Current.Response.End();
            return true;
        }
        catch
        {
            return false;
        }
    }


    /// <summary>
    /// --重载方法,将GRIDVIEW 导出为EXCEL,并直接提供下载
    /// </summary>
    /// <param name="ds">需要导处的DataTable</param>
    /// <param name="fileName">到处生成的文件名</param>
    /// 
    public bool ExportExcelByDataGrid(DataGrid dg, string fileName)
    {
        try
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Charset = "gb2312";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");

            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            dg.RenderControl(htmlWrite);
            HttpContext.Current.Response.Write(stringWrite.ToString());
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
            HttpContext.Current.Response.Charset = "gb2312";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            HttpContext.Current.Response.End();
            return true;
        }
        catch
        {
            return false;
        }
    }
}
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,