如何将单元格垂直对齐 RowSpan>1
我想要这个 , 而是使用
。 请注意,当我将 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();
}
}
}
I want to have this ,
but instead have this .
Please note, when I change VerticalAlign.Middle to VerticalAlign.Top it actually works as expected.
Please see below code that I am trying:
// 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);
Update: Please see extra code below. I don't have html code as such, but I looked at sw.ToString() and formatting looks right but still excel file does not seem to be rightly formatted. My browser is IE but I think it does not matter.
I tried tCell.CssClass = "className"; result is the same.
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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最后我自己测试了一下。如果您在表格单元格上使用
valign="middle"
,它似乎无法在 Excel 中工作。您必须使用 CSS。因此,代替
do
That 将在 Excel 中正确对齐文本。如果将 Excel 导出为 html,Excel 本身会使用 css 类。
但重复我的评论,我建议生成一个真正的 Excel 文件,而不是创建可以正确打开和解释的 html。
使用 EPPlus 非常简单:将数据导出到 Excel
Finally i've tested it by myself. It seems not to work in Excel if you use
valign="middle"
on your table-cell. You must use CSS.So instead of
do
That will align the text correctly in excel. Excel itself uses css-classes if you export it from excel to html.
But repeating my comment, i would recommend to generate a real excel file instead of creating html that might be openened and interpreted correctly.
It's very simple with EPPlus: Exporting data to excel
请在 CSS 中这样做!!在您希望其中包含“英格兰”的单元格上设置一个类别并定位该类别。
代码
CSS
编辑
好吧,根据大众的需求,解释一下为什么在这里使用 CSS,而不是在创建表格单元格时设置它。
通过使用单独的样式表,您将获得大量的权力并控制该元素在客户端上的外观。当你在代码中设置它时,你明确地说它应该只是这样 - 但是当你在 CSS 中设置它并使用样式表时,你可以执行诸如针对不同平台、轻松更改位置、添加额外元素等操作。关于使用内联样式和将样式拉到单独的工作表中的旧讨论。我认为这个话题在网上讨论得很好,如果你愿意阅读更多相关内容的话......
编辑 2
我只是在粘贴时尝试了您的代码,它对我来说效果很好 - 单元格居中对齐。我会使用 Firebug 来尝试找出是否有一些其他样式作用于该表格单元格以使其具有此行为。
Do this in CSS please!! Set a class on the cell that you want to have 'England' in it and target that class.
Code
Css
Edit
Ok so by popular demand an explanation for why to use CSS here rather than set this in the creation of the table cell.
What you gain from using a seperate stylesheet is a whole heap of power and control on how this element is going to look on the client. When you set this in code you are explicitly saying it should only be like this - however when you set it in CSS and use a stylesheet you can do things like target different platforms, change the position easily, add extra elements etc. This is the old discussion between having inline style and pulling the style out into a separate sheet. I think this topic is quite well discussed on the web if you care to read more about it...
Edit 2
I just tried your code as you pasted it and it worked fine for me - the cell was middle aligned. I would use Firebug to try figure out if there are some other styles that are acting on that table cell to make it have this behavior.
无法从代码后面导出Excel文件。所以我在互联网上搜索并找到了这个例子。
这节省了我的时间。
谢谢
Not working in exporting excel file from code behind.so i search in internet and found this example.
this save my time.
Thanks