【发布时间】:2015-06-05 11:37:05
【问题描述】:
我想要这个, 而是有这个。 请注意,当我将 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();
}
}
}
【问题讨论】:
-
它对我有用(见图片here),所以一定有别的东西弄乱了你的html。
-
哇,真快!对我来说,它也适用于 VerticalAlign.Top,但它不适用于 VerticalAlign.Middle。这就是为什么我提出这个问题。这在我的文件中没有其他内容。我发现您的评论比答案更有价值!
-
这在其他浏览器中是否有效,您用什么浏览器测试过?也发布生成的 HTML。
-
在 IE 中工作。请参阅 HTML 问题的更新。
-
附带说明,您是否考虑过创建一个真正的 excelsheet 而不是 HTML-Table?这是very simple with EPPlus。
标签: asp.net web-applications c#-2.0