这里我将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)snap。enter image description here
答案 0 :(得分:0)
这是一个工作样本
<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)
{
}