如何使用自动调整图像将包含图像的ASP NET GridView导出到Excel

时间:2019-02-28 01:42:31

标签: c# asp.net sql-server excel export-to-excel

我有一个包含图像的网格视图,我想将其导出到excel。但是我的问题是,当我将其导出到excel中时,图像的大小不符合excel单元格的高度和宽度。

所以我的问题是应该怎么解决这个问题?

这是我尝试过的

Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GV5SAudit.AllowPaging = false;
this.BindGridAudit(FStatus.SelectedValue, FStartDate.Text, FEndDate.Text, FArea.SelectedValue);

GV5SAudit.Columns[1].Visible = false;
GV5SAudit.Columns[3].Visible = false;
GV5SAudit.Columns[11].Visible = false;
GV5SAudit.Columns[14].Visible = false;
GV5SAudit.Columns[16].Visible = false;
GV5SAudit.Columns[18].Visible = false;
GV5SAudit.Columns[19].Visible = false;
GV5SAudit.Columns[20].Visible = false;
GV5SAudit.Columns[21].Visible = false;
GV5SAudit.HeaderRow.ForeColor = System.Drawing.Color.Black;
GV5SAudit.HeaderRow.HorizontalAlign = HorizontalAlign.Center;

for (int i = 0; i < 13; i++)
{
    GV5SAudit.Columns[i].ItemStyle.HorizontalAlign = HorizontalAlign.Center;
}

GV5SAudit.RowStyle.VerticalAlign = VerticalAlign.Middle;
GV5SAudit.HeaderRow.BackColor = System.Drawing.Color.Gray;

for (int i = 0; i < GV5SAudit.Rows.Count;i++ )
{
    for(int j=0; j<13; j++)
    {
       GV5SAudit.Rows[i].Cells[j].ForeColor = System.Drawing.Color.Black;
    }
}

for (int i = 0; i < GV5SAudit.Rows.Count; i++)
{
    GridViewRow row = GV5SAudit.Rows[i];
    row.Attributes.Add("class", "textmode");
}

GV5SAudit.RenderControl(hw);

string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();

0 个答案:

没有答案