Java'da HTML Tablosu Oluşturma

Bu eğitim, Java’da HTML tablosunun nasıl oluşturulacağını ele alır. Sistem ortamı yapılandırmalarını, algoritmayı ve Java’da HTML tablosu oluşturmak için çalışan bir örnek kodu ele alır. Ayrıca, gereksinimlerinize göre HTML tablosu oluşturmayı iyileştirmek için bilgileri derler.

Java’da Temel Bir HTML Tablo Oluşturucu Oluşturma Adımları

  1. HTML tabloları oluşturmak için Java için Aspose.HTML‘i kullanacak şekilde IDE’nizi ayarlayın
  2. Bir tablo eklemek için HTMLDocument sınıf nesnesini kullanarak boş bir HTML belgesi oluşturun
  3. createElement() metodunu çağırarak bir tablo öğesi bildirin
  4. Farklı tablo stilleri ayarlayın ve HTML sütunları ve satırları ekleyin
  5. Tablo başlığı, satırlar ve sütunlar oluşturun ve örnek verilerle doldurun
  6. Çıktı dosyasını HTML dosyası olarak kaydedin

Bu adımlar, Java’da bir HTML tablo oluşturucusu oluşturma iş akışını özetler. İlk adım olarak, boş bir HTML dosyası oluşturun ve ardından bazı satırlar ve sütunlar ekleyin. Son olarak, dönüşümü tamamlamak için HTML tablo içeriklerini bir dosyaya aktarın.

Java kullanarak HTML’de Tablo Oluşturma Kodu

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");
}
}

Yukarıda Java kullanarak HTML’de tablo oluşturma kodunun hızlı versiyonu bulunmaktadır. Boş belgeyi başlatmak ve ardından kenarlık rengi, kenar çizgisi stili vb. gibi görsel özellikleri biçimlendirmek için HTMLDocument sınıfını kullanır. Daha sonra, insertCell ve insertRow yöntemlerini çağırarak ihtiyaç duyduğunuz kadar satır ve sütun eklemeyi seçebilirsiniz.

Bu makale, Java’da bir HTML tablo oluşturucusu oluşturmanın ayrıntılarını ele aldı. Markdown’u bir HTML dosyasına dönüştürmek için Java’da Markdown’u HTML’ye Dönüştürme makalesini okuyun.

 Türkçe