Skip to content

Instantly share code, notes, and snippets.

@fdanelyan
Created April 17, 2014 13:16
Show Gist options
  • Save fdanelyan/10982615 to your computer and use it in GitHub Desktop.
Save fdanelyan/10982615 to your computer and use it in GitHub Desktop.
Generate html content from Android cursor
Cursor cursor = ... // get cursor
try {
File myFile = new File("/sdcard/cursor_content.html");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter out = new OutputStreamWriter(fOut);
out.append("<!doctype html><html><head><title>Untitled</title>");
out.append("<style>table a:link{color:#666;font-weight:700;text-decoration:none}table a:visited{color:#999;font-weight:700;text-decoration:none}table a:active,table a:hover{color:#bd5a35;text-decoration:underline}table{font-family:Arial, Helvetica, sans-serif;color:#666;font-size:12px;text-shadow:1px 1px 0 #fff;background:#eaebec;border:#ccc 1px solid;-moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;-moz-box-shadow:0 1px 2px #d1d1d1;-webkit-box-shadow:0 1px 2px #d1d1d1;box-shadow:0 1px 2px #d1d1d1;margin:20px}table th{border-top:1px solid #fafafa;border-bottom:1px solid #e0e0e0;background:0;padding:21px 25px 22px}table th:first-child{text-align:left;padding-left:20px}table tr:first-child th:first-child{-moz-border-radius-topleft:3px;-webkit-border-top-left-radius:3px;border-top-left-radius:3px}table tr:first-child th:last-child{-moz-border-radius-topright:3px;-webkit-border-top-right-radius:3px;border-top-right-radius:3px}table tr{text-align:center;padding-left:20px}table tr td:first-child{text-align:left;padding-left:20px;border-left:0}table tr td{border-top:1px solid #fff;border-bottom:1px solid #e0e0e0;border-left:1px solid #e0e0e0;background:0;padding:18px}table tr:last-child td{border-bottom:0}table tr:last-child td:first-child{-moz-border-radius-bottomleft:3px;-webkit-border-bottom-left-radius:3px;border-bottom-left-radius:3px}table tr:last-child td:last-child{-moz-border-radius-bottomright:3px;-webkit-border-bottom-right-radius:3px;border-bottom-right-radius:3px}table tr.even td,table tr:hover td{background:0}</style>");
out.append("</head><body>");
out.append("<table>");
out.append("<tr>");
String[] names = cursor.getColumnNames();
for (String string : names) {
out.append("<th>");
out.append(string);
out.append("</th>");
}
out.append("</tr>");
if (cursor.moveToFirst()) {
int colCount = cursor.getColumnCount();
do {
out.append("<tr>");
for (int i = 0; i < colCount; i++) {
out.append("<td>");
out.append(cursor.getString(i));
out.append("</td>");
}
out.append("</tr>");
} while (cursor.moveToNext());
}
out.append("</table>");
out.append("</body></html>");
out.close();
fOut.close();
cursor.close();
Toast.makeText(this, "Done writing to: " + myFile.getPath(), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment