Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML TABLE to CSV converter

Tags:

java

csv

I am trying to convert a simple (but big) HTML table to CSV in Java.

I have tried looking around for code, however am non the wiser. If anyone could point me in the right direction, I'd be extremely grateful.

Here is the html table file i'm trying to convert:

<table border="1" width="100%">
<tr>
<th>Destination</th>
<th>Dial Code</th>
<th>Rate</th>
<th>Currency</th>
<th>Next Change</th>
<th>New Rate</th>
<th>Comments</th>
</tr>
<tr>
<td>Afghanistan Mobile Afghan Telecom</td>
<td>9375</td>
<td>0.1829</td>
<td>USD</td>
<td>----</td>
<td>----</td>
<td>----</td>
</tr>
<tr>
<td>Afghanistan Mobile Awcc</td>
<td>9370</td>
<td>0.1777</td>
<td>USD</td>
<td>----</td>
<td>----</td>
<td>----</td>
</tr>
<tr>
<td>Afghanistan Mobile Etisalat</td>
<td>9378</td>
<td>0.1595</td>
<td>USD</td>
<td>----</td>
<td>----</td>
<td>----</td>
</tr>
<tr>
<td>Afghanistan Mobile Mtn (Afghanistan)</td>
<td>9376</td>
<td>0.191</td>
<td>USD</td>
<td>----</td>
<td>----</td>
<td>----</td>
</tr>
like image 774
chostwales Avatar asked Jun 14 '26 11:06

chostwales


1 Answers

It is easy to do with Jsoup. You should parse every row and walk through every cell in that row, separating them by commas. Like this:

    try {
        FileWriter writer = new FileWriter("csv.txt");

        Document doc = Jsoup.parseBodyFragment(table);
        Elements rows = doc.getElementsByTag("tr");

        for (Element row : rows) {
            Elements cells = row.getElementsByTag("td");
            for (Element cell : cells) {
                writer.write(cell.text().concat(", "));
            }
            writer.write("\n");
        }
        writer.close();
    } catch (IOException e) {
        e.getStackTrace();
    }
like image 78
Yevgen Avatar answered Jun 16 '26 03:06

Yevgen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!