PDA

View Full Version : تبديل داده هاي يک جدول html به فايل csv



cmm_cmm_2007@yahoo
جمعه 30 اردیبهشت 1390, 20:04 عصر
سلام.من ميخام داده هاي خودم رو که داخل تگ هاي td , tr است
به صورت فايل csv در بيارم. فايل csv به اين صورت است.مثلا دو سطر شامل
4 ستون به صورت زير در csv ذخيره ميشود.ش رديف-نام-نام خ-تلفن
فيلد4,فيلد3,فيلد2,فيلد1
فيلد4,فيلد3,فيلد2,فيلد1
خب مثلا يک کد html که من ميخام رکوردهاي داخل هر سطر که شامل 4
ستون است رو به اين صورت در بيارم.مثلا هر فايل html شامل 100 سطر 4
ستونه است.
و ميخام برنامه اي باشد که يک فايل html در يافت کنه و فايل CSV بده.
نمونه فايل html رو براتون ميزارم.

system32
جمعه 30 اردیبهشت 1390, 20:39 عصر
namespace WebLib
{
using System.Collections.Generic;
using System.Text;
using System.Web.UI.HtmlControls;

public class HTMLTableHelper
{
#region Fields

private readonly HtmlTable htmlTable;

#endregion

#region Constructors

public HTMLTableHelper(HtmlTable htmlTable)
{
this.htmlTable = htmlTable;
}

#endregion

#region Methods

/// <summary>
/// Converts HTML Table to Comma Seperated Values structure.
/// </summary>
/// <returns></returns>
public string[] ConvertToCSV()
{
//Will hold all rows of table as CSV
List<string> rows = new List<string>();

//Loop through each row in the HTML table
foreach (HtmlTableRow row in htmlTable.Rows)
{
StringBuilder rowCVS = new StringBuilder();
//Loop through each cell in the row
foreach (HtmlTableCell cell in row.Cells)
{
//Appends cell Text to rowCVS
rowCVS.Append(cell.TagName.Trim());
//Adds comma after text
rowCVS.Append(",");
}
//Removes last Comma from row
rowCVS.Remove(rowCVS.Length - 1, 1);
//Add cvs row to list of rows
rows.Add(rowCVS.ToString());
}

return rows.ToArray();
}

#endregion
}
}

cmm_cmm_2007@yahoo
شنبه 31 اردیبهشت 1390, 19:25 عصر
با تشکر از تو دوست عزیز.
اما من چطوری ازش استفاده کنم؟
چطوری صفحه html رو به کلاس htmlcontrol تخصیص بدم؟
فرضا این کلاس رو نمونه سازی کردم حالا مثلا

weblib objwl=new weblib(objhtmlcontrol)
حالا چطور صفحه html رو بدم به کلاس html control ?