Monday, January 13, 2014

Html in java


import javax.swing.*;

import java.io.*;

public class ExpToHTML {

private JTable _table;
private File _file;
private StringBuilder _html;

public ExpToHTML(JTable table, File file){
_table = table;
_file = file;
int rowcount = table.getRowCount();
int colcount = table.getColumnCount();

try{
BufferedWriter bw = new BufferedWriter(new FileWriter(_file));

_html.append("<tr>");
for(int i=0;i<colcount;i++){
_html.append("<th>")
    .append(_table.getColumnName(i))
    .append("</th>");
}
_html.append("</tr>\n");

for(int i=0;i<rowcount;i++){
_html.append("<tr>");
for(int k=0;k<colcount;k++){
_html.append("<td>")
    .append(GetData(_table, i, k))
    .append("</td>");
}
_html.append("</tr>\n");
}

bw.write(_html.toString());
bw.close();
}catch(Exception e){}
}

public String GetData(JTable table, int row_index, int col_index){
return table.getModel().getValueAt(row_index, col_index).toString();
}
}

No comments:

Post a Comment