2017-03-01 77 views
0

我想設置標題,但它不是在生成的Excel文件中工作,並且數據在生成的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()); 
     } 
    } 
} 

回答

0

變化int i = 0;在代碼中int i = 1;

既然你已經使用row(0)作爲標題,下一行應該從row(1)開始。

+0

感謝arun現在的工作...... :) –