【问题标题】:How to export images to excel from gridview?如何将图像从gridview导出到excel?
【发布时间】:2016-07-19 13:23:01
【问题描述】:

在这里,我正在将 gridview 导出到 excel,在我的 gridview 中,我有一些图像我也想导出图像。这里我附上了可能的代码。这里我使用的是 EPPlus

 var products = GetProducts();
        gvDetails.DataSource = products;
        gvDetails.DataBind();
        ExcelPackage excel = new ExcelPackage();
        var workSheet = excel.Workbook.Worksheets.Add("Products");
        var totalCols = gvDetails.Rows[0].Cells.Count;
        var totalRows = gvDetails.Rows.Count;
        var headerRow = gvDetails.HeaderRow;
        for (var col = 1; col <= totalCols; col++)
        {
            workSheet.Cells[1, col].Value = products.Columns[col - 1].ColumnName;
        }
        for (var row = 1; row <= totalRows; row++)
        {

            for (var col = 0; col < totalCols; col++)
            {

                workSheet.Cells[row + 1, col + 1].Value = products.Rows[row - 1][col];


            }
        }
        using (var memoryStream = new MemoryStream())
        {
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment;  filename=products.xlsx");
            excel.SaveAs(memoryStream);
            memoryStream.WriteTo(Response.OutputStream);
            Response.Flush();
            Response.End();
        }



public DataTable GetProducts()
    {
        using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString))
        using (var cmd = new SqlCommand("SELECT * FROM Glines", conn))
        using (var adapter = new SqlDataAdapter(cmd))
        {
            var products = new DataTable();
            adapter.Fill(products);       
 return products;
        }
    }

这里我也附上我的数据库表(glines)快照。enter image description here

【问题讨论】:

    标签: c# asp.net excel gridview


    【解决方案1】:

    这是一个工作示例

     <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                    <Columns>
                        <asp:BoundField DataField="Name" HeaderText="Name" />
                        <asp:BoundField DataField="Job" HeaderText="Job" />
                        <asp:BoundField DataField="Location" HeaderText="Location" />
                        <asp:ImageField DataImageUrlField="Image" HeaderText="Image">
                        </asp:ImageField>
                    </Columns>
    
                </asp:GridView>
            </div>
    
            <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
            </form>
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    GridView1.DataSource = GetTable();
                    GridView1.DataBind();
                }
            }
            static DataTable GetData()
            {
                DataTable table = new DataTable();
                table.Columns.Add("Name", typeof(string));
                table.Columns.Add("Job", typeof(string));
                table.Columns.Add("Location", typeof(string));
                table.Columns.Add("Image");
    
                table.Rows.Add("JP", "XXX", "QQQQ", "http://localhost:4832/images/JP.png");
                table.Rows.Add("HP", "TTT", "AAAA", "http://localhost:4832/images/HP.png");
                table.Rows.Add("SQ", "YYY", "HHHH", "http://localhost:4832/images/SQ.png");
                table.Rows.Add("XS", "EEE", "UUUU", "http://localhost:4832/images/XS.png");
                return table;
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                Response.ClearContent();
                Response.AddHeader("content-disposition", "attachment; filename=test.xls");
                Response.ContentType = "application/excel";
                System.IO.StringWriter sw = new System.IO.StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                GridView1.RenderControl(htw);
                Response.Write(sw.ToString());
                Response.End();
            }
            public override void VerifyRenderingInServerForm(Control control)
            {
    
            }
    

    【讨论】:

    • 我试过上面的代码。它成功地为我工作。但生成的 excel 文件不应该是正确的格式。并且在文档中显示的图像非常大,所以我正在尝试使用上面的代码。
    猜你喜欢
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    相关资源
    最近更新 更多