Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.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# 导出到Excel并冻结顶行_C#_Asp.net_Excel - Fatal编程技术网

C# 导出到Excel并冻结顶行

C# 导出到Excel并冻结顶行,c#,asp.net,excel,C#,Asp.net,Excel,作为一名编码员,我认为我的工作是制作工具和程序,使每个人的工作都更容易。我有一个ASP.NETGridView,它相当大,有20多页,对于会计人员来说,翻阅它非常麻烦。因此,我编写了一个导出到Excel按钮,它可以按预期工作。但现在,它们有一个很长的xls,很难记住哪列与哪种数据相关联。所以,我一直在寻找一种方法来导出启用了冻结窗格的相同xls,以便在第一行冻结时打开它。我没有找到一个这样做的例子 这是我目前所拥有的,它运行得很好,所以我不想改变方法。我只想让这个代码冻结第1行 protecte

作为一名编码员,我认为我的工作是制作工具和程序,使每个人的工作都更容易。我有一个ASP.NETGridView,它相当大,有20多页,对于会计人员来说,翻阅它非常麻烦。因此,我编写了一个导出到Excel按钮,它可以按预期工作。但现在,它们有一个很长的xls,很难记住哪列与哪种数据相关联。所以,我一直在寻找一种方法来导出启用了冻结窗格的相同xls,以便在第一行冻结时打开它。我没有找到一个这样做的例子

这是我目前所拥有的,它运行得很好,所以我不想改变方法。我只想让这个代码冻结第1行

protected void ExportGridviewToExcel(object sender, EventArgs e)
{
    string myMonth = MonthTextBox.Text;
    string myYear = YearTextBox.Text;
    SqlConnection myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["InterfaceDev"].ConnectionString);
    string myCommandText = "GatherZeroBillingExtensions";
    SqlCommand myCommand = new SqlCommand(myCommandText, myConn);
    myCommand.CommandType = CommandType.StoredProcedure;
    myCommand.Parameters.AddWithValue("@Month", myMonth);
    myCommand.Parameters.AddWithValue("@Year", myYear);
    SqlDataAdapter myDataAdapter = new SqlDataAdapter(myCommand);
    DataSet myDataSet = new DataSet();
    myDataAdapter.Fill(myDataSet);
    DataTable myDataTable = myDataSet.Tables[0];
    ZeroBillExtGridView.AllowPaging = false;
    ZeroBillExtGridView.DataSource = myDataTable;
    ZeroBillExtGridView.DataBind();

    string myDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    string myFilename = "ZeroBillingExtensions_" + DateTime.Now.ToShortDateString() + ".xls";
    string myFullyQualifiedPath = myDesktopPath + "\\" + myFilename;
    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", myFullyQualifiedPath));
    Response.ContentType = "application/ms-excel";
    StringWriter myStringWriter = new StringWriter();
    HtmlTextWriter myHtmlTextWriter = new HtmlTextWriter(myStringWriter);
    //Change the Header Row back to white color
    ZeroBillExtGridView.HeaderRow.Style.Add("background-color", "#FFFFFF");
    //Applying stlye to gridview header cells
    for (int i = 0; i < ZeroBillExtGridView.HeaderRow.Cells.Count; i++)
    {
        ZeroBillExtGridView.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
    }
    int j = 1;
    //Set alternate row color
    foreach (GridViewRow gvrow in ZeroBillExtGridView.Rows)
    {
        gvrow.BackColor = System.Drawing.Color.White;
        if (j <= ZeroBillExtGridView.Rows.Count)
        {
            if (j % 2 != 0)
            {
                for (int k = 0; k < gvrow.Cells.Count; k++)
                {
                    gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                }
            }
        }
        j++;
    }
    ZeroBillExtGridView.RenderControl(myHtmlTextWriter);
    Response.Write(myStringWriter.ToString());
    Response.End();
}

您不能使窗格冻结,因为您不是在实际创建Excel文档。您正在创建扩展名为.xls的HTML文档。HTML没有冻结窗格或行的概念。如果要使用Excel功能,需要使用OpenXML、XML、服务器上的Office Interop或第三方工具生成真正的Excel文档

请参见使用设置格式的示例


您正在将GridView导出为HTML。那是个坏主意。相反,使用一个库,例如,它专门支持冻结窗格。好吧,我们生活和学习。我不熟悉EPPlus,但只要在网站上停留几分钟,就可以清楚地看到它正是我所需要的。谢谢梅森!!梅森,我不得不再次发回邮件,感谢你把我引向EPPlus。多棒的工具库啊。我已经向一位开发人员朋友推荐了它。EPPlus是一个不错的选择!
<WorksheetOptions
xmlns="urn:schemas-microsoft-com:office:excel">

<Selected/>
<FreezePanes/>
<FrozenNoSplit/>

<!--- Bottom row number of top pane. --->
<SplitHorizontal>1</SplitHorizontal>

<!---
    Offset row of bottom frame. This is not the actual row
    number of the overall Excel document, but rather the
    index of the available rows for this pane. This number
    cannot exclude rows, it can merely set the offset
    scroll of this pane. (1) scrolls to the top of the
    frame (first row).
--->
<TopRowBottomPane>1</TopRowBottomPane>

</WorksheetOptions>