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

各位好汉请伸出援助之手吧,是关于ASP.NET的问题,感激不尽

问题是 ASP.NET程序中导出数据到Excel的方法(在ASP.NET程序中如何导出数据到Excel),俺们没学这部分内容,考试又要考到,请各位帮帮忙吧,如果可以请跟俺说具体一点,有例子最好,感激不尽啊,谢谢!!!!!!!! --------------------编程问答--------------------
 /// <summary>
    /// 导出数据
    /// </summary>
    private void ExportExcel()
    {

        StringWriter sw = new StringWriter();
        StringBuilder header = new StringBuilder();

        header.Append("企业名称\t");
        header.Append("产品名称\t");
        header.Append("生产地点\t");
        header.Append("检验方式\t");
        header.Append("证书编号\t");
        header.Append("有效期\t");
        header.Append("发证日期\t");
        header.Append("备注\t");
        sw.WriteLine(header.ToString());
        //取每行数据
        string sql = string.Empty;
        if (this.hdSql.Value != string.Empty)
        {
            sql = "select * from view_spgl_qs_zx where "+this.hdSql.Value+" ";
        }
        else
        {
            sql = "select * from view_spgl_qs_zx ";
        }//where (Createcorp='" + Gs_Createcorp + "' or " + Gs_Viewall + " = 1)
        DataTable dt = AdoHelper.CreateHelper(AppConfig.IAInstance).ExecuteSqlDataset(sql).Tables[0];
        foreach (DataRow row in dt.Rows)
        {
            StringBuilder items = new StringBuilder();

            items.AppendFormat("{0}\t", row["corpname"].ToString());
            items.AppendFormat("{0}\t", row["productname"].ToString());
            items.AppendFormat("{0}\t", row["scdz"].ToString());
            items.AppendFormat("{0}\t", row["jyfs"].ToString());
            items.AppendFormat("{0}\t", row["annexremark"].ToString());
           
            if (row["usefullife"].ToString() != string.Empty)
            {
                items.AppendFormat("{0}\t", Convert.ToDateTime(row["usefullife"].ToString()).ToShortDateString());
            }
            else
            {
                items.AppendFormat("{0}\t", row["usefullife"].ToString());
            }
            if (row["fzrq"].ToString() != string.Empty)
            {
                items.AppendFormat("{0}\t", Convert.ToDateTime(row["fzrq"].ToString()).ToShortDateString());
            }
            else
            {
                items.AppendFormat("{0}\t", row["fzrq"].ToString());
            }
            items.AppendFormat("{0}\t", row["remark"].ToString());

            sw.WriteLine(items.ToString());
        }
        string fileName = HttpUtility.UrlEncode("QS证注销企业表" + DateTime.Now.ToString("-yyyyMMdd-HHmm"));
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
        Response.ContentType = "application/ms-excel";
        Response.Charset = "utf-8";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.Write(sw.ToString());
        sw.Close();
        Response.End();

    }
--------------------编程问答--------------------  DataTable dt = new DataTable();
  HttpContext context =HttpContext.Current;
 context.Response.Clear();
 context.Response.Buffer = true;
  context.Response.Charset = "GB2312";
 System.Globalization.CultureInfo myC= new System.Globalization.CultureInfo("ZH-CN", true);
    StringWriter sw = new StringWriter(myC);            
string header = "";
           foreach (DataColumn column in dt.Columns)
           {
               header += " " + column.Caption.ToString() +"\t";
           }
           if (header != "")
               header = header.Remove(0, 1) + " ";
           sw.WriteLine(header);

            foreach (DataRow dr in dt.Rows)
            {
                string record = "";
                for (int i = 0; i < dt.Columns.Count; i++)
               {
                   record += " " + dr[i].ToString().Replace(" ", " ").ToString()  + "\t";
                }

                if (record != "")
                    record = record.Remove(0, 1) + " ";
               sw.WriteLine(record);
          }
           sw.Close();
           context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(ExcelFileName, System.Text.Encoding.UTF8) + ".xls");
           context.Response.ContentType = "application/ms-excel";            context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            context.Response.Write(sw);
           context.Response.End();
            
