Tutorial ini membahas cara membuat tabel HTML di Java. Tutorial ini membahas konfigurasi lingkungan sistem, algoritma, dan contoh kode yang berfungsi untuk membuat tabel HTML di Java. Lebih jauh, tutorial ini mengompilasi informasi untuk meningkatkan pembuatan tabel HTML sesuai kebutuhan Anda.
Langkah-langkah Membuat Generator Tabel HTML Dasar di Java
- Siapkan IDE Anda untuk menggunakan Aspose.HTML untuk Java guna membuat tabel HTML
- Hasilkan dokumen HTML kosong menggunakan objek kelas HTMLDocument untuk menyisipkan tabel
- Nyatakan elemen tabel dengan memanggil metode createElement()
- Tetapkan gaya tabel yang berbeda dan tambahkan kolom dan baris HTML
- Buat tajuk tabel, baris dan kolom dan isi dengan data contoh
- Simpan file keluaran sebagai file HTML
Langkah-langkah berikut merangkum alur kerja untuk membuat generator tabel HTML di Java. Sebagai langkah pertama, buat file HTML kosong lalu tambahkan beberapa baris dan kolom. Terakhir, ekspor konten tabel HTML ke file untuk menyelesaikan konversi.
Kode untuk Membuat Tabel dalam HTML menggunakan Java
import com.aspose.html.*; | |
public class Main | |
{ | |
public static void main(String[] args) throws Exception // Create HTML table in Java | |
{ | |
// Set the licenses | |
new License().setLicense("License.lic"); | |
// Prepare a path | |
String savePath = "Table.html"; | |
// Initialize an empty HTML document | |
com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument(); | |
// Create a style element | |
com.aspose.html.dom.Element style = document.createElement("style"); | |
style.setTextContent("table, th, td { border: 1px dotted #90EE90; }"); | |
// Access the head and set its style | |
com.aspose.html.dom.Element head = document.getElementsByTagName("head").get_Item(0); | |
head.appendChild(style); | |
// Declare a variable body | |
com.aspose.html.dom.Element body = document.getBody(); | |
// Specify cols and rows | |
int cols = 3; | |
int rows = 2; | |
boolean isFirstRowHeader = false; | |
// Create table element | |
com.aspose.html.dom.Element table = document.createElement("table"); | |
// Create a table body | |
com.aspose.html.dom.Element tbody = document.createElement("tbody"); | |
table.appendChild(tbody); | |
// Create a table header row | |
if (isFirstRowHeader) | |
{ | |
com.aspose.html.dom.Element tr = document.createElement("tr"); | |
tbody.appendChild(tr); | |
// Create table header columns | |
for (int j = 1; j < cols + 1; j++) | |
{ | |
com.aspose.html.dom.Element th = document.createElement("th"); | |
com.aspose.html.dom.Text title = document.createTextNode("Column-" + j); | |
th.appendChild(title); | |
tr.appendChild(th); | |
} | |
for (int i = 0; i < rows - 1; i++) | |
{ | |
// Create a table row | |
com.aspose.html.dom.Element dataTr = document.createElement("tr"); | |
tbody.appendChild(dataTr); | |
// Create table header cells | |
for (int j = 1; j < cols + 1; j++) | |
{ | |
com.aspose.html.dom.Element td = document.createElement("td"); | |
com.aspose.html.dom.Text title = document.createTextNode("Data-" + j); | |
td.appendChild(title); | |
dataTr.appendChild(td); | |
} | |
} | |
} | |
else | |
{ | |
for (int i = 0; i < rows; i++) | |
{ | |
// Create a table row | |
com.aspose.html.dom.Element dataTr = document.createElement("tr"); | |
tbody.appendChild(dataTr); | |
// Create table cells | |
for (int j = 1; j < cols + 1; j++) | |
{ | |
com.aspose.html.dom.Element td = document.createElement("td"); | |
com.aspose.html.dom.Text title = document.createTextNode("Data-" + j); | |
td.appendChild(title); | |
dataTr.appendChild(td); | |
} | |
} | |
} | |
// Append table to body | |
body.appendChild(table); | |
// Save the output | |
document.save(savePath); | |
System.out.println("Table created in HTML successfully"); | |
} | |
} |
Di atas adalah versi cepat dari kode untuk membuat tabel dalam HTML menggunakan Java. Kode ini menggunakan kelas HTMLDocument untuk memulai dokumen kosong dan kemudian memformat properti visual seperti warna border, gaya borderline, dll. Selanjutnya, Anda dapat memilih untuk menambahkan baris dan kolom sebanyak yang Anda perlukan dengan memanggil metode insertCell dan insertRow.
Artikel ini telah membahas detail pembuatan pembuat tabel HTML di Java. Untuk merender Markdown ke file HTML, baca artikel di Konversi Markdown ke HTML di Java.