HTML Table to CSV
Get a web table into a spreadsheet, with the columns still aligned
HTML Input
CSV Output
Why copying a table into a spreadsheet is harder than it looks
Select a table in the browser, paste it into Excel, and you get whatever the clipboard decided to keep: sometimes clean columns, often one fat column of text, and merged cells smeared across the wrong headings. The markup itself is far more reliable than the rendered page. Copy the <table> element from the page source, paste it here, and you get a plain comma-separated file that any spreadsheet or loader opens the same way.
CSV makes merged cells more dangerous than they are anywhere else, because a CSV line has no way to say "this value spans two fields". Take the cells of a row in markup order and a colspan="2" leaves that line one field short; every value after it slides left into the neighbouring column, and the file still opens without a single complaint. So the table is laid out on a grid first, the way the browser draws it, and a spanning cell writes its value into each field it covers. Every line comes out with the same number of commas.
Old layout-driven pages add a second problem: a table inside a cell of another table. A naive querySelectorAll("tr") returns the inner rows too, and your CSV grows lines that were never part of the data. Only the rows that belong directly to the chosen table are read here, and the inner table shows up as its own entry in the picker.
CSV itself adds a wrinkle the JSON version of this page does not have. A table cell has no type, the HTML specification has nothing to say about whether 007 is a number, but CSV cannot record a type either way. There is no toggle here for "make this a number," because a CSV field is just characters between commas; quoting it only means the value contains a comma, a quote or a line break, not that it is text. Every cell is written out exactly as its text, which happens to be the honest answer for a format with no type system of its own.
Getting the data out
- Paste the markup – Either a saved page or a single element. To grab just the table, open the developer tools on it, find the
<table>node in the Elements panel and use Copy outer HTML. - Pick the table – The picker lists every table it found, labelled with the
<caption>text, then theid, then simply Table 1, Table 2 and so on. - Check the header guess – A
<thead>, or a first row made entirely of<th>, becomes the first line of the CSV. Without either, every row, including whatever looks like a header, is written out as an ordinary line, which is what CSV expects since it has no separate concept of column names. - Choose how much gets quoted – Only where needed follows RFC 4180 and wraps a field only when it holds a comma, a quote or a line break. Every field quotes everything, for loaders that expect it regardless.
- Copy or download – Download saves
table.csvwith a comma between fields and a line feed after every row, including the last. The conversion happened in this tab, so there is nothing on a server to clean up.
When the same value shows up twice on one line of the CSV, look for the merged-cells chip in the output header before calling it a bug. If it is showing, the repetition is the span being honoured, and the source table really did say that value covers both columns.
A table with a merged cell
The third row has one cell spanning the last two columns. A parser that reads cells in order puts Suspended under iccid and drops the rsrp column entirely for that row; this one repeats the merged value into both columns it actually covers, so the CSV stays the right width.
<tr><th>msisdn</th><th>iccid</th><th>rsrp</th></tr> <tr><td>447700900142</td><td>8901240544102066246</td><td>-92</td></tr> <tr><td>447700900377</td><td colspan="2">Suspended</td></tr>
msisdn,iccid,rsrp 447700900142,8901240544102066246,-92 447700900377,Suspended,Suspended # the merged cell fills both columns it covers
When this is what you want
A one-off extraction
An operator's coverage list, a tariff table, a list of APN settings on a support page: data you need once, published only as HTML. Turning it into a CSV is quicker than retyping it and far quicker than writing a scraper you will run exactly one time.
Checking what a scraper should produce
If you are about to automate the extraction, convert one saved copy here first and keep the CSV as the expected output. When your scraper's file differs from it, the diff usually points straight at a span or a header row it handled differently.
Rescuing data from a report
Plenty of billing and monitoring systems export a report as an .html file. Opening it in Excel invites type guessing on every column; converting the markup to CSV first keeps a leading zero or a 19-digit ICCID exactly as the report printed it.
Feeding a bulk import or a script
A CSV pastes into a spreadsheet, loads with COPY or LOAD DATA INFILE, or reads with any language's standard library. If the destination speaks JSON instead, HTML Table to JSON runs the same parser and keeps values that would round as strings.
What it handles
- Spans are honoured. A
rowspanin a middle column, the case that breaks most hand-written parsers, still leaves every later field under the right header. - Inner tables are kept apart. Rows of a table nested in a cell never appear as extra lines of the outer table's CSV.
- The header line comes from the markup. A
<thead>wins, an all-<th>first row is the fallback, and a table with neither gets no header line at all. - Quoting follows RFC 4180, and only wraps a field when it actually needs it. Long IDs and zero-padded codes are written as plain digits, unquoted, exactly as the cell held them.
- Short rows are filled out with empty fields, so every line of the file has as many fields as the widest row of the table.
- Pasted markup stays inert. A
<script>copied along with the table does not run and an image beacon does not load, because the table is read out of a detached document. Nothing is uploaded.
Questions people actually ask
Can a pasted page run scripts here?
No. The markup goes through DOMParser, whose documents never run scripts or fetch images and frames. The page only reads the cells' text out of that document; nothing from your paste is ever rendered here.
Why does one value appear in two fields of the same line?
Because the source cell had a colspan or rowspan, which means the value belongs to every position it covers. The other choice, an empty field, would be a quiet loss of data, and dropping the field entirely would make that line shorter than the rest. The merged-cells chip in the output header says how many spans were filled in.
My CSV came out with no header line.
Because the first row you see as a header is, in the markup, just a row of <td> cells in bold. With no <thead> and no all-<th> row there is nothing to tell headings from data, so that row is written as the first ordinary line. In practice the CSV still looks right; your spreadsheet simply will not know it is a header.
Do I have to isolate the table first?
No. Pasting the whole page source (View Source, select all, copy) is often less fiddly than hunting for the right node, and every table in it shows up in the picker.
Do links in a cell keep their URLs?
No, only their text reaches the CSV. A link contributes its visible label, not its href, and a cell holding nothing but an image becomes an empty field. If the URLs are what you are after, that is a job for a script that reads attributes.
Why does a long ID stay exact instead of turning into a number?
Because CSV has nowhere to record that it is a number in the first place: a field is just text, and this page never runs it through Number() or anything like it. That happens to be the safe outcome: JavaScript stops holding integers exactly past 253, so a spreadsheet program that "helpfully" retypes the column on import is the thing to watch for, not this conversion.
Related tools
Worth reading
- MDN: the table element – How thead, th and scope are meant to be used, which decides whether this page finds a header line