Can you please let me know how can we render the html table data into Excel cell using Apache POI in java. I have the below requirement like..
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;">
<tbody>
<tr>
<td>details</td>
<td>testing</td>
</tr>
<tr><td>1</td>
<td>220</td>
</tr>
<tr><td>3</td>
<td>4</td>
<td>10</td>
</tr></tbody>
</table>
but its just coming as text string.. enter image description here
Here basically you need to parse and convert HTML to data rows and then finally write to excel. I will show you how to do it using Gradle, Jsoup and apache poi
You need following dependency in your build.gradle
implementation 'org.apache.poi:poi:3.15'
implementation 'org.apache.poi:poi-ooxml:3.15'
implementation group: 'org.jsoup', name: 'jsoup', version: '1.11.3'
And here is the code to convert and write to excel
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) throws IOException {
//HTML to Data
String data = "<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:500px;\">\n" +
"<tbody>\n" +
"<tr>\n" +
"<td>details</td>\n" +
"<td>testing</td>\n" +
"</tr>\n" +
"<tr><td>1</td>\n" +
"<td>220</td>\n" +
"</tr>\n" +
"<tr><td>3</td>\n" +
"<td>4</td>\n" +
"<td>10</td>\n" +
"</tr></tbody>\n" +
"</table>";
Document doc = Jsoup.parse(data);
Element table = doc.select("table").get(0); //select the first table.
Elements rows = table.select("tr");
Element headerRow = rows.get(0);
List<String> headers = new ArrayList<>();
List<List<String>> dataRows = new ArrayList<>();
Elements headerColumns = headerRow.select("td");
for (int l = 0; l < headerColumns.size(); l++) {
headers.add(headerColumns.get(l).text());
}
System.out.println(headers);
for (int i = 1; i < rows.size(); i++) {
Element row = rows.get(i);
Elements cols = row.select("td");
List<String> dataRow = new ArrayList<>();
for (int j = 0; j < cols.size(); j++) {
dataRow.add(cols.get(j).text());
}
dataRows.add(dataRow);
}
//Data to excel
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("data");
Row header = sheet.createRow(0);
for (int k = 0; k < headers.size(); k++) {
Cell headerCell = header.createCell(k);
headerCell.setCellValue(headers.get(k));
}
for (int i = 0; i < dataRows.size(); i++) {
Row r = sheet.createRow(i + 1);
List<String> d = dataRows.get(i);
for (int j = 0; j < d.size(); j++) {
Cell dataCell = r.createCell(j);
dataCell.setCellValue(d.get(j));
}
}
File currDir = new File(".");
String path = currDir.getAbsolutePath();
String fileLocation = path.substring(0, path.length() - 1) + "Data.xlsx";
FileOutputStream outputStream = new FileOutputStream(fileLocation);
workbook.write(outputStream);
workbook.close();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.