日期:2014-05-20  浏览次数:20851 次

jxl读数据库插入excel数据换行
public tt() throws Exception{

  String path="c:\\zhbx"; 
  File lj=new File(path); 
  if(!lj.isDirectory()){
  lj.mkdir(); 
  }
  File f = new File("c:/zhbx/结果.xls");  
  f.createNewFile();
 
 
  WritableWorkbook wwb = Workbook.createWorkbook(new FileOutputStream(f));
  WritableSheet ws = wwb.createSheet("Sheet1",0);
  Label labelC;
   
  int row=0;//行
  int col=0;//列
   
  /**
  * 首行
  */
  String[] strings={"评价人","1","2","3","4","5","6","7","8","9","10",
  "11","12","13","14","15","16","17","18","19","20","21","22","23","24","25",
  "26","27","28","29","30","31","32","33","34","35","36","37","38","39","40",
  "41","42","43","44","45","46","47","48","49","50","51","52","53","54","55",
  "56","57","58","59","60","61","62","63","64","65"
  };
for(int k=0;k<strings.length;k++){
labelC =new Label(k, 0, strings[k]);
ws.addCell(labelC);
}

/**
* 列显示问卷编号SN
*/
int j=1;
  int i=0;
  String sql="select distinct r_sn from results";
  List<String> userNameList=new ArrayList<String>();
  dbcon db=new dbcon(sql);
  while(db.getRs().next()){
 
for (i =0; i <= db.getNumColumns(); i++) {
if(i!=db.getNumColumns()){
labelC=new Label(i,j,db.getRs().getString(i+1));
ws.addCell(labelC);
}
}
j++;
  }
   

  /**
  * 行输出选择结果
  */
  db=new dbcon(sql);
  while(db.getRs().next()){  
  userNameList.add(db.getRs().getString(1));
  }
 
  String userSN=null;
  int h=1;
int l=0;
int n=0;
  for (int u = 0; u < userNameList.size(); u++) {
  userSN=userNameList.get(u);
  row++;
  col++;
  sql="Select r_result from results where r_sn='"+userSN+"' order by r_sn";
 
db=new dbcon(sql);
 
while(db.getRs().next()){


for (l=0; l <= db.getNumColumns(); l++) {
if(l!=db.getNumColumns()){
labelC=new Label(h,l+1,db.getRs().getString(l+1));
ws.addCell(labelC);
}
}
h++;
}

  }
  wwb.write();
  wwb.close();
}





数据库字段
r_id,r_sn(编号),r_result(选择结果),r_order(题号)


输出结果时候r_result 在excel不换行 求高手

------解决方案--------------------
你个思路在只适合保证每个人每个问卷都有答案的情况.
查询问卷编号的时候,要带上order by 
sql="Select r_result from results where r_sn='"+userSN+"' order by r_sn";查询答案的时候order by 的字段应该是问卷编号而不是r_sn.