How-to guide

How to Convert an HTML Table to CSV

Copy the table markup from the page source, paste it into a converter, and each row becomes a CSV line with cells separated by commas and quoted where needed. The two things that break alignment are merged cells, which occupy more columns than they declare, and nested tables, which produce rows that do not belong to the outer table.

HTML Table to CSV

Pull a table out of a web page and turn it into CSV.

Open the tool

Copying a table straight from a rendered page usually loses the structure: the browser hands over text with tabs or spaces, and any cell containing a line break destroys the row alignment. Working from the markup avoids that entirely, because the cell boundaries are explicit.

The awkward cases are all structural. A table designed for human reading may merge cells across columns or rows, and CSV has no way to express that — so the converter has to make a decision about where the missing values go.

Step by step

  1. Copy the markup, not the rendered table

    Right-click the table, choose Inspect, then copy the outer HTML of the table element. Selecting the table on the page and copying gives you text whose columns are only visually aligned, which is not recoverable.

  2. Include the whole table element

    Start at the opening table tag and end at its closing tag. A fragment beginning mid-row leaves the parser without the row structure, and header cells outside the copied section are lost.

  3. Decide how header cells are treated

    Header cells usually become the first CSV row. If the table has two header rows — a grouped header above a detailed one — you will normally want to keep only the detailed row and rename the columns afterwards.

  4. Check rows with merged cells

    A cell spanning two columns produces one value where the other rows have two, so everything after it shifts left. Compare the cell count of a merged row against a normal one, and insert the empty cells the span implied.

  5. Verify the quoting on text columns

    Any cell containing a comma must be quoted in the output, or the spreadsheet will split it. Check one long text cell before trusting the whole file, and remember that Excel can still damage the result when opening it.

Example

A small table and the CSV it produces. The comma inside a cell forces quoting.

HTML

<table>
  <tr><th>City</th><th>Country</th></tr>
  <tr><td>Leeds</td><td>England</td></tr>
  <tr><td>Cork</td><td>Ireland, Munster</td></tr>
</table>

CSV

City,Country
Leeds,England
Cork,"Ireland, Munster"

Frequently asked questions

Why not just copy and paste the table into Excel?
It often works, but any cell containing a line break or a tab breaks the row alignment, and the result is silently wrong rather than obviously broken. Converting from the markup keeps the cell boundaries explicit.
What happens to merged cells?
CSV has no equivalent, so a cell spanning two columns yields one value where other rows have two and everything after it shifts. Check any row that looks short and add the empty cells the span implied.
Can I convert a table that is generated by JavaScript?
Yes, as long as you copy the markup after it has rendered. Use Inspect rather than View Source — View Source shows the original document, which for a JavaScript-rendered table is usually an empty container.
What about nested tables?
They produce rows that belong to the inner table interleaved with the outer one, which rarely makes sense as a single CSV. Copy the inner table on its own if that is the data you want.
Will the CSV open correctly in Excel?
Not necessarily. Excel retypes columns on open, stripping leading zeros and reformatting dates. Import the file through Data, then From Text/CSV with every column set to Text rather than double-clicking it.

Tools used in this guide

All tools →
All guides →

Last reviewed .