如何使具有RowSpan>1的单元格垂直对齐

3

我想要这个 enter image description here,但是希望看起来像这个enter image description here。 请注意,当我将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();
}

}

}

它对我有效(请在此处查看图片),因此必须有其他东西搞乱了你的HTML。 - Tim Schmelter
顺便提一下,您是否考虑过创建一个真正的Excel表格而不是HTML表格?使用EPPlus非常简单。请参考此链接:http://stackoverflow.com/questions/7589768/exporting-data-to-excel-in-vb-net/7590023#7590023。 - Tim Schmelter
不,它创建了一个HTML表格,可以被Excel打开和解释。但它确实是纯HTML。 - Tim Schmelter
(更改文件扩展名为'txt',手动打开即可查看) - Tim Schmelter
正如我在更新中所说,我查看了sw.ToString()(纯HTML),它看起来很好。但是最终在Excel中,它的格式似乎不正确。 - Pavel Nefyodov
显示剩余4条评论
3个回答

2
请在CSS中完成这个操作!在您想要包含“England”单元格上设置一个类,并针对该类进行目标设置。
代码:
tCell.Text = "England";
tCell.CssClass = "className";

Css

td.className {
   vertical-align:middle;
}

编辑 好的,受欢迎的要求是解释为什么要在这里使用CSS而不是在创建表格单元格时设置它。

使用单独的样式表所得到的好处是对客户端上的这个元素的外观具有强大的控制力和控制能力。当你在代码中设置它时,你明确地表示它应该只是这样的 - 但是当你在CSS中设置它并使用样式表时,你可以做一些像针对不同平台、轻松更改位置、添加额外元素等的事情。这是行内样式和将样式拉出到单独的样式表中之间的老问题。如果您想了解更多,请阅读网上的相关讨论......

编辑2 我刚刚尝试了您粘贴的代码,它对我很好用,单元格是居中对齐的。我会使用Firebug来尝试找出是否有其他样式影响了表格单元格,使其具有这种行为。


这既没有解决OP的问题,也没有解释为什么他应该使用CSS而不是编译后的内联版本。 - Tim Schmelter
虽然不是我问题的完美解决方案,但已经非常接近了。我投了+1票表示有帮助。 - Pavel Nefyodov

2

最后我自己测试了一下。如果你在表格单元格中使用valign="middle",它似乎在Excel中无法正常工作。你必须使用CSS。

因此,不要使用

tCell.VerticalAlign = VerticalAlign.Middle

执行

tCell.Style.Add("vertical-align", "middle")

这会在Excel中正确对齐文本。如果您将其从Excel导出到HTML,则Excel本身使用CSS类。

但是重申我的评论,我建议生成一个真正的Excel文件,而不是创建可能无法正确打开和解释的HTML。 使用EPPlus非常简单:导出数据到Excel


刚刚检查了一下,它的运行非常好!实际上我并不是从Excel导出到HTML,而是相反地从HTML->Excel。那么我的方法和创建“真正”的Excel文件有什么区别呢? - Pavel Nefyodov
@Pavel:我只提到了Excel本身如何导出HTML,因为这是我检查Excel可以理解什么和不能理解什么的方式。你的文件与Excel文件之间的区别在于,你正在创建损坏的HTML(根本没有HTML/Head/Body标签)。一个“真正”的Excel文件不仅具有扩展名xlsxlsx,而且是一个隐藏的HTML文件。它是二进制的。这里有一些优缺点:https://dev59.com/FHVC5IYBdhLWcg3w51lv - Tim Schmelter

0
<td   align='left' valign='middle' ></td>

在代码后端导出Excel文件时无法工作,因此我在互联网上搜索并找到了这个示例。

td.className {
   vertical-align:middle;
}

这个节省了我的时间。 谢谢


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接