现在想做一个下载的程序,当点击下载之后。把数据库里面的相应的字段填充到一个Excel文件里面,然后在吧Excel下载下来,请问问各位,这个应该怎么来做。谢谢了,一定给个实例。两三句话是说不清楚的,我是菜鸟,再次谢谢

解决方案 »

  1.   

    这个涉及到有几个技术点
    一个是数据的导出,一个是文件的下载。
    1:首先在目录生成一个XLS文件(完成导出的操作)
    2:再下载生成的XLS文件把思路理清,然后就去找相关的代码吧。
      

  2.   

    要代码前先自己把思路和需求理清。先读数据库生成相应的DATATABLE,然后DATATABLE导出EXCEL文件并下载。//dtData是要导出为Excel的DataTable,FileName是要导出的Excel文件名(不加.xls)
        public static void DataTable2Excel(System.Data.DataTable dtData, String FileName)
        {
            System.Web.UI.WebControls.GridView dgExport = null;
            //当前对话 
            System.Web.HttpContext curContext = System.Web.HttpContext.Current;
            //IO用于导出并返回excel文件 
            System.IO.StringWriter strWriter = null;
            System.Web.UI.HtmlTextWriter htmlWriter = null;        if (dtData != null)
            {
                //设置编码和附件格式 
                //System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)作用是方式中文文件名乱码
                curContext.Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");
                curContext.Response.ContentType = "application/vnd.ms-excel";
                curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
                curContext.Response.Charset = "GB2312";            //导出Excel文件 
                strWriter = new System.IO.StringWriter();
                htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);            //为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的GridView 
                dgExport = new System.Web.UI.WebControls.GridView();
                dgExport.DataSource = dtData.DefaultView;
                dgExport.AllowPaging = false;
                //如果需要设置Excel格式,需要加入下面一行代码,同时添加dgExport_RowDataBound事件
                dgExport.RowDataBound += new GridViewRowEventHandler(dgExport_RowDataBound);
                dgExport.DataBind();            //下载到客户端 
                dgExport.RenderControl(htmlWriter);
                curContext.Response.Write(strWriter.ToString());
                curContext.Response.End();
            }
        }
      

  3.   

    先获取数据再创建excel或读取excel模板填充值
    public void INSERT_Excel(DataView dvs,string strPath,string Name)
    {
    string s="";
    Excel.Application app=new Application();
    Excel._Workbook book;
    Excel._Worksheet sheet;
    book=(Excel._Workbook)app.Workbooks.Open(strPath,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,
    Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
    sheet=(Excel._Worksheet)book.Sheets[1];
    int j=dvs.Count;
    Excel.Range ran1=app.ActiveCell;
    ran1=sheet.get_Range(sheet.Cells[1,1],sheet.Cells[1,9]);
    ran1.Value2=Name;
    for(int i=0;i<dvs.Count;i++)
    {
    try
    {
    s=Convert.ToString(i);
    sheet.Cells[i+4,"A"]=dvs[i]["ID"].ToString().Trim();
    }
    catch(Exception ex)
    {
    HttpContext.Current.Response.Write("<script language='javascript'>alert('"+ex.Message+"')</script>");
    book.Close(null,null,null);
    app.Workbooks.Close();
    app.Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
    sheet=null;
    book=null;
    app=null;
    GC.Collect();
    HttpContext.Current.Response.Write("<script language='javascript'>alert('导出失败!')</script>");
    return;
    }
      

    book.Save();
    book.Close(null,null,null);
    app.Workbooks.Close();
    app.Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
    sheet=null;
    book=null;
    app=null;
    GC.Collect();
    GC.Collect();
    GC.Collect();
    HttpContext.Current.Response.Write("<script language='javascript'>alert('导出成功!')</script>");
    HttpContext.Current.Response.Write("<script language='javascript'>window.open('../Template_temp/A.xls','_bank')</script>");
    }
      

  4.   

    public string ExcelCache
            {
                get
                {
                    string _path = Path.Combine(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath, "ExcelBuffer\\");
                    if (!Directory.Exists(_path))
                    {
                        Directory.CreateDirectory(_path);
                    }
                    return _path;
                }        }                Report rpt = Report.LoadReport(Convert.ToInt32(reportID));
                    ExcelHeaderBuilder builder = new ExcelHeaderBuilder();
                    ExcelWorkbook book = new ExcelWorkbook();
                    book.CreateSheet(sheetName);
                    book.SetActiveSheet = sheetName;
                    ExcelWorksheet sheet = book.GetSheet(sheetName);
                    ExcelCellStyle style = book.CreateStyle();
                    style.BorderColour = EnumColours.Black;
                    style.BorderLineStyle = EnumLineStyle.Thin;
                    style.Font.Name = "黑体";
                    style.Font.Size = 12;
                    style.HorizontalAlignment = EnumHorizontalAlignment.Center;
                    style.VerticalAlignment = EnumVerticalAlignment.Center;                builder.Style = style;
                    builder.MakeHeader(sheet, rpt);                book.Save(path);
                        HttpContext.Current.Response.Clear();
                        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + Path.GetFileName(path));
                        HttpContext.Current.Response.ContentType = "application/octet-stream";
                        HttpContext.Current.Response.Charset = "GB2312";
                        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                        HttpContext.Current.Response.WriteFile(path);
                        HttpContext.Current.Response.Flush();参考