导出到Excel并冻结顶行

时间:2014-05-01 17:00:27

标签: c# asp.net excel

作为一名程序员,我认为我的工作是制作工具和程序,使每个人的工作更轻松。我有一个非常庞大的ASP.Net GridView(20页+),这对会计人员来说非常麻烦。所以我编写了一个导出到Excel按钮,它正在按预期工作。但现在,他们有一个非常长的xls,很难记住哪些列与哪些数据相关联。所以,我一直在寻找一种方法来导出相同的xls并启用FreezePanes,这样它就会在第一行被冻结的情况下打开。我没有找到这样做的例子。

这就是我现在拥有的东西,而且效果很好,所以我不想改变方法。我只想让这段代码冻结第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();
}

1 个答案:

答案 0 :(得分:0)

由于您实际上并未创建Excel文档,因此无法冻结窗格。您正在创建扩展名为.xls的HTML文档。 HTML没有&#34;冻结&#34;窗格或行。如果要使用Excel功能,则需要使用OpenXML,XML,Office Interop(在服务器上)或第三方工具生成真实的Excel文档。

请参阅使用XML

进行格式化的示例
<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>