Java 标题不工作生成的excel文件。。。?
我正在尝试设置标题,但它在生成的excel文件中不起作用,数据正在生成的excel上打印,但标题不起作用。请建议我使用此代码Java 标题不工作生成的excel文件。。。?,java,mongodb,apache-poi,Java,Mongodb,Apache Poi,我正在尝试设置标题,但它在生成的excel文件中不起作用,数据正在生成的excel上打印,但标题不起作用。请建议我使用此代码 public class Csv { public static void main(String args[]) throws IOException { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("My
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());
}
}
}
改变
inti=0代码中的代码>到int i=1代码>
因为您已经使用了行(0)
作为标题,所以下一行应该从行(1)
开始