我正在使用apache poi在excelsheet上编写mongodb数据,但是它的写作首先在excel的第一行插入数据,但我需要在第一行插入数据,我想在excelsheet上打印最后一行,有人可以帮助我怎么做这是我的代码。
public static void main(String args[]) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Sheet1");
//HSSFSheet sheet = wb.createSheet("Excel Sheet");
XSSFRow rowhead;
rowhead = sheet.createRow(0);
rowhead.createCell(0).setCellValue("Employee ID");
rowhead.createCell(1).setCellValue("Name");
rowhead.createCell(2).setCellValue("Phone Number");
rowhead.createCell(3).setCellValue("Domain");
rowhead.createCell(4).setCellValue("Address");
try {
// To connect to mongodb server
MongoClient mongoClient = new MongoClient("localhost", 27017);
// Now connect to your databases
DB db = mongoClient.getDB("test");
System.out.println("Connect to database successfully");
DBCollection collection = db.getCollection("testCollection");
System.out.println("Collection mycol selected successfully");
DBCursor cursor = collection.find();
int i = 1;
while (cursor.hasNext()) {
DBObject o = cursor.next();
//String fname = (String) o.get("Employee ID") ;
String lname = (String) o.get("Name");
String sid = (String) o.get("Phone Number");
String prg = (String) o.get("Domain");
String lvl = (String) o.get("Address");
Row row = sheet.createRow(i);
row.createCell(0).setCellValue(lname);
row.createCell(1).setCellValue(sid);
row.createCell(2).setCellValue(prg);
row.createCell(3).setCellValue(lvl);
i++;
}
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
try (FileOutputStream outputStream = new FileOutputStream("datasheet.xlsx")) {
workbook.write(outputStream);
}
}
答案 0 :(得分:0)
如果当前正在以与您想要查看行的方式完全相反的顺序创建Excel,则可以创建包含DBObjects和
的Deque对象(myDeque)myDeque.addFirst(o);
直到你完成" cursor.hasNext()"循环。
之后你可以设置
DBObject o = myDeque.removeFirst();
并且您可以在将行添加到Excel时进行迭代,直到Deque对象为空。
根据要求,代码如下:
import java.util.Deque;
//Other Imports here
public static void main(String args[]) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Sheet1");
//HSSFSheet sheet = wb.createSheet("Excel Sheet");
XSSFRow rowhead;
rowhead = sheet.createRow(0);
rowhead.createCell(0).setCellValue("Employee ID");
rowhead.createCell(1).setCellValue("Name");
rowhead.createCell(2).setCellValue("Phone Number");
rowhead.createCell(3).setCellValue("Domain");
rowhead.createCell(4).setCellValue("Address");
try {
// To connect to mongodb server
MongoClient mongoClient = new MongoClient("localhost", 27017);
// Now connect to your databases
DB db = mongoClient.getDB("test");
System.out.println("Connect to database successfully");
DBCollection collection = db.getCollection("testCollection");
System.out.println("Collection mycol selected successfully");
DBCursor cursor = collection.find();
Deque<DBObject> stack = new Deque<>();
int i = 1;
while (cursor.hasNext()) {
DBObject o = cursor.next();
stack.addFirst(o);
}
while(!stack.isEmpty()){
o = stack.removeFirst();
//String fname = (String) o.get("Employee ID") ;
String lname = (String) o.get("Name");
String sid = (String) o.get("Phone Number");
String prg = (String) o.get("Domain");
String lvl = (String) o.get("Address");
Row row = sheet.createRow(i);
row.createCell(0).setCellValue(lname);
row.createCell(1).setCellValue(sid);
row.createCell(2).setCellValue(prg);
row.createCell(3).setCellValue(lvl);
i++;
}
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
try (FileOutputStream outputStream = new FileOutputStream("datasheet.xlsx")) {
workbook.write(outputStream);
}
}
答案 1 :(得分:0)
它正好用于Last in first out cursor.sort(new BasicDBObject(“_ id”, - 1));