Este tutorial aborda como criar a tabela HTML em Java. Ele discute as configurações do ambiente do sistema, o algoritmo e um código de amostra funcional para construir uma tabela HTML em Java. Além disso, ele compila as informações para melhorar a geração da tabela HTML de acordo com seus requisitos.
Etapas para criar um gerador de tabela HTML básico em Java
- Configure seu IDE para usar Aspose.HTML para Java para criar tabelas HTML
- Gere um documento HTML em branco usando o objeto de classe HTMLDocument para inserir uma tabela
- Declare um elemento de tabela invocando o método createElement()
- Defina diferentes estilos de tabela e anexe colunas e linhas HTML
- Crie cabeçalho de tabela, linhas e colunas e preencha com dados de amostra
- Salve o arquivo de saída como um arquivo HTML
Estas etapas resumem o fluxo de trabalho para criar um gerador de tabela HTML em Java. Como primeira etapa, crie um arquivo HTML vazio e, em seguida, anexe algumas linhas e colunas. Por fim, exporte o conteúdo da tabela HTML para um arquivo para concluir a conversão.
Código para criar tabela em HTML usando 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"); | |
} | |
} |
Acima está a versão rápida do código para criar tabela em HTML usando Java. Ele usa a classe HTMLDocument para iniciar o documento em branco e então formatar propriedades visuais como a cor da borda, estilo da linha de borda, etc. Posteriormente, você pode escolher adicionar quantas linhas e colunas precisar invocando os métodos insertCell e insertRow.
Este artigo abordou os detalhes da criação de um HTML table builder em Java. Para renderizar Markdown em um arquivo HTML, leia o artigo em Converter Markdown para HTML em Java.