Dovevo inserire assolutamente questo primo metodo, altrimenti non andava proprio!
Ambiente: MS VS 2005 .net 2.0 aspx
/// <summary>
/// Questo metodo deve essere presente per un bug del framework .net 2.0
/// </summary>
/// <param name="control"></param>
public override void VerifyRenderingInServerForm(Control control)
{
return;
}
Poi ho tranquillamente popolato il foglio excel con la griglia di dati.
protected void Export_Click(object sender, EventArgs e)
{
try
{
Button button = (Button)sender;
HiddenField fieldId = (HiddenField)button.Parent.Parent.FindControl("Field_Id");
string yearMonth = fieldId.Value;
int lastDayMonth = DateTime.DaysInMonth(Convert.ToInt16(yearMonth.Substring(0, 4)), Convert.ToInt16(yearMonth.Substring(4, 2)));
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWrite);
htmlTextWriter.WriteLine("TITOLO DI ESEMPIO");
htmlTextWriter.WriteBreak();
htmlTextWriter.WriteBreak();
htmlTextWriter.WriteFullBeginTag("table border='1'");
htmlTextWriter.WriteFullBeginTag("tr");
htmlTextWriter.WriteFullBeginTag("td bgcolor='#BFCEEE' align='left'");
htmlTextWriter.WriteLine(" " + ConvertyearMonth(yearMonth));
htmlTextWriter.WriteEndTag("td");
htmlTextWriter.WriteEndTag("tr");
htmlTextWriter.WriteFullBeginTag("tr");
htmlTextWriter.WriteFullBeginTag("td");
GridView gridView = (GridView)button.Parent.Parent.FindControl("GridView1");
gridView.HeaderStyle.ForeColor = Color.Black;
gridView.RowStyle.ForeColor = Color.Black;
gridView.BorderWidth = Unit.Pixel(1);
int numRows = gridView.Rows.Count;
for (int i = 1; i < 32; i++)
{
if (i <= lastDayMonth)
{
if (Utility.Holiday(yearMonth.Substring(0, 4), yearMonth.Substring(4, 2), Convert.ToString(i)))
{
gridView.Columns[i+1 ].HeaderStyle.BackColor = Color.FromArgb(255, 119, 112);
gridView.Columns[i+1 ].HeaderStyle.ForeColor = Color.Black;
for (int j = 0; j < numRows; j++)
{
gridView.Rows[j].Cells[i+1 ].BackColor = Color.FromArgb(255, 119, 112);
}
}
}
}
gridView.RenderControl(htmlTextWriter);
htmlTextWriter.WriteEndTag("td");
htmlTextWriter.WriteEndTag("tr");
htmlTextWriter.WriteEndTag("table");
htmlTextWriter.WriteBreak();
htmlTextWriter.WriteFullBeginTag("table border='1'");
htmlTextWriter.WriteFullBeginTag("tr");
htmlTextWriter.WriteFullBeginTag("td");
GridView gridView2 = (GridView)button.Parent.Parent.FindControl("GridView2");
gridView.HeaderStyle.ForeColor = Color.Black;
gridView2.HeaderStyle.Height = Unit.Pixel(0);
gridView2.RowStyle.ForeColor = Color.Black;
gridView2.Width = Unit.Pixel(300);
gridView2.BorderWidth = Unit.Pixel(1);
gridView2.RenderControl(htmlTextWriter);
htmlTextWriter.WriteEndTag("td");
htmlTextWriter.WriteEndTag("tr");
htmlTextWriter.WriteEndTag("table");
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.Charset = "";
response.Buffer = true;
response.AddHeader("content-disposition", "attachment; filename=esempio.xls");
response.ContentType = "application/vnd.ms-excel";
response.Write(stringWrite.ToString());
response.End();
}
catch (Exception ex)
{
log.Error(ex);
}
}