--------------------编程问答--------------------
 /// <summary>
    /// 导出数据
    /// </summary>
    private void ExportExcel()
    {

        StringWriter sw = new StringWriter();
        StringBuilder header = new StringBuilder();

        header.Append("企业名称\t");
        header.Append("产品名称\t");
        header.Append("生产地点\t");
        header.Append("检验方式\t");
        header.Append("证书编号\t");
        header.Append("有效期\t");
        header.Append("发证日期\t");
        header.Append("备注\t");
        sw.WriteLine(header.ToString());
        //取每行数据
        string sql = string.Empty;
        if (this.hdSql.Value != string.Empty)
        {
            sql = "select * from view_spgl_qs_zx where "+this.hdSql.Value+" ";
        }
        else
        {
            sql = "select * from view_spgl_qs_zx ";
        }//where (Createcorp='" + Gs_Createcorp + "' or " + Gs_Viewall + " = 1)
        DataTable dt = AdoHelper.CreateHelper(AppConfig.IAInstance).ExecuteSqlDataset(sql).Tables[0];
        foreach (DataRow row in dt.Rows)
        {
            StringBuilder items = new StringBuilder();

            items.AppendFormat("{0}\t", row["corpname"].ToString());
            items.AppendFormat("{0}\t", row["productname"].ToString());
            items.AppendFormat("{0}\t", row["scdz"].ToString());
            items.AppendFormat("{0}\t", row["jyfs"].ToString());
            items.AppendFormat("{0}\t", row["annexremark"].ToString());
           
            if (row["usefullife"].ToString() != string.Empty)
            {
                items.AppendFormat("{0}\t", Convert.ToDateTime(row["usefullife"].ToString()).ToShortDateString());
            }
            else
            {
                items.AppendFormat("{0}\t", row["usefullife"].ToString());
            }
            if (row["fzrq"].ToString() != string.Empty)
            {
                items.AppendFormat("{0}\t", Convert.ToDateTime(row["fzrq"].ToString()).ToShortDateString());
            }
            else
            {
                items.AppendFormat("{0}\t", row["fzrq"].ToString());
            }
            items.AppendFormat("{0}\t", row["remark"].ToString());

            sw.WriteLine(items.ToString());
        }
        string fileName = HttpUtility.UrlEncode("QS证注销企业表" + DateTime.Now.ToString("-yyyyMMdd-HHmm"));
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
        Response.ContentType = "application/ms-excel";
        Response.Charset = "utf-8";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.Write(sw.ToString());
        sw.Close();
        Response.End();

    }
--------------------编程问答--------------------
 /// <summary>
    /// 导出数据
    /// </summary>
    private void ExportExcel()
    {

        StringWriter sw = new StringWriter();
        StringBuilder header = new StringBuilder();

        header.Append("企业名称\t");
        header.Append("产品名称\t");
        header.Append("生产地点\t");
        header.Append("检验方式\t");
        header.Append("证书编号\t");
        header.Append("有效期\t");
        header.Append("发证日期\t");
        header.Append("备注\t");
        sw.WriteLine(header.ToString());
        //取每行数据
        string sql = string.Empty;
        if (this.hdSql.Value != string.Empty)
        {
            sql = "select * from view_spgl_qs_zx where "+this.hdSql.Value+" ";
        }
        else
        {
            sql = "select * from view_spgl_qs_zx ";
        }//where (Createcorp='" + Gs_Createcorp + "' or " + Gs_Viewall + " = 1)
        DataTable dt = AdoHelper.CreateHelper(AppConfig.IAInstance).ExecuteSqlDataset(sql).Tables[0];
        foreach (DataRow row in dt.Rows)
        {
            StringBuilder items = new StringBuilder();

            items.AppendFormat("{0}\t", row["corpname"].ToString());
            items.AppendFormat("{0}\t", row["productname"].ToString());
            items.AppendFormat("{0}\t", row["scdz"].ToString());
            items.AppendFormat("{0}\t", row["jyfs"].ToString());
            items.AppendFormat("{0}\t", row["annexremark"].ToString());
           
            if (row["usefullife"].ToString() != string.Empty)
            {
                items.AppendFormat("{0}\t", Convert.ToDateTime(row["usefullife"].ToString()).ToShortDateString());
            }
            else
            {
                items.AppendFormat("{0}\t", row["usefullife"].ToString());
            }
            if (row["fzrq"].ToString() != string.Empty)
            {
                items.AppendFormat("{0}\t", Convert.ToDateTime(row["fzrq"].ToString()).ToShortDateString());
            }
            else
            {
                items.AppendFormat("{0}\t", row["fzrq"].ToString());
            }
            items.AppendFormat("{0}\t", row["remark"].ToString());

            sw.WriteLine(items.ToString());
        }
        string fileName = HttpUtility.UrlEncode("QS证注销企业表" + DateTime.Now.ToString("-yyyyMMdd-HHmm"));
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
        Response.ContentType = "application/ms-excel";
        Response.Charset = "utf-8";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.Write(sw.ToString());
        sw.Close();
        Response.End();

    }
--------------------编程问答--------------------  DataTable dt = new DataTable();
  HttpContext context =HttpContext.Current;
 context.Response.Clear();
 context.Response.Buffer = true;
  context.Response.Charset = "GB2312";
 System.Globalization.CultureInfo myC= new System.Globalization.CultureInfo("ZH-CN", true);
    StringWriter sw = new StringWriter(myC);            
string header = "";
           foreach (DataColumn column in dt.Columns)
           {
               header += " " + column.Caption.ToString() +"\t";
           }
           if (header != "")
               header = header.Remove(0, 1) + " ";
           sw.WriteLine(header);

            foreach (DataRow dr in dt.Rows)
            {
                string record = "";
                for (int i = 0; i < dt.Columns.Count; i++)
               {
                   record += " " + dr[i].ToString().Replace(" ", " ").ToString()  + "\t";
                }

                if (record != "")
                    record = record.Remove(0, 1) + " ";
               sw.WriteLine(record);
          }
           sw.Close();
           context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(ExcelFileName, System.Text.Encoding.UTF8) + ".xls");
           context.Response.ContentType = "application/ms-excel";            context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            context.Response.Write(sw);
           context.Response.End();
            
