Excel导出无法正常工作

时间:2014-01-11 11:15:44

标签: c# asp.net

Excel导出无法正常工作。代码出错了,我需要帮助。

我的代码在

之下
<asp:Panel ID="Panel2" runat="server" ScrollBars="Vertical">
        <asp:GridView ID="GridView1" runat="server" 
            AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
            ForeColor="#333333" GridLines="None" Height="171px" Width="748px" 
             RowStyle-VerticalAlign="Bottom" 
            onrowcancelingedit="GridView1_RowCancelingEdit" 
            onrowdatabound="GridView1_RowDataBound" onrowdeleting="GridView1_RowDeleting" 
            onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" DataKeyNames="AccountId" >
            <AlternatingRowStyle BackColor="White" BorderColor="Black" BorderStyle="Solid" 
                BorderWidth="1px" />
            <Columns>
                <asp:TemplateField HeaderText="Action">
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField HeaderText="Edit" ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
                <asp:TemplateField HeaderText="AccountId" ShowHeader="False" Visible="False">
                    <ItemTemplate>
                        <asp:Label ID="Label15" runat="server" Text="<%# bind('AccountId') %>"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Account Name">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text="<%# bind('AccountName') %>"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text="<%# bind('AccountName') %>"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Account Desc">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server" Text="<%# bind('AccountDesc') %>"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label5" runat="server" Text="<%# bind('AccountDesc') %>"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Account Type">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox4" runat="server" Text="<%# bind('AccountType') %>"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label6" runat="server" Text="<%# bind('AccountType') %>"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="TDS %">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox5" runat="server" Text="<%# bind('TDSper') %>"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label7" runat="server" Text="<%# bind('TDSper') %>"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ADV %">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox6" runat="server" Text="<%# bind('Advper') %>"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label8" runat="server" Text="<%# bind('Advper') %>"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>

这是onclick excel图像编码在下面......

protected void ImageButton7_Click(object sender, ImageClickEventArgs e)
    {
        try
        {

            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Employees.xls"));
            Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            GridView1.AllowPaging = false;

            //Change the Header Row back to white color
            GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
            //Applying stlye to gridview header cells
            for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
            {
                GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
            }
            int j = 1;
            //Set alternate row color
            foreach (GridViewRow gvrow in GridView1.Rows)
            {
                gvrow.BackColor = System.Drawing.Color.White;
                if (j <= GridView1.Rows.Count)
                {
                    if (j % 2 != 0)
                    {
                        for (int k = 0; k < gvrow.Cells.Count; k++)
                        {
                            gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                        }
                    }
                }
                j++;
            }
            GridView1.RenderControl(htw);
            Response.Flush();
            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }

输出是excel表的下载,但它没有正确对齐。所以编码有什么问题。我的输出图像是这样的...... enter image description here

请给我解决方案。我正在回复你的回复。

2 个答案:

答案 0 :(得分:0)

我面临同样的问题。

这是一个解决方案。试试吧。 [测试此] [1]    Export/import to Excel with combo box

此程序导入和导出数据。我试过这个。按预期工作。感谢用户。

答案 1 :(得分:0)

此代码在我的项目中正常运行。

DataSet1TableAdapters.Mas_SubItemTableAdapter object_customer = new DataSet1TableAdapters.Mas_SubItemTableAdapter();
        DataSet1.Mas_SubItemDataTable table = object_customer.GetData();
        DataGrid dg = new DataGrid();
        dg.DataSource = table;
        dg.DataBind();
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename= Subitem.xls");
        Response.ContentType = "application/excel";
        //[].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
        System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        dg.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();

试试这个我希望它有用。