在网页上dynamic显示CSV文件作为HTML表格

我想要一个CSV文件生活服务器端,并显示为dynamic的HTML表格。 例如,这个:

Name, Age, Sex "Cantor, Georg", 163, M 

应该成为这样的:

 <html><body><table> <tr> <td>Name</td> <td>Age</td> <td>Sex</td> </tr> <tr> <td>Cantor, Georg</td> <td>163</td> <td>M</td> </td> </table></body></html> 

欢迎任何语言的解决scheme。

以前连接的解决scheme是一个可怕的代码块; 几乎每一行都包含一个错误。 改用fgetcsv :

 <?php echo "<html><body><table>\n\n"; $f = fopen("so-csv.csv", "r"); while (($line = fgetcsv($f)) !== false) { echo "<tr>"; foreach ($line as $cell) { echo "<td>" . htmlspecialchars($cell) . "</td>"; } echo "</tr>\n"; } fclose($f); echo "\n</table></body></html>"; 

这是一个简单的函数,使用php将csv转换为html表格:

 function jj_readcsv($filename, $header=false) { $handle = fopen($filename, "r"); echo '<table>'; //display header row if true if ($header) { $csvcontents = fgetcsv($handle); echo '<tr>'; foreach ($csvcontents as $headercolumn) { echo "<th>$headercolumn</th>"; } echo '</tr>'; } // displaying contents while ($csvcontents = fgetcsv($handle)) { echo '<tr>'; foreach ($csvcontents as $column) { echo "<td>$column</td>"; } echo '</tr>'; } echo '</table>'; fclose($handle); } 

可以调用这个函数,如jj_readcsv('image_links.csv',true);

如果第二个参数是true,那么csv的第一行将作为标题/标题。

希望这有助于某人。 请评论此代码中的任何缺陷。

phihag的答案把每行放在一个单元格中,而你要求每个值在一个单独的单元格中。 这似乎是这样做的:

 <?php // Create a table from a csv file echo "<html><body><table>\n\n"; $f = fopen("so-csv.csv", "r"); while (($line = fgetcsv($f)) !== false) { $row = $line[0]; // We need to get the actual row (it is the first element in a 1-element array) $cells = explode(";",$row); echo "<tr>"; foreach ($cells as $cell) { echo "<td>" . htmlspecialchars($cell) . "</td>"; } echo "</tr>\n"; } fclose($f); echo "\n</table></body></html>"; ?> 

HTML …标签可以做到这一点,即没有PHP或Java。

或者看到这个职位上面(所有选项..)完整的细节。

http://www.linuxquestions.org/questions/programming-9/how-to-show-csv-data-as-html-in-internet-explorer-with-filter-buttons-4175443612/

我知道我有点晚了;-),但如果有人search这个,并有Wordpress安装,这是一个插件,可能会解决这个问题: https : //wordpress.org/plugins/csv-to- html / 。

如果你用\引用的逗号转义,它会起作用吗?

姓名,年龄,性别

“Cantor \,Georg”,163,M

大多数分隔符格式都要求分隔符被转义以正确parsing。


一个粗略的Java示例:

 import java.util.Iterator; public class CsvTest { public static void main(String[] args) { String[] lines = { "Name, Age, Sex", "\"Cantor, Georg\", 163, M" }; StringBuilder result = new StringBuilder(); for (String head : iterator(lines[0])) { result.append(String.format("<tr>%s</tr>\n", head)); } for (int i=1; i < lines.length; i++) { for (String row : iterator(lines[i])) { result.append(String.format("<td>%s</td>\n", row)); } } System.out.println(String.format("<table>\n%s</table>", result.toString())); } public static Iterable<String> iterator(final String line) { return new Iterable<String>() { public Iterator<String> iterator() { return new Iterator<String>() { private int position = 0; public boolean hasNext() { return position < line.length(); } public String next() { boolean inquote = false; StringBuilder buffer = new StringBuilder(); for (; position < line.length(); position++) { char c = line.charAt(position); if (c == '"') { inquote = !inquote; } if (c == ',' && !inquote) { position++; break; } else { buffer.append(c); } } return buffer.toString().trim(); } public void remove() { throw new UnsupportedOperationException(); } }; } }; } } 

定义“dynamic显示”? 这意味着表正在build立通过JavaScript和某种Ajax-Y更新..如果你只是想用PHP构build表,这真的不是我所说的“dynamic”

XmlGrid.net有工具将csv转换为html表格。 这里是链接: http : //xmlgrid.net/csvToHtml.html

我用你的示例数据,并得到了以下的HTML表格:

 <table> <!--Created with XmlGrid Free Online XML Editor (http://xmlgrid.net)--> <tr> <td>Name</td> <td> Age</td> <td> Sex</td> </tr> <tr> <td>Cantor, Georg</td> <td> 163</td> <td> M</td> </tr> </table>