--------------------编程问答--------------------
 /// <summary>
    /// 导出数据
    /// </summary>
    private void ExportExcel()
    {

        StringWriter sw = new StringWriter();
        StringBuilder header = new StringBuilder();

        header.Append("企业名称\t");
        header.Append("产品名称\t");
        header.Append("生产地点\t");
        header.Append("检验方式\t");
        header.Append("证书编号\t");
        header.Append("有效期\t");
        header.Append("发证日期\t");
        header.Append("备注\t");
        sw.WriteLine(header.ToString());
        //取每行数据
        string sql = string.Empty;
        if (this.hdSql.Value != string.Empty)
        {
            sql = "select * from view_spgl_qs_zx where "+this.hdSql.Value+" ";
        }
        else
        {
            sql = "select * from view_spgl_qs_zx ";
        }//where (Createcorp='" + Gs_Createcorp + "' or " + Gs_Viewall + " = 1)
        DataTable dt = AdoHelper.CreateHelper(AppConfig.IAInstance).ExecuteSqlDataset(sql).Tables[0];
        foreach (DataRow row in dt.Rows)
        {
            StringBuilder items = new StringBuilder();

            items.AppendFormat("{0}\t", row["corpname"].ToString());
            items.AppendFormat("{0}\t", row["productname"].ToString());
            items.AppendFormat("{0}\t", row["scdz"].ToString());
            items.AppendFormat("{0}\t", row["jyfs"].ToString());
            items.AppendFormat("{0}\t", row["annexremark"].ToString());
           
            if (row["usefullife"].ToString() != string.Empty)
            {
                items.AppendFormat("{0}\t", Convert.ToDateTime(row["usefullife"].ToString()).ToShortDateString());
            }
            else
            {
                items.AppendFormat("{0}\t", row["usefullife"].ToString());
            }
            if (row["fzrq"].ToString() != string.Empty)
            {
                items.AppendFormat("{0}\t", Convert.ToDateTime(row["fzrq"].ToString()).ToShortDateString());
            }
            else
            {
                items.AppendFormat("{0}\t", row["fzrq"].ToString());
            }
            items.AppendFormat("{0}\t", row["remark"].ToString());

            sw.WriteLine(items.ToString());
        }
        string fileName = HttpUtility.UrlEncode("QS证注销企业表" + DateTime.Now.ToString("-yyyyMMdd-HHmm"));
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
        Response.ContentType = "application/ms-excel";
        Response.Charset = "utf-8";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.Write(sw.ToString());
        sw.Close();
        Response.End();

    }
我晕了发了好几遍都回复不成功。 --------------------编程问答-------------------- 汗死。都发成功了啊。SORRY 。我这里一直不显示还以为没发成功。 --------------------编程问答--------------------

public void ExportToExcel(string Filename, DataGrid gridview)
    {
        page.Response.Clear();
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
        Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(Filename + DateTime.Now.ToShortDateString(), System.Text.Encoding.UTF8) + ".xls\"");
        System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        gridview.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }
--------------------编程问答-------------------- asp.net code 
using Microsoft.Office.Interop.Excel;

try 
{
string filePath = parameters.GetFileName ;
     
if (System.IO.File.Exists(filePath)==false)
{
    System.IO.File.Create(filePath,65535).Close();

 
}

Microsoft.Office.Interop.Excel.ApplicationClass xapp = new Microsoft.Office.Interop.Excel.ApplicationClass();//lauch excel application
xapp.Visible=false; 

xapp.DisplayAlerts=false; 
Microsoft.Office.Interop.Excel.Workbook wb=(Microsoft.Office.Interop.Excel.Workbook)xapp.Workbooks.Open(filePath,Type.Missing, 
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing,Type.Missing,Type.Missing);

Microsoft.Office.Interop.Excel.Worksheet ws=(Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1); 

ws.Name="11111111";
if (ws.Name.CompareTo("")==0 )
{
ws.Name="h" ;   
}
int i;
for (i=1 ;i<=10 ;i++)
{
ws.Cells[1,i]="11111111";
}

   
wb.Save();
wb.RefreshAll() ;

System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
ws=null; 
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
wb=null;
xapp.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComObject(xapp);   
xapp=null; 
System.GC.Collect();
}
catch(Exception ex)
{
return;
} --------------------编程问答-------------------- http://topic.csdn.net/u/20071015/12/a9c49cb4-a83e-44ae-bdca-5b942b920003.html --------------------编程问答-------------------- 天啊,好多代码啊,看不太明白,有注释就好了,呵呵,有点小笨,不过还是感激不尽啊,非常感谢!!!
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,