我正在尝试设置标题,但它在生成的 excel 文件中不起作用,并且数据正在生成的 excel 上打印,但标题不起作用,请建议我有此代码。
public class Csv {
public static void main(String args[]) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("My Sheet");
//HSSFSheet sheet = wb.createSheet("Excel Sheet");
XSSFRow rowhead;
rowhead = sheet.createRow(0);
rowhead.createCell(0).setCellValue("Designation");
rowhead.createCell(1).setCellValue("Title");
rowhead.createCell(2).setCellValue("Domain");
rowhead.createCell(3).setCellValue("Name");
try {
// To connect to mongodb server
MongoClient mongoClient = new MongoClient("localhost", 27017);
// Now connect to your databases
DB db = mongoClient.getDB("mytest");
System.out.println("Connect to database successfully");
DBCollection coll = db.getCollection("myCollection");
System.out.println("Collection mycol selected successfully");
DBCursor cursor = coll.find();
int i = 0;
while (cursor.hasNext()) {
DBObject o = cursor.next();
//String fname = (String) o.get("Employee ID") ;
String lname = (String) o.get("Designation");
String sid = (String) o.get("Title");
String prg = (String) o.get("Domain");
String lvl = (String) o.get("Name");
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());
}
}
}
将int i = 0;
代码更改为int i = 1;
由于您已用作row(0)
标题,因此下一行应从row(1)
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句