代码之家  ›  专栏  ›  技术社区  ›  DaveDev

有人能向HTML表格生成器推荐一个csv吗?[关闭]

  •  1
  • DaveDev  · 技术社区  · 14 年前

    我需要解析一个相当简单的csv文件,它代表7列和3行。.NET中是否有内置的功能来完成此操作,或者我应该手动完成此操作?

    4 回复  |  直到 12 年前
        1
  •  2
  •   ChrisLively    14 年前

    使用类似 FileHelpers library 要加载文件并将其转换为数据表,只需使用转发器以所需的HTML格式发出行。

        2
  •  0
  •   MUG4N    14 年前

    那怎么样?

    public static DataTable csvToDataTable(string file, bool isRowOneHeader)
    {
    
    DataTable csvDataTable = new DataTable();
    
    //no try/catch - add these in yourselfs or let exception happen
    String[] csvData = File.ReadAllLines(HttpContext.Current.Server.MapPath(file));
    
    //if no data in file ‘manually’ throw an exception
    if (csvData.Length == 0)
    {
        throw new Exception(”CSV File Appears to be Empty”);
    }
    
    String[] headings = csvData[0].Split(',');
    int index = 0; //will be zero or one depending on isRowOneHeader
    
    if(isRowOneHeader) //if first record lists headers
    {
        index = 1; //so we won’t take headings as data
    
        //for each heading
        for(int i = 0; i < headings.Length; i++)
        {
            //replace spaces with underscores for column names
            headings[i] = headings[i].Replace(” “, “_”);
    
           //add a column for each heading
            csvDataTable.Columns.Add(headings[i], typeof(string));
       } 
    }
    else //if no headers just go for col1, col2 etc.
    {
        for (int i = 0; i < headings.Length; i++)
        {
           //create arbitary column names
           csvDataTable.Columns.Add(”col”+(i+1).ToString(), typeof(string));
        }
    }
    
    //populate the DataTable
    for (int i = index; i < csvData.Length; i++)
    {
        //create new rows
        DataRow row = csvDataTable.NewRow();
    
        for (int j = 0; j < headings.Length; j++)
        {
             //fill them
             row[j] = csvData[i].Split(’,')[j];
        }
    
        //add rows to over DataTable
        csvDataTable.Rows.Add(row);
    }
    
    //return the CSV DataTable
    return csvDataTable;
    
    } 
    
        3
  •  0
  •   x0n    14 年前

    动力壳牌:

    ps> import-csv foo.csv | convertto-html foo.html
    

    ;-)

        4
  •  0
  •   DaveDev    14 年前

    结果证明这是可行的:(好吧,“这”是一个近似值)

    protected void Page_Load(object sender, EventArgs e)
    {
        List<List<string>> data = GetListFromCsv(this.DataFile);
    
        Table table = GetHtmlTable(data);
    
        this.plcDataTable.Controls.Add(table);
    }
    
    // get list of 'rows' (event though each row is just a list of strings)
    public static List<List<string>> GetListFromCsv(string file)
    {
        String[] csvData = File.ReadAllLines(file);
    
        List<string> rowList = new List<string>();
    
        if (csvData.Length == 0)
        {
            throw new Exception("CSV File Appears to be Empty");
        }
    
        var rows = (from r in csvData
                    select r.Split(',').ToList()
                   ).ToList();
    
        return rows;
    
    }
    
    private Table GetHtmlTable(List<List<string>> dataTable)
    {
        List<TableRow> rows = new List<TableRow>();
    
        rows.AddRange(GetListOfRows(dataTable));
    
        Table table = new Table();
        table.Rows.AddRange(rows.ToArray());
    
        return table;
    }
    
    // convert the 'rows' to real rows.
    public static IEnumerable<TableRow> GetListOfRows(List<List<string>> table)
    {
        var rows = new List<TableRow>();
    
        foreach (var row in table
        {
            rows.Add(GetTableRow(row));
        }
    
        return rows;
    }
    
    private static TableRow GetTableRow(List<string> rows)
    {
        TableRow row = new TableRow();
    
        row.Cells.Add(GetColumnOneCell(rows));
        row.Cells.AddRange(GetValueCells(rows).ToArray());
    
        return row;
    }