我想将我的 Sqlite 导出为 PDF。我能够做到,但不是将结果放在一行中,而是将结果放在我拥有的另一列中。我想要的是用数据库结果填充同一列中的行(增加行)。这就是我所拥有的,你能帮我解决这个问题吗?
db = new Database(FirstActivity.this);
Cursor c = bd.getGroupsNamesMachines();
PdfPTable table = new PdfPTable(2);
cell = new PdfPCell(new Phrase("groups"));
cell2 = new PdfPCell(new Phrase("machines"));
if (c.moveToFirst()) {
do {
String groups = c.getString(c.getColumnIndex("groupname"));
cell3 = new PdfPCell(new Phrase(groups));
table.addCell(cell3);
} while (c.moveToNext());
}
cell.setColspan(1);
cell2.setColspan(2);
cell3.setColspan(1);
table.addCell(cell);
table.addCell(cell2);
table.addCell(cell3);
Run Code Online (Sandbox Code Playgroud)
请阅读文档,或观看此视频(在前 10 分钟结束之前,您将知道如何创建一个显示美国所有州的表格)。该示例的代码可以在这里找到:set format of header row using iText
就您而言,您正在做一些非常奇怪的事情。您创建了一个包含 2 列的表。如果每条记录有两个字段,那也没关系。但是,在添加标题单元格之前添加正文单元格。我认为您正在尝试创建一个包含 3 列的标题行(这是不合逻辑的),并且您可能没有完成最后一行(并且 iText 默认只添加完整的行)。
你需要这样的东西:
// Table
PdfPTable table = new PdfPTable(2);
// Header
PdfPCell cell1 = new PdfPCell(new Phrase("groups"));
PdfPCell cell2 = new PdfPCell(new Phrase("machines"));
table.addCell(cell1);
table.addCell(cell2);
// Data
db = new Database(FirstActivity.this);
Cursor c = db.getGroupsNamesMachines();
if (c.moveToFirst()) {
do {
String group = c.getString(c.getColumnIndex("groupname"));
cell1 = new PdfPCell(new Phrase(group));
table.addCell(cell1);
String machine = c.getString(c.getColumnIndex("machinename"));
cell2 = new PdfPCell(new Phrase(machine));
table.addCell(cell2);
} while (c.moveToNext());
}
document.add(table);
Run Code Online (Sandbox Code Playgroud)
不知道数据库代码是否OK;您有责任知道如何从数据库中获取正确的数据,但请将您非常不对称的代码与我将其更改为的逻辑代码进行比较。
| 归档时间: |
|
| 查看次数: |
2303 次 |
| 最近记录: |