Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将gridview导出到.xls_C#_Asp.net_Excel - Fatal编程技术网

C# 将gridview导出到.xls

C# 将gridview导出到.xls,c#,asp.net,excel,C#,Asp.net,Excel,我正在处理一个有gridview控件的网页。我需要将数据转换为.xls格式。我可以创建.xls文件并传输数据,但问题是我需要在excel工作表的后台显示网格单元格。现在,它只显示一个没有网格单元格的空白背景。否则,gridview会被很好地传输。由于此问题,打印.xls文件是一个问题。列数较多的表不是在压缩,而是在2-3页上打印。我的代码如下: public static void ExportToXLS(string fileName, GridView gv,string companyNa

我正在处理一个有gridview控件的网页。我需要将数据转换为.xls格式。我可以创建.xls文件并传输数据,但问题是我需要在excel工作表的后台显示网格单元格。现在,它只显示一个没有网格单元格的空白背景。否则,gridview会被很好地传输。由于此问题,打印.xls文件是一个问题。列数较多的表不是在压缩,而是在2-3页上打印。我的代码如下:

public static void ExportToXLS(string fileName, GridView gv,string companyName,string reportTitle , string period)
    {
        //For writing to XLS file
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {

                Table tableReport = new Table();
                tableReport.GridLines = gv.GridLines;

                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    ReportList.PrepareControlForExport(gv.HeaderRow);
                    tableReport.Rows.Add(gv.HeaderRow);
                }

                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    ReportList.PrepareControlForExport(row);
                    tableReport.Rows.Add(row);
                }

                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    ReportList.PrepareControlForExport(gv.FooterRow);
                    tableReport.Rows.Add(gv.FooterRow);
                }

                //Takes value of company name
                System.Web.UI.WebControls.Label labelCompany = new System.Web.UI.WebControls.Label();
                labelCompany.Text = companyName;
                labelCompany.Font.Bold = true;

                //Takes value of report title
                System.Web.UI.WebControls.Label labelReport = new System.Web.UI.WebControls.Label();
                labelReport.Text = reportTitle;
                labelReport.Font.Bold = true;

                //Takes value of report period
                System.Web.UI.WebControls.Label labelPeriod = new System.Web.UI.WebControls.Label();
                labelPeriod.Text = period;

                //  render the htmlwriter into the response
                htw.Write("<center>");
                labelCompany.RenderControl(htw);
                htw.Write("<br/>");
                labelReport.RenderControl(htw);
                htw.Write("<br/>");
                labelPeriod.RenderControl(htw);
                htw.Write("</center>");
                htw.Write("<br/>");
                tableReport.RenderControl(htw);
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }
public static void ExportToXLS(字符串文件名、GridView gv、字符串companyName、字符串报告标题、字符串句点)
{
//用于写入XLS文件
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(“内容处置”,string.Format(“附件;文件名={0}”,文件名));
HttpContext.Current.Response.ContentType=“应用程序/ms excel”;
使用(StringWriter sw=new StringWriter())
{
使用(HtmlTextWriter htw=新的HtmlTextWriter(sw))
{
tableReport=新表();
tableReport.GridLines=gv.GridLines;
//将标题行添加到表中
如果(gv.HeaderRow!=null)
{
报告列表。准备控制出口(gv.HeaderRow);
tableReport.Rows.Add(gv.HeaderRow);
}
//将每个数据行添加到表中
foreach(gv.Rows中的GridViewRow行)
{
ReportList.PrepareControlForExport(行);
tableReport.Rows.Add(行);
}
//将页脚行添加到表中
如果(gv.FooterRow!=null)
{
ReportList.PrepareControlForExport(gv.FooterRow);
tableReport.Rows.Add(gv.FooterRow);
}
//接受公司名称的价值
System.Web.UI.WebControls.Label labelCompany=new System.Web.UI.WebControls.Label();
labelCompany.Text=companyName;
labelCompany.Font.Bold=true;
//获取报告标题的值
System.Web.UI.WebControls.Label labelReport=new System.Web.UI.WebControls.Label();
labelReport.Text=报告标题;
labelReport.Font.Bold=true;
//取报告期的价值
System.Web.UI.WebControls.Label labelPeriod=new System.Web.UI.WebControls.Label();
labelPeriod.Text=周期;
//将htmlwriter呈现到响应中
htw.写(“”);
labelCompany.RenderControl(htw);
htw.写(“
”); labelReport.RenderControl(htw); htw.写(“
”); labelPeriod.RenderControl(htw); htw.写(“”); htw.写(“
”); tableReport.RenderControl(htw); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End(); } } }
有什么建议吗?

参考:

  Response.Clear();
  Response.Buffer = true;
  Response.AddHeader("content-disposition",attachment;filename=GridViewExport.xls");
  Response.Charset = "";
  Response.ContentType = "application/vnd.ms-excel";

  StringWriter sw = new StringWriter();
  HtmlTextWriter hw = new HtmlTextWriter(sw);
试试这个:

protected void btnExportExcel_Click(object sender, EventArgs e)

    {     

        Response.Clear();
        Response.Buffer = true;

        Response.AddHeader("content-disposition",

         "attachment;filename=GridViewExport.xls");

        Response.Charset = "";

        Response.ContentType = "application/vnd.ms-excel";

        StringWriter sw = new StringWriter();

        HtmlTextWriter hw = new HtmlTextWriter(sw);

        grdExport.AllowPaging = false;

        oMailing.GetData(out ODs);

        grdExport.DataSource = ODs;

        grdExport.DataBind();

        //Change the Header Row back to white color

        grdExport.HeaderRow.Style.Add("background-color", "#FFFFFF");

        //Apply style to Individual Cells

        grdExport.HeaderRow.Cells[0].Style.Add("background-color", "green");

        grdExport.HeaderRow.Cells[1].Style.Add("background-color", "green");

        grdExport.HeaderRow.Cells[2].Style.Add("background-color", "green");

        grdExport.HeaderRow.Cells[3].Style.Add("background-color", "green");

        grdExport.HeaderRow.Cells[4].Style.Add("background-color", "green");

        grdExport.RenderControl(hw);

        //style to format numbers to string

        string style = @"<style> .textmode { mso-number-format:\@; } </style>";

        Response.Write(style);

        Response.Output.Write(sw.ToString());

        Response.Flush();

        Response.End();

    }
protectedvoid btnExportExcel\u单击(对象发送方,事件参数e)
{     
Response.Clear();
Response.Buffer=true;
Response.AddHeader(“内容处置”,
“附件;文件名=GridViewExport.xls”);
响应。Charset=“”;
Response.ContentType=“application/vnd.ms excel”;
StringWriter sw=新的StringWriter();
HtmlTextWriter hw=新的HtmlTextWriter(sw);
grdExport.allowpage=false;
oMailing.GetData(输出ODs);
grdExport.DataSource=ODs;
grdExport.DataBind();
//将标题行更改回白色
grdExport.HeaderRow.Style.Add(“背景色”,“#FFFFFF”);
//将样式应用于单个单元格
grdExport.HeaderRow.Cells[0]。Style.Add(“背景色”、“绿色”);
grdExport.HeaderRow.Cells[1]。Style.Add(“背景色”、“绿色”);
grdExport.HeaderRow.Cells[2]。Style.Add(“背景色”、“绿色”);
grdExport.HeaderRow.Cells[3]。Style.Add(“背景色”、“绿色”);
grdExport.HeaderRow.Cells[4]。Style.Add(“背景色”、“绿色”);
grdExport.RenderControl(硬件);
//将数字格式化为字符串的样式
字符串样式=@“.textmode{mso数字格式:\@;}”;
回应。写作(风格);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}

所有答案都会满足您的需求-您只是缺少格式部分

最终,您真正创建的是一个HTML文件(带有HTML表格),Excel可以读取。@bhaskarredymule中的响应头“强制”客户端将文件视为“xls”文件,如果它运行并打开Excel(但底线是它的不是真正的“本机”Excel文件)

现在,这已经不是问题了,用HTML思考。像用HTML一样设置列、行和文本内容的样式。这就是控制格式的方法(例如,老式的“nowrap”可以防止包装单元格内容、字体大小等)

我已经有一段时间没有这样做了(当我需要这样做时,我已经转到Excel XML和VB.Net XML文本),所以我不确定您对
RenderControl
…的控制级别有多高,或者您是否必须进一步“老派化”并从头开始构建HTML表字符串….

首先请参考此:,希望它能帮助您解决问题

参考:

您可以参考下面链接中的导出到Excel控件。这是一个自定义控件。希望它能帮助您

:

protectedvoid btnExportExcel\u单击(对象发送方,事件参数e)
{
R
 protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;

            Response.AddHeader("content-disposition",
             "attachment;filename=GridViewExport.xls");

            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);

            grdExport.AllowPaging = false;
            oMailing.GetData(out ODs);
            grdExport.DataSource = ODs;
            grdExport.DataBind();

            //Change the Header Row back to white color
            grdExport.HeaderRow.Style.Add("background-color", "#FFFFFF");

            //Apply style to Individual Cells
            grdExport.HeaderRow.Cells[0].Style.Add("background-color", "green");

            grdExport.HeaderRow.Cells[1].Style.Add("background-color", "green");

            grdExport.HeaderRow.Cells[2].Style.Add("background-color", "green");

            grdExport.HeaderRow.Cells[3].Style.Add("background-color", "green");

            grdExport.HeaderRow.Cells[4].Style.Add("background-color", "green");

            for (int i = 0; i < grdExport.Rows.Count; i++)
            {
                GridViewRow row = grdExport.Rows;

                //Change Color back to white
                row.BackColor = System.Drawing.Color.White;

                //Apply text style to each Row
                row.Attributes.Add("class", "textmode");

                //Apply style to Individual Cells of Alternating Row
                if (i % 2 != 0)
                {
                    row.Cells[0].Style.Add("background-color", "#C2D69B");
                    row.Cells[1].Style.Add("background-color", "#C2D69B");
                    row.Cells[2].Style.Add("background-color", "#C2D69B");
                    row.Cells[3].Style.Add("background-color", "#C2D69B");
                    row.Cells[4].Style.Add("background-color", "#C2D69B");
                }

            }
            grdExport.RenderControl(hw);
            //style to format numbers to string
            string style = @"<style> .textmode { mso-number-format:\@; } </style>";
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }