简体   繁体   English

将ID添加到HTML表代码字符串

[英]Add IDs To HTML Table Code String

I have an Excel spreadsheet that I have exported as an HTML file. 我有一个已导出为HTML文件的Excel电子表格。 The Excel file has 'defined names' for some of the cells. Excel文件为某些单元格具有“定义的名称”。 I would like to somehow open the exported HTML file, then read through the Excel file defined names collection and give the corresponding HTML table cells an ID that I can parse back to the Excel file's defined name. 我想以某种方式打开导出的HTML文件,然后通读Excel文件定义的名称集合,并给对应的HTML表单元格一个ID,我可以将其解析回Excel文件的定义名称。 Once the cells have been given IDs (and a runat="server" attribute), I want to write the modified HTML out as a string/file so I can stuff it into my ASP page. 给单元格指定ID(和runat =“ server”属性)后,我想将修改后的HTML作为字符串/文件写出,以便将其填充到ASP页面中。

Can anybody tell me a way to go about this? 有人可以告诉我解决方法吗? I thought I would be able to use the System.Web.UI.HtmlControls.HtmlTable class, then just stuff my exported HTML code into the InnerHtml property and be off to the races. 我以为我可以使用System.Web.UI.HtmlControls.HtmlTable类,然后将导出的HTML代码填充到InnerHtml属性中,开始比赛。 Alas, the InnerHtml property is not valid (write OR read) for the HtmlTable (or HtmlTableRow, but it IS valid for the HtmlTableCell for some reason). las,InnerHtml属性对HtmlTable(或HtmlTableRow)无效(写或读),但由于某种原因,它对HtmlTableCell有效。

There has to be a way to do this since, just pasting the exported HTML into an aspx page in the VS IDE, then adding id="MyTable" and runat="server" to the <table> tag will immediately give me an HtmlTable object that I can use in the context of the page. 必须有一种方法,因为,只需将导出的HTML粘贴到VS IDE中的aspx页面中,然后将id =“ MyTable”和runat =“ server”添加到<table>标记中,将立即为我提供一个HtmlTable我可以在页面上下文中使用的对象。

I'm obviously not asking for fully fleshed out and working code, just a few lines to get me started. 显然,我并不需要完全充​​实的工作代码,仅需要几行就可以开始。 Please don't kill me for my crime of ignorance! 请不要因为我的无知罪而杀了我!

If it helps, which I seriously doubt, this is a sample of what comes out of Excel when exported: 如果有帮助,我非常怀疑,这是导出时Excel产生的示例:

 <table border=0 cellpadding=0 cellspacing=0 width=856 style='border-collapse: collapse;table-layout:fixed;width:642pt'> <tr class=xl1527583 height=30 style='mso-height-source:userset;height:22.5pt'> <td colspan=4 height=30 class=xl13127583 width=442 style='border-right:.5pt solid black; height:22.5pt;width:331pt'><a name="RANGE!A1:G197">BLAH FORM</a></td> <td class=xl6427583 width=91 style='border-left:none;width:68pt'>&nbsp;</td> <td class=xl6527583 width=62 style='width:47pt'>&nbsp;</td> <td class=xl6527583 width=261 style='width:196pt'>&nbsp;</td> </tr> <tr class=xl1527583 height=21 style='height:15.75pt'> <td colspan=4 height=21 class=xl13427583 style='border-right:.5pt solid black; height:15.75pt'>Title:</td> <td colspan=3 rowspan=3 class=xl11527583 width=414 style='border-right:.5pt solid black; border-bottom:.5pt solid black;width:311pt'><font class="font827583">BLAH Management System<br> Document Number:</font><font class="font927583"><br> 12.3.456A</font></td> </tr> </table> 

There are ~4300 lines in the exported HTML - 2600 of those are style info. 导出的HTML中大约有4300行-其中2600行是样式信息。 They will be occasionally changing this form and I don't want to keep rebuilding it every time they do. 他们有时会更改此表单,我不想每次都重新构建它。

Not being one with the ability to wait around for the right way to do things, here is what I came up with. 我不是一个有能力等待正确的做事方式的人,这就是我想到的。 As I noted, the VS IDE pretty obviously has an HTML parser built into it that would work perfectly well for my purpose if I could only figure out where it 'lived' outside of the VS IDE and how to access if from my code. 正如我指出的那样,VS IDE很显然内置了HTML解析器,如果我只能弄清楚它在VS IDE之外的位置以及如何从我的代码访问,该解析器将非常适合我的目的。 I decided to try to use that in the only way I know how at the moment. 我决定尝试以目前我唯一知道的方式使用它。 I pasted my exported HTML code into a new aspx web form page. 我将导出的HTML代码粘贴到新的aspx Web表单页面中。 Then I created the Page_Init event handling code shown below. 然后,我创建了如下所示的Page_Init事件处理代码。

// This is not absolutely necessary, but you may want to include it
using System.Web.UI.HtmlControls;

  protected void Page_PreInit(object sender, EventArgs e)
  {
    StringWriter strWtr;
    StringBuilder sbldr;
    HtmlTextWriter htmlWtr;

    // Put in table modifying code here
    this.tbl_Mine.Rows[0].ID = "r1";
    this.tbl_Mine.Rows[0].Cells[0].ID = "r1c1";
    sbldr = new StringBuilder();
    strWtr = new StringWriter(sbldr);
    htmlWtr = new HtmlTextWriter(strWtr);

    this.tbl_Mine.RenderControl(htmlWtr);
    File.WriteAllText(@"C:\Temp\HtmlTblMod.htm", sbldr.ToString());
  }

When launched, the page will write out my desired updates so I can paste them in to the real aspx page. 启动后,页面将写出我想要的更新,因此我可以将它们粘贴到真实的aspx页面中。 While this works, there has GOT to be a better way to get it done... Anybody suggesting a sane approach to this task will get their answer accepted. 尽管此方法可行,但GOT还是一种更好的方法来完成它……任何建议对这项任务采取明智方法的人都将接受他们的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM