我想java里web项目下,客户端点按钮时,服务器生成一csv文件,并下载到客户端用excel程序打开
1个回答
展开全部
public void createEXCEL(String title[],String width[], String sql,String path){ int i=0; int j=0; WritableWorkbook wwb =null; String sql1=sql; Connection conn=Dbconnection.getConnection(); PreparedStatement ps=null; try{ ps=conn.prepareStatement(sql); System.out.println("sql4=="+sql); ResultSet rs=ps.executeQuery(); wwb = Workbook.createWorkbook(new FileOutputStream(path)); // WritableWorkbook wwb2 = Workbook.createWorkbook(arg0) WritableSheet ws = wwb.createSheet("sheet1", 0); /** * 使用WritableCellFormat 的setWrap(true)可以设成自动换行,然后再用WritableSheet的setRowView设置行的高度,setColumnView设置列的宽度 * setColumnView(i,j)设置列的宽度设置第i列为j值 */ for( i=0;i<title.length;i++){ WritableFont wf = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD, false); WritableCellFormat wcfF = new WritableCellFormat(wf); //ws.setColumnView(i,Integer.parseInt(width[i])); Label labelCF = new Label(i, 0, title[i],wcfF); ws.addCell(labelCF); } i=1; while (rs.next()) { for(j=1;j<title.length;j++) { System.out.println("数据:"+j+"个"+rs.getString(j)); Label labelC = new Label(j-1,i, rs.getString(j)); ws.addCell(labelC); } i++; } wwb.write(); wwb.close(); rs.close(); conn.close(); }catch(Exception e){ }finally{ try{ }catch(Exception e){ } } }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询