我想要这个 ,但是希望看起来像这个
。
请注意,当我将VerticalAlign.Middle更改为VerticalAlign.Top时,实际上它按预期工作。
请参见下面我正在尝试的代码:
// I assume that table table is already created and well-defined
// Create a new row
TableRow tRow = new TableRow();
tRow.HorizontalAlign = HorizontalAlign.Center;
// add the row to the table
table.Rows.Add(tRow);
// Create a new cell
TableCell tCell = new TableCell();
tCell.VerticalAlign = VerticalAlign.Middle; // Want to get it in the middle of two merged rows
tCell.RowSpan = 2;
tCell.Text = "England";
tCell.Font.Bold = true;
tRow.Cells.Add(tCell);
// Create new cell
tCell = new TableCell();
tCell.Text = "2010";
tCell.Font.Bold = true;
tRow.Cells.Add(tCell);
// Create new row
tRow = new TableRow();
// add the row to the table
table.Rows.Add(tRow);
// Create new cell
tCell = new TableCell();
tCell.Text = "2011";
tCell.Font.Bold = true;
tRow.Cells.Add(tCell);
更新: 请查看下面的额外代码。我没有HTML代码,但我看了一下sw.ToString(),格式看起来正确,但Excel文件似乎仍然没有正确格式化。我的浏览器是IE,但我认为这并不重要。我尝试使用tCell.CssClass = "className";,结果相同。
public static void Comparison_Report(string fileName)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// Create a table to contain the grid
Table table = new Table();
///----- Original code goes here----
// render the table into the htmlwriter
table.RenderControl(htw);
// render the htmlwriter into the response
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